Advertisement
AbdulMuttaqin

Raja Ongkir Scrapping

Feb 8th, 2019
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.82 KB | None | 0 0
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  2.  
  3. <?php
  4.     $api_key = "b701886ced237add34aeca9076cfbc17";
  5.  
  6.     function get_city($key){
  7.         $data = [
  8.             'status' => false,
  9.             'result' => []
  10.         ];
  11.         $curl = curl_init();
  12.  
  13.         curl_setopt_array($curl, array(
  14.           CURLOPT_URL => "https://pro.rajaongkir.com/api/city",
  15.           CURLOPT_RETURNTRANSFER => true,
  16.           CURLOPT_ENCODING => "",
  17.           CURLOPT_MAXREDIRS => 10,
  18.           CURLOPT_TIMEOUT => 30,
  19.           CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  20.           CURLOPT_CUSTOMREQUEST => "GET",
  21.           CURLOPT_HTTPHEADER => array(
  22.             "key: ".$key
  23.           ),
  24.         ));
  25.        
  26.         $response = curl_exec($curl);
  27.         $err = curl_error($curl);
  28.        
  29.         curl_close($curl);
  30.        
  31.         if ($err) {
  32.             $data['result'] = $err;
  33.         } else {
  34.             $result = json_decode($response, true);
  35.             if ($result['rajaongkir']['status']['code'] == 200){
  36.                 $data['status'] = true;
  37.                 $data['result'] = $result['rajaongkir']['results'];
  38.             } else {
  39.                 $data['result'] = $result['rajaongkir']['status']['description'];
  40.             }
  41.         }
  42.         return $data;
  43.     }
  44.  
  45.     function get_subdistrict($city_id, $key){
  46.         $data = [
  47.             'status' => false,
  48.             'result' => []
  49.         ];
  50.         $curl = curl_init();
  51.  
  52.         curl_setopt_array($curl, array(
  53.           CURLOPT_URL => "https://pro.rajaongkir.com/api/subdistrict?city=".$city_id,
  54.           CURLOPT_RETURNTRANSFER => true,
  55.           CURLOPT_ENCODING => "",
  56.           CURLOPT_MAXREDIRS => 10,
  57.           CURLOPT_TIMEOUT => 30,
  58.           CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  59.           CURLOPT_CUSTOMREQUEST => "GET",
  60.           CURLOPT_HTTPHEADER => array(
  61.             "key: ".$key
  62.           ),
  63.         ));
  64.        
  65.         $response = curl_exec($curl);
  66.         $err = curl_error($curl);
  67.        
  68.         curl_close($curl);
  69.        
  70.         if ($err) {
  71.             $data['result'] = $err;
  72.         } else {
  73.             $result = json_decode($response, true);
  74.             if ($result['rajaongkir']['status']['code'] == 200){
  75.                 $data['status'] = true;
  76.                 $data['result'] = $result['rajaongkir']['results'];
  77.             } else {
  78.                 $data['result'] = $result['rajaongkir']['status']['description'];
  79.             }
  80.         }
  81.         return $data;
  82.     }
  83.  
  84.     function hitung_ongkir($kecamatan_asal, $kecamatan_tujuan, $kurir, $berat, $key){
  85.         $curl = curl_init();
  86.  
  87.         curl_setopt_array($curl, array(
  88.           CURLOPT_URL => "https://pro.rajaongkir.com/api/cost",
  89.           CURLOPT_RETURNTRANSFER => true,
  90.           CURLOPT_ENCODING => "",
  91.           CURLOPT_MAXREDIRS => 10,
  92.           CURLOPT_TIMEOUT => 30,
  93.           CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  94.           CURLOPT_CUSTOMREQUEST => "POST",
  95.           CURLOPT_POSTFIELDS => "origin=".$kecamatan_asal."&originType=subdistrict&destination=".$kecamatan_tujuan."&destinationType=subdistrict&weight=".$berat."&courier=".$kurir,
  96.           CURLOPT_HTTPHEADER => array(
  97.             "content-type: application/x-www-form-urlencoded",
  98.             "key: ".$key
  99.           ),
  100.         ));
  101.        
  102.         $response = curl_exec($curl);
  103.         $err = curl_error($curl);
  104.        
  105.         curl_close($curl);
  106.        
  107.         if ($err) {
  108.             $data['result'] = $err;
  109.         } else {
  110.             $result = json_decode($response, true);
  111.             if ($result['rajaongkir']['status']['code'] == 200){
  112.                 $data['status'] = true;
  113.                 $data['result'] = $result['rajaongkir']['results'][0];
  114.             } else {
  115.                 $data['result'] = $result['rajaongkir']['status']['description'];
  116.             }
  117.         }
  118.         return $data;
  119.     }
  120.  
  121.     //ambil data kota
  122.     $city = [];
  123.     $check = get_city($api_key);
  124.     if ($check['status']){
  125.         $city = $check['result'];
  126. ?>        
  127.         <form method="GET">
  128.             Kota Asal Pengiriman<br/>
  129.             <select name="kota_asal" id="kota_asal">
  130.                 <?php
  131.                     foreach ($city as $item):
  132.                         echo "<option value='".$item['city_id']."'>".$item['type']." ".$item['city_name']."</option>";
  133.                     endforeach;
  134.                 ?>
  135.             </select>
  136.             <br/><br/>
  137.             Kecamatan Asal Pengiriman<br/>
  138.             <select name="kecamatan_asal" id="kecamatan_asal">
  139.             <?php
  140.                 $check = get_subdistrict($city[0]['city_id'],$api_key);
  141.                 if ($check['status']){
  142.                     $subdistrict = $check['result'];
  143.                     foreach ($subdistrict as $item):
  144.                         echo "<option value='".$item['subdistrict_id']."'>".$item['subdistrict_name']."</option>";
  145.                     endforeach;
  146.                 }
  147.             ?>
  148.             </select>
  149.             <br/><br/><br/><br/>
  150.             Kota Tujuan Pengiriman<br/>
  151.             <select name="kota_tujuan" id="kota_tujuan">
  152.                 <?php
  153.                     foreach ($city as $item):
  154.                         echo "<option value='".$item['city_id']."'>".$item['type']." ".$item['city_name']."</option>";
  155.                     endforeach;
  156.                 ?>
  157.             </select>
  158.             <br/><br/>
  159.             Kecamatan Tujuan Pengiriman<br/>
  160.             <select name="kecamatan_tujuan" id="kecamatan_tujuan">
  161.             <?php
  162.                 $check = get_subdistrict($city[0]['city_id'],$api_key);
  163.                 if ($check['status']){
  164.                     $subdistrict = $check['result'];
  165.                     foreach ($subdistrict as $item):
  166.                         echo "<option value='".$item['subdistrict_id']."'>".$item['subdistrict_name']."</option>";
  167.                     endforeach;
  168.                 }
  169.             ?>
  170.             </select>
  171.             <br/><br/><br/><br/>
  172.             Kurir<br/>
  173.             <select name="kurir">
  174.                 <option value="jne">JNE</option>
  175.                 <option value="pos">POS Indonesia</option>
  176.                 <option value="tiki">TIKI</option>
  177.             </select>
  178.             <br/><br/>
  179.             Berat<br/>
  180.             <input type=text name="berat" value=500> gram
  181.             <br/><br/>
  182.             <button type="submit">CEK Ongkir</button>
  183.         </form>
  184. <?php      
  185.         if (isset($_GET['kota_asal'])){
  186.             $kecamatan_asal = $_GET['kecamatan_asal'];
  187.             $kecamatan_tujuan = $_GET['kecamatan_tujuan'];
  188.             $kurir = $_GET['kurir'];
  189.             $berat = $_GET['berat'];
  190.             $ongkir = hitung_ongkir($kecamatan_asal,$kecamatan_tujuan,$kurir,$berat,$api_key);
  191.             echo "<pre>";
  192.             print_r($ongkir['result']);
  193.             echo "</pre>";
  194.         }
  195.  
  196.     } else {
  197.         echo $check['result'];
  198.     }
  199. ?>
  200.  
  201. <script>
  202.     $('#kota_asal').on('change', function(){
  203.         var city_id = $(this).val();
  204.         var key = "<?=$api_key;?>";
  205.         $.ajax({
  206.             type : 'POST',
  207.             url : 'http://localhost/CekOngkirPro/cek_kecamatan.php',
  208.             data :  {'city_id' : city_id, 'key' : key},
  209.                 success: function (data) {
  210.                     $("#kecamatan_asal").html(data);
  211.             }
  212.         });        
  213.     });
  214.  
  215.     $('#kota_tujuan').on('change', function(){
  216.         var city_id = $(this).val();
  217.         var key = "<?=$api_key;?>";
  218.         $.ajax({
  219.             type : 'POST',
  220.             url : 'http://localhost/CekOngkirPro/cek_kecamatan.php',
  221.             data :  {'city_id' : city_id, 'key' : key},
  222.                 success: function (data) {
  223.                     $("#kecamatan_tujuan").html(data);
  224.             }
  225.         });        
  226.     });
  227. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement