Language:

Search

Calling Rest API in PHP using Guzzle Client

  • Share this:
Calling Rest API in PHP using Guzzle Client

The Traditional Way

If you've been working with PHP lately, then you must have gone through the time where you need to consume some sort of Restful API for your application.

The very first thought of utilising HTTP calls is through CURL. Let's take a look at how we used CURL for a simple GET request in our application.

public function curl_call (){
      $curl = curl_init();
      curl_setopt_array( $curl, array(
         CURLOPT_URL            => "mydomain.com/api",
         CURLOPT_RETURNTRANSFER => true,
         CURLOPT_ENCODING       => "",
         CURLOPT_MAXREDIRS      => 10,
         CURLOPT_TIMEOUT        => 30,
         CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
         CURLOPT_CUSTOMREQUEST  => "GET",
         CURLOPT_HTTPHEADER     => array(
            "authorization: <some_token>",
            "cache-control: no-cache",
         ),
      ) );
      response = curl_exec( $curl );
      $err            = curl_error( $curl );
      curl_close( $curl );
}

Well it seemed ok at first if you need to make a couple of API calls, but when your entire application depends on API calls, with tons of form data and authentication headers, you might need to rethink your choice of using CURL for HTTP calls.

Introducing Guzzle - The perfect alternative

Guzzle is a PHPHTTP Client which makes it easy to send HTTP requests over the web services medium.

Installing guzzle

Make sure you've already installed composer on your machine. If not we got you covered.

With composer installed, head over to your terminal window inside of your project root. Run the following command to install guzzle.

composer require guzzlehttp/guzzle

Using guzzle

Include the required namespaces in your working file to avoid any errors.

use GuzzleHttp\Client;

Now lets make a simple GET request to our API and see how it works out.

$client = new Client();
$response = $client->get( "mydomain.com/api" , [
   'headers' =>
      [
         'authorization' => '<some_token>'
      ]
] );

Simple enough?

Getting Response Data

On successful API call, you'll see a statusCode of 200 along with the other parameters, such as:

Response {#318 ▼
  -reasonPhrase: "OK"
  -statusCode: 200
  -headers: array:9 []
  -headerNames: array:9 []
  -protocol: "1.1"
  -stream: Stream {#316 ▶}
}

Decoding response body

var_dump(\GuzzleHttp\json_decode($result->getBody()));

Conclusion

Give guzzle a try yourself in your next API driven application and see how it'll save you tons of extra lines of code.

If you have any questions regarding this article, do leave us a comment below. You can also follow us on Twitter.

Usama Muneer

Usama Muneer

A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.