-
-
Save frankyso/0147323ccf197811726ba0a5a04c2acf to your computer and use it in GitHub Desktop.
Membuat API kode POS Indonesia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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