Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get_ongkir(Request $request){
- $weight = $request->input('weight');
- $destination = $request->input('kota');
- try {
- $dataCourier = Courier::all();
- $courier = '';
- foreach ($dataCourier as $key => $value) {
- $courier .= $value->code;
- if($key+1 != $dataCourier->count()){
- $courier.=":";
- }
- }
- $endpoint = "https://pro.rajaongkir.com/api/count";
- $client = new \GuzzleHttp\Client();
- $key = env("KEY_RAJAONGKIR", "");
- $data = array(
- "origin" => 23,
- "originType" => "city",
- "destination" => (int) $destination,
- "destinationType" => "city",
- "courier" => $courier
- );
- $response = $client->post($endpoint,
- [
- 'headers' => [
- 'key' => $key,
- 'content-type' => 'application/x-www-form-urlencoded'
- ],
- 'form_params'=> $data
- ]
- );
- $content = json_decode($response, true);
- $result = $content['rajaongkir']['results'];
- $courierReturn = [];
- foreach ($courierReturn as $index => $value) {
- if(count($val->costs) > 0){
- foreach ($val->cost as $idx => $val) {
- $data = array(
- "name" => strtoupper($val->code)." ".$val->service." (".$val->cost.")",
- "price" => $val->cost
- );
- array_push($courierReturn, $data);
- }
- }
- }
- return response()->json($courierReturn);
- } catch (\Throwable $th) {
- abort(500, $th);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement