Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.06 KB | None | 0 0
  1. function get_ongkir(Request $request){
  2.         $weight = $request->input('weight');
  3.         $destination = $request->input('kota');
  4.  
  5.         try {    
  6.             $dataCourier = Courier::all();
  7.             $courier = '';
  8.    
  9.             foreach ($dataCourier as $key => $value) {
  10.                 $courier .= $value->code;
  11.                 if($key+1 != $dataCourier->count()){
  12.                     $courier.=":";
  13.                 }
  14.             }
  15.    
  16.             $endpoint = "https://pro.rajaongkir.com/api/count";
  17.    
  18.             $client = new \GuzzleHttp\Client();
  19.            
  20.             $key = env("KEY_RAJAONGKIR", "");
  21.  
  22.             $data = array(
  23.                         "origin" => 23,
  24.                         "originType" => "city",
  25.                         "destination" => (int) $destination,
  26.                         "destinationType" => "city",
  27.                         "courier" => $courier
  28.                     );
  29.  
  30.             $response = $client->post($endpoint,
  31.                 [
  32.                     'headers' => [
  33.                         'key' => $key,
  34.                         'content-type' => 'application/x-www-form-urlencoded'
  35.                     ],
  36.                     'form_params'=> $data
  37.                 ]
  38.             );
  39.  
  40.  
  41.             $content = json_decode($response, true);
  42.             $result = $content['rajaongkir']['results'];
  43.  
  44.             $courierReturn = [];
  45.  
  46.             foreach ($courierReturn as $index => $value) {
  47.                 if(count($val->costs) > 0){
  48.                     foreach ($val->cost as $idx => $val) {
  49.                         $data = array(
  50.                             "name" => strtoupper($val->code)." ".$val->service." (".$val->cost.")",
  51.                             "price" => $val->cost
  52.                         );
  53.  
  54.                         array_push($courierReturn, $data);
  55.                     }
  56.                 }
  57.             }
  58.  
  59.             return response()->json($courierReturn);
  60.  
  61.         } catch (\Throwable $th) {
  62.             abort(500, $th);
  63.         }
  64.        
  65.  
  66.        
  67.        
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement