Done!

Consume External API from Laravel 5.2 using Guzzle Http Client


API could be consumed in many ways in Laravel. For e.g. you can create your own custom API Package to consume API on the remote application. Or there is a better way. This is by using the Guzzle HTTP Client.


1. First install Guzzle

//Specify Guzzle as a dependency in your project's existing composer.json file:

 {
   "require": {
      "guzzlehttp/guzzle": "~6.0"
   }
}

-> composer update

2. On your Laravel controller


use GuzzleHttp\Client as GuzzleHttpClient;
use GuzzleHttp\Exception\RequestException;

...

public function passwordHash()
{
      try {

           $client = new GuzzleHttpClient();

           $apiRequest = $client->request('GET', 'http://legacyapp.com/api/PasswordServiceAPI/hashPassword', [
                'query' => ['plain' => 'Ab1L853Z24N'],
               // 'auth' => ['John', 'password777'],       //If authentication required
               // 'debug' => true                                  //If needed to debug   
          ]);

          // echo $apiRequest->getStatusCode());
          // echo $apiRequest->getHeader('content-type'));

          $content = json_decode($apiRequest->getBody()->getContents());

      } catch (RequestException $re) {
          //For handling exception
      }
 }