Laravel guzzle

1.composer require guzzlehttp/guzzle
2. Create route
3. Write in controller
use GuzzleHttp\Client;
GET Request:
public function getRequest()
    {
        $client = new \GuzzleHttp\Client();
        $request = $client->get('http://localhost:8001/api/index');
        $response = $request->getBody()->getContents();
        echo '<pre>';
        print_r($response);
        exit;
    }
POST Request:
public function postRequest()
{
    $client = new \GuzzleHttp\Client();
    $response = $client->request('POST', 'http://localhost:8001/api/store', [
        'form_params' => [
            'name' => 'krunal',
        ]
    ]);
    $response = $response->getBody()->getContents();
    echo '<pre>';
    print_r($response);
}
Source: https://appdividend.com/2018/04/17/laravel-guzzle-http-client-example/
The following tutorials are the best tutorials:
1. https://github.com/guzzle/guzzle
2. https://guzzle.readthedocs.io/en/latest/

Labels: ,

© copyright-2020 Rejaul