Jumat, 22 Januari 2021

Laravel API Client dengan Guzzle


1. composer require guzzlehttp/guzzle


<?php
    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use GuzzleHttp\Client;
    use App\Models\Branch;
    class ProductController extends Controller
    {
    public function index()
    {
    $branch = Branch::query();
    $branch->truncate();
    $url = 'https://mnfdev.agrobogautama.co.id/api/getbranch';
    $client = new Client();
    $headers = [
    'Content-Type' => 'application/json',
    'x-api-key' => 'xTYuaskl76510!#$%Zhyuulattr6!',
    ];
    $data = '{}';
    $options = [
    'body' => $data,
    'headers' => $headers,
    ];
    $response = $client->request('get', $url, $options);
    $res = json_decode($response->getBody());
    $data = [];
    $res = $res->result->data;
    foreach($res as $key => $item){
    $data[] = [
    "branch_id"=> $item->branch_id,
    "branch_name"=> $item->branch_name,
    "branch_latitude"=> $item->branch_latitude,
    "branch_longitude"=> $item->branch_longitude,
    "company_id"=> $item->company_id,
    "company_name"=> $item->company_name,
    "region_id"=> $item->region_id,
    "region_name"=> $item->region_name
    ];
    }
    $jd = count($res);
    $chunk = array_chunk($data,$jd);
    foreach($chunk as $d){
    Branch::insert($d);
    }
        }
    }


Tidak ada komentar:

Posting Komentar

flutter firebase notification