How to receive data from API using Guzzle

Times have come, when applications are no longer isolated, so we, developers, have to adapt. RESTful web services are now getting more and more attention, but, before diving into specifics of REST, you should know how to access simple API and how to receive data from it. Just read and you will see. 🙂

In old times, PHP developers (me included) were using cURL library, but it is a bit pain in the ass. Better, more readable and modern way is to use some library .. and I present you – Guzzle. What it does, you might ask. It just abstracts HTTP protocol. You don’t have to worry about losing pointer with cURL or some other low-level mistakes. Give me a few minutes and I will show you how to do it!

Lets do simple example and try to implement getting user information from GitHub. Documentation says:

GET /users/:username

That means: you send GET request to server, you get the answer. Request should be sent to https://api.github.com/users/cerkauskas, if you want to get information about cerkauskas user.
But how do we do this with code?

Before anything else, we have to set up Guzzle. Gladly, it is very easy using Composer (if you are not using Composer, start doing it, seriously). Since I have it installed, I skipped the installation part. If you have no composer, installation instructions could be found here. Follow these steps with me and you will be on your way to working with APIs in no time!

Note: I did it on my Arch Linux notebook, it should work well with other Linux-based operating systems. Talking about Windows, I may update this post when I will get access to Windows machine.

 

Step 1: Installing Guzzle

composer require guzzlehttp/guzzle:~6.0

Step 2: Create PHP file

touch github-api-sample.php

Note: If you are using some kind of framework, that works with Composer (for example Laravel or Symfony), you can skip steps from 2 to 4.

Step 3: Open your file using your favourite editor. I will be using vim.

vim github-api-sample.php

Step 4: Include autoloading

<?php

require_once 'vendor/autoload.php';

Step 5: Create Guzzle client

$client = new GuzzleHttp\Client([
    'base_uri' => 'https://api.github.com'
]);

What we are saying is create a Guzzle client, that will work with https://api.github.com API. In more technical terms, we give associative array of parameters to constructor. To learn more, check out documentation.

Step 6: Send Request and get Response

$response = $client->get('/users/cerkauskas');

Since we are using GET request, we call get method. Of course, you can call delete, head, post, etc. methods. If you know how, you can do anything that HTTP protocol allows you.

Step 7: Get body of response

$body = $response->getBody();

Responses have headers and body. I don’t care about headers for this example since it has no valuable information for me right now, so I chose to skip it. Maybe one day .. 🙂

Step 8: Do whatever you want with that data. I choose to print it out

echo '<pre>';
echo $body;
echo '</pre>';

(Last) Step 9: Go to your browser and navigate to github-api-sample.php

In my case it is http://cerkauskas.lt/github-api/github-api-sample.php. At the time of writing this, I got:

{
  "login": "cerkauskas",
  "id": 12143172,
  "avatar_url": "https://avatars.githubusercontent.com/u/12143172?v=3",
  "gravatar_id": "",
  "url": "https://api.github.com/users/cerkauskas",
  "html_url": "https://github.com/cerkauskas",
  "followers_url": "https://api.github.com/users/cerkauskas/followers",
  "following_url": "https://api.github.com/users/cerkauskas/following{/other_user}",
  "gists_url": "https://api.github.com/users/cerkauskas/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/cerkauskas/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/cerkauskas/subscriptions",
  "organizations_url": "https://api.github.com/users/cerkauskas/orgs",
  "repos_url": "https://api.github.com/users/cerkauskas/repos",
  "events_url": "https://api.github.com/users/cerkauskas/events{/privacy}",
  "received_events_url": "https://api.github.com/users/cerkauskas/received_events",
  "type": "User",
  "site_admin": false,
  "name": "Tomas Čerkauskas",
  "company": null,
  "blog": "https://cerkauskas.com/",
  "location": "Vilnius, Lithuania",
  "email": null,
  "hireable": null,
  "bio": null,
  "public_repos": 0,
  "public_gists": 2,
  "followers": 0,
  "following": 0,
  "created_at": "2015-04-27T20:48:45Z",
  "updated_at": "2016-06-10T11:50:11Z"
}

You should get something similar.
And that’s it! How do you like it? It is so easy and so rewarding. 🙂
Complete code, that I used:

Did you got it successfully first time? What was the hardest part? Or was it piece of cake? Share your experience in comments! 🙂

If you want to dive more deeply, feel free to read documentation and learn more about Guzzle. If you do, please share what interesting you have found out!

One Comments

  • Dima

    28th June 2016

    really helpful, thanks

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.