Pages

Minggu, 21 Oktober 2012

membuat website cek harga pengirman (JNE)

Posted by anes 10:42 AM, under | 1 comment

akhirnya sempet juga buka dan update ini blog :D
untuk melepas rasa kangen ngeblog kali ini saya akan share cara cek harga pengiriman jne dari website kita sendiri,
ide ini datang ketika ada request untuk cek harga pengiriman jne untuk website e-commerce.
langsung aja masuk ke caranya, setelah searching2 di si om google dapet lah refrensi dari website ini.

biasakan untuk baca2 documentation nya dulu :)http://api.ongkir.info/docs/starting.html 
Selanjutnya, lakukan registrasi di http://api.ongkir.info//akun/registrasi .

Class.php file :

<?php
function get_city($query,$type)
{
//library yang harus anda download
require_once 'REST_Ongkir.php';

 $rest = new REST_Ongkir(array(
 'server' => 'http://api.ongkir.info/'
 ));

//ganti API-Key dibawah ini sesuai dengan API Key yang anda peroleh setalah mendaftar di ongkir.info
 $result = $rest->post('city/list', array(
 'query' => $query,
 'type' => $type,
 'courier' => 'jne',
 'API-Key' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456' ), 'JSON');

 try
 {
 $status = $result['status'];

 // Handling the data
 if ($status->code == 0)
 {
 return $cities = $result['cities'];
 //print_r($cities);
 //foreach ($cities->item as $item)
 //{
 //echo 'Kota: ' . $item . '<br />';
 // }
 }
 else
 {
 echo 'Tidak ditemukan kota yang diawali "band"';
 }

 }
 catch (Exception $e)
 {
 echo 'Processing error.';
 }
}

function get_cost($from, $to,$weight)
{
//library yang harus anda download
 require_once 'REST_Ongkir.php';

 $rest = new REST_Ongkir(array(
 'server' => 'http://api.ongkir.info/'
 ));

//ganti API-Key dibawah ini sesuai dengan API Key yang anda peroleh setalah mendaftar di ongkir.info
 $result = $rest->post('cost/find', array(
 'from' => $from,
 'to' => $to,
 'weight' => $weight.'000',
 'courier' => 'jne',
'API-Key' =>'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456'
 ));

 try
 {
 $status = $result['status'];

 // Handling the data
 if ($status->code == 0)
 {
 $prices = $result['price'];
 $city = $result['city'];

 echo 'Ongkos kirim dari ' . $city->origin . ' ke ' . $city->destination . '<br /><br />';

 foreach ($prices->item as $item)
 {
 echo 'Layanan: ' . $item->service . ', dengan harga : Rp. ' . $item->value . ',- <br />';
 }
 }
 else
 {
 echo 'Tidak ditemukan jalur pengiriman dari surabaya ke jakarta';
 }

 }
 catch (Exception $e)
 {
 echo 'Processing error.';
 }
}

//$kota = get_city('ban','origin');
//echo json_encode($kota);

?>
 Lalu buat file tampilannya,di sarankan gunakan jquery autocomplete agar lebih menarik tampilan dan penggunaannya.
 index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></pre>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>jQuery UI Autocomplete - Default functionality</title>
 <link rel="stylesheet" media="screen" href="js/jqueryui/css/ui-lightness/jquery-ui-1.8.13.custom.css"></script>
 <script type="text/javascript" src="js/jquery.js"></script>
 <SCRIPT language="javascript" src="js/jquery-ui.min.js"></SCRIPT>

 <script>
 $(function() {
//autocomplete untuk mencari kota asal
 $("#from").autocomplete({
 minLength: 3,
 delay: 3,
 source: function(request, response) {
 $.ajax({
 url: "data.php?type=origin",
 dataType: "json",
 data: {
 term : request.term,
 from: $('#from').val(),
 },
 success: function(data) {
 response( $.map( data, function( item )
 {
 return{
 label: item.nama_kota,
 value: item.nama_kota,
 }
 }));

 }
 });
 },
 });

//autocomplete untuk mencari kota tujuan
$("#to").autocomplete({
 minLength: 3,
 delay: 3,
 source: function(request, response) {
 $.ajax({
 url: "data.php?type=destination",
 dataType: "json",
 data: {
 term : request.term,
 to: $('#to').val(),
 },
 success: function(data) {
 response( $.map( data, function( item )
 {
 return{
 label: item.nama_kota,
 value: item.nama_kota,
 }
 }));

 }
 });
 },
 })
 });
 </script>
</head>
<body>
<h2>Menghitung Onkos Kirim JNE</h2>
<form action="" method="post">
<table width="20%" border="1" cellspacing="0" cellpadding="0">
 <tr>
 <td width="19%">From</td>
 <td width="81%"><input type="text" name="from" id="from" /></td>
 </tr>
 <tr>
 <td>to</td>
 <td>&nbsp;<input type="text" name="to" id="to" /></td>
 </tr>
 <tr>
 <td>Weight</td>
 <td>&nbsp;<input type="text" name="weight" id="weight" />Kg</td>
 </tr>
 <tr>
 <td>Couriers</td>
 <td>&nbsp;<select name="couriers"><option value="jne">JNE</option></select></td>
 </tr>
 <tr>
 <td>&nbsp;</td>
 <td align="right">&nbsp;<input type="submit" name="go" value="Go" /></td>
 </tr>
</table>

</form>

<?php
if(isset($_POST['go'])){
//include file class.php untuk memanaggil fungsi get_cost(); yang berfungsi untuk menghitung ongkos kirim.
 include('class.php');
 $from = $_POST['from'];
 $to = $_POST['to'];
 $weight = $_POST['weight'];
//pemanggilan fungsi get_cost();
get_cost($from, $to,$weight);

}
?>
</body>
</html>
<pre>

Lalu buat file data.php untuk lookup datanya

<?php

//includekan file class.php
include('class.php');
$return_arr = array();

//tangkap variable type untuk mengetahui apakan kota asal (origin) atau kota tujuan (destination)

$type = $_GET['type'];

//panggil fungsi get_city() untuk mendapatkankan nama-nama kota.
 $cities = get_city(trim($_GET['term']),$type) ;

 $i=0;
 foreach ($cities->item as $value) {
 $row_array['id_kota'] = $i;
 $row_array['nama_kota'] = strval($value);

 array_push($return_arr,$row_array);
 $i++;
 }

/*convert ke dalam bentuk JSON. kira2 formatnya seperti ini
[{"id_kota":0,"nama_kota":"BANDAACEH"},{"id_kota":1,"nama_kota":"BANDARLAMPUNG"},{"id_kota":2,"nama_kota"</code><code>:"BANDUNG"},{"id_kota":3,"nama_kota":"BANGUI, CENTRAL AFRICAN REP."},{"id_kota":4,"nama_kota":"BANJARMASIN"</code><code>},{"id_kota":5,"nama_kota":"BANJUL, GAMBIA"}]
*/
echo json_encode($return_arr);
?>
 Oke sekian share dari saya, semoga bisa membantu anda :)

Thanks to :
http://ongkir.info
Refrence :
http://ramoreez.wordpress.com/2012/08/03/cara-ambil-harga-pengiriman-jne-dengan-php/






1 komentar:

yang itu udah ku coba kurang detail sekarang ane dah bikin sendiri di www.edydava.net

Posting Komentar