Kaskus

Tech

ramoreezAvatar border
TS
ramoreez
Cara Ambil Harga Pengiriman (Ongkos Kirim) dari RajaOngkir.com
RajaOngkir menyediakan API ongkos kirim sehingga Anda dapat membuat perhitungan ongkos kirim otomatis di toko online, membuat aplikasi mobile, atau apapun sesuai kreativitas Anda. Untuk menggunakan API RajaOngkir anda harus mempunyai API Key dengan mendaftar disini. Setelah login anda bisa melihat API key di dashboard akun anda.
Cara Ambil Harga Pengiriman (Ongkos Kirim) dari RajaOngkir.com

Selanjutnya kita akan membuat tampilan antar mukanya, dalam hal ini saya akan menggunakan JQuery EasyUI versi mobile. Berikut screenshoot tampilannya.
Cara Ambil Harga Pengiriman (Ongkos Kirim) dari RajaOngkir.com

Dan hasilnya seperti gambar di bawah ini:
Cara Ambil Harga Pengiriman (Ongkos Kirim) dari RajaOngkir.com


Langkah pertama setelah kita mendapatkan API Key adalah membut tampilan antar mukanya, berikut sourcecode nya:

1. File index.php, sourcecode ini berfungsi untuk menapilkan form pengecekan ongkos kirim/ ongkir.
Code:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Raja Ongkir - Cek Ongkos Kirim</title>
<link rel="stylesheet" type="text/css" href="../jquery-easyui-1.4.2/themes/black/easyui.css">
<link rel="stylesheet" type="text/css" href="../jquery-easyui-1.4.2/themes/mobile.css">
<link rel="stylesheet" type="text/css" href="../jquery-easyui-1.4.2/themes/color.css">
<link rel="stylesheet" type="text/css" href="../jquery-easyui-1.4.2/themes/icon.css">
<link rel="stylesheet" type="text/css" href="styles.css">
[removed][removed]
[removed][removed]
[removed][removed]
[removed]
$(function(){
$('#cekOngkir').click(function(){
$('form').submit();
})
})
[removed]
</head>
<body>
<div class="easyui-navpanel" style="position:relative;padding:20px">
<header>
<div class="m-toolbar">
<div class="m-title">Raja Ongkir Form</div>
<div class="m-right">
<a href="javascriptx:void(0)" class="easyui-linkbutton">Reset</a>
</div>
</div>
</header>
<form id="ff" name="frmOngkir" method="POST" action="result.php">
<div>
<label>Kota Asal Pengiriman</label>
<input class="easyui-combobox" prompt="Kota Asal Pengiriman" name="origin" style="width:100%" required data-options="
valueField: 'city_id',
textField: 'city_name',
url:'getCities.php',
method:'get',
panelHeight:'30%'

">
<!-- <input class="easyui-textbox" prompt="Kota Asal Pengiriman" style="width:100%" data-options="required:true"> -->
</div>
<div>
<label>Kota Tujuan Pengiriman</label>
<input class="easyui-combobox" prompt="Kota Tujuan Pengiriman" name="destination" style="width:100%" required data-options="
valueField: 'city_id',
textField: 'city_name',
url:'getCities.php',
method:'get',
panelHeight:'30%' ">
<!-- <input class="easyui-textbox" prompt="Kota Tujuan Pengiriman" style="width:100%" data-options="required:true"> -->
</div>
<div>
<label>Berat Kiriman</label>
<br>
<input class="easyui-slider" value="1" style="width:100%"
data-options="
showTip:true,
rule: [0,'|',25,'|',50,'|',75,'|',100],
onChange: function(value){
$('#berat').numberbox('setValue',value);
},
">
</div>
<div>
<p> </p>
<input class="easyui-numberbox" name="weight" value="1" id="berat" style="width:20%"> gram
</div>
<div style="text-align:center;margin-top:30px">
<a class="easyui-linkbutton" style="width:100%;height:40px" id="cekOngkir"><span style="font-size:16px">Periksa Ongkir</span></a>
</div>

</form>
</div>
<style scoped>
form label{
display: block;
margin: 10px 0 5px 0;
}
</style>
</body>
</html>


2. File result.php, sourcecode ini berfungsi untuk menapilkan informasi kurir, layanan kurir dan ongkos kirimnya.
Code:

<?php
$origin = $_POST['origin'];
$destination = $_POST['destination'];
$weight = $_POST['weight'];

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "http://rajaongkir.com/api/starter/cost",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "origin=$origin&destination=$destination&weight=$weight",
CURLOPT_HTTPHEADER => array(
"key: Your api key"
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {

$data = json_decode($response, true);
}

?>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Raja Ongkir - Cek Ongkos Kirim</title>
<link rel="stylesheet" type="text/css" href="../jquery-easyui-1.4.2/themes/black/easyui.css">
<link rel="stylesheet" type="text/css" href="../jquery-easyui-1.4.2/themes/mobile.css">
<link rel="stylesheet" type="text/css" href="../jquery-easyui-1.4.2/themes/color.css">
<link rel="stylesheet" type="text/css" href="../jquery-easyui-1.4.2/themes/icon.css">
<link rel="stylesheet" type="text/css" href="styles.css">
[removed][removed]
[removed][removed]
[removed][removed]
</head>
<body>
<div class="easyui-navpanel">
<header>
<div class="m-toolbar">
<span class="m-title">
<?php echo $data['rajaongkir']['origin_details']['city_name'];?> ke <?php echo $data['rajaongkir']['destination_details']['city_name'];?> @<?php echo $weight;?>gram
</span>
</div>
</header>
<div class="easyui-accordion" fit="true" border="false">
<?php
for ($i=0; $i < count($data['rajaongkir']['results']); $i++) {
?>
<div title="<?php echo strtoupper($data['rajaongkir']['results'][$i]['name']);?>" style="padding:10px">
<table border="1" width="100%" class="table table-striped">
<tr>
<th>No.</th>
<th>Jenis Layanan</th>
<th>ETD</th>
<th>Tarif</th>
</tr>
<?php
for ($j=0; $j < count($data['rajaongkir']['results'][$i]['costs']); $j++) {
# code...
?>
<tr>
<td><?php echo $j+1;?></td>
<td>
<div style="font:bold 16px Arial"><?php echo $data['rajaongkir']['results'][$i]['costs'][$j]['service'];?></div>
<div style="font:normal 11px Arial"><?php echo $data['rajaongkir']['results'][$i]['costs'][$j]['description'];?></div>
</td>
<td align="center"> <?php echo $data['rajaongkir']['results'][$i]['costs'][$j]['cost'][0]['etd'];?></td>
<td align="right"><?php echo number_format($data['rajaongkir']['results'][$i]['costs'][$j]['cost'][0]['value']);?></td>
</tr>
<?
}
?>
</table>
</div>
<?php
}

?>
</div>
<footer>
<div class="m-toolbar">
<div class="m-left">
<a href="index.php" class="easyui-linkbutton m-back">Kembali</a>
</div>
</div>
</footer>
</div>
</body>
</html>


3. File getCities.php, sourcecode ini berfungsi untuk menapilkan data kabupaten/kota.
Code:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "http://rajaongkir.com/api/starter/city",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"key: your api key"
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {

$k = json_decode($response, true);
echo json_encode($k['rajaongkir']['results']);

}
?>


Untuk demonya sabar ya..masih nyari tumpangan hostingan :P

Silahkan di-cacimaki gan

SOURCE
Diubah oleh ramoreez 07-04-2015 06:54
nona212Avatar border
nona212 memberi reputasi
1
10K
3
GuestAvatar border
Komentar yang asik ya
Urutan
Terbaru
Terlama
GuestAvatar border
Komentar yang asik ya
Komunitas Pilihan