how to get data from external api in laravel code example

Example 1: laravel send http post request json

use GuzzleHttp\Client;
class yourController extends Controller {

    public function saveApiData()
    {
        $client = new Client();
        $res = $client->request('POST', 'https://url_to_the_api', [
            'form_params' => [
                'client_id' => 'test_id',
                'secret' => 'test_secret',
            ]
        ]);
        echo $res->getStatusCode();
        // 200
        echo $res->getHeader('content-type');
        // 'application/json; charset=utf8'
        echo $res->getBody();
        // {"type":"User"...'
}

Example 2: how to save data from api to laravel

$res = $gallery->request('GET','https://www.instagram.com/explore/tags/MIZONEDanceBattle/?__a=1');
    $data = json_decode($res->getBody()->getContents(), true);

    $gallery = Gallery::firtsOrCreate([
            'shortcode',
            'thumbnail_src',
        ], $data]); // add $data here
    $gallery->save();

Tags:

Php Example