Skip to content

Instantly share code, notes, and snippets.

@mrhmt80
Forked from bachors/kode_pos.php
Created January 10, 2017 13:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrhmt80/5668186ad75952636263e223195e8817 to your computer and use it in GitHub Desktop.
Save mrhmt80/5668186ad75952636263e223195e8817 to your computer and use it in GitHub Desktop.
Membuat API kode POS Indonesia
<?php
/* Just 4 Fun
API kode POS Indonesia
by iBacor.com */
function kode_pos($q){
// array untuk output
$result = array();
// cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.posindonesia.co.id/tarif/');
curl_setopt($ch, CURLOPT_URL,'http://www.posindonesia.co.id/tarif/source/kodepos.php');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'keyword='.$q);
if(!$html = curl_exec($ch)){
// website sdg offline
$result["status"] = "offline";
}else{
// website sdg online
$result["status"] = "online";
// merubah data json ke array
$array = json_decode($html);
// lalu kita ambil data label.y untuk diolah & dimasukan ke array cell
$cell = array();
foreach($array as $anu){
$data = explode(', ', $anu->label);
$data2 = explode(' - ', $data[1]);
$cell[] = array(
"alamat" => $data[0],
"kota" => $data2[0],
"kode" => $data2[1]
);
}
// memasukan data dari array cell ke array result
$result["data"] = $cell;
}
curl_close($ch);
// output array
return $result;
}
// keyword alamat
$alamat = 'dago bandung';
// array
$data = kode_pos($alamat);
// array to json
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
echo json_encode($data, JSON_PRETTY_PRINT);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment