Guzzle: a HTTP client to php

At a time when interoperability between systems has become increasingly important and the creation/use of APIs is increasingly ascending, it is always important to think about the consumption of third party services within our projects.

In php, although it is relatively simple to make requests “http” with the native resources of the language itself, it is recommended that a library be used for this purpose, since this way we have more security and resources that facilitate and make this process more agile procedure.

One of the most popular “http client” libraries for the php language is Guzzle (https://docs.guzzlephp.org/en/stable/), which according to the official documentation, “is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services”.

Therefore, the purpose of this post is to show you how to make HTTP requests in php using Guzzle.

First of all, we need to install Guzzle in our project. To do this, I will use Composer:

composer require guzzlehttp/guzzle:^7.0

Once coupled, making basic requests using Guzzle is simple. Just instantiate the Guzzle\Client class

1
2
3
4
5
6
<?php
require 'vendor/autoload.php';
 
use GuzzleHttp\Client;
 
$client = new Client();

The $client object provides the request method where we pass in the first parameter the type of the request (Ex: GET, POST, PUT or DELETE) and in the second the URL of the web service that will be consumed.

Let’s say, for example, calling a service in the Github REST API (https://docs.github.com/en/rest). We will consume the data from the URL: https://api.github.com/users/rafaelwendel

Through this call, a JSON will be returned with the user data of rafaelwendel

1
2
3
4
5
6
7
8
<?php
require 'vendor/autoload.php';
 
use GuzzleHttp\Client;
 
$client = new Client();
$response = $client->request('GET', 'https://api.github.com/users/rafaelwendel');
echo $response->getBody();

The $response variable will receive all the data returned from the requested request. The execution of the above code will produce the following result:

{
  "login": "rafaelwendel",
  "id": 2855089,
  "node_id": "MDQ6VXNlcjI4NTUwODk=",
  "avatar_url": "https://avatars.githubusercontent.com/u/2855089?v=4",
  "gravatar_id": "",
  "url": "https://api.github.com/users/rafaelwendel",
  "html_url": "https://github.com/rafaelwendel",
  "followers_url": "https://api.github.com/users/rafaelwendel/followers",
  "following_url": "https://api.github.com/users/rafaelwendel/following{/other_user}",
  "gists_url": "https://api.github.com/users/rafaelwendel/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/rafaelwendel/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/rafaelwendel/subscriptions",
  "organizations_url": "https://api.github.com/users/rafaelwendel/orgs",
  "repos_url": "https://api.github.com/users/rafaelwendel/repos",
  "events_url": "https://api.github.com/users/rafaelwendel/events{/privacy}",
  "received_events_url": "https://api.github.com/users/rafaelwendel/received_events",
  "type": "User",
  "site_admin": false,
  "name": "Rafael Pinheiro",
  "company": null,
  "blog": "https://www.rafaelwendel.com",
  "location": "Capivari-SP, Brasil",
  "email": null,
  "hireable": null,
  "bio": "Professor no Instituto Federal de Educação, Ciência e Tecnologia de São Paulo - Campus Capivari.",
  "twitter_username": null,
  "public_repos": 10,
  "public_gists": 0,
  "followers": 19,
  "following": 15,
  "created_at": "2012-11-21T15:57:59Z",
  "updated_at": "2021-04-18T20:27:41Z"
}

To facilitate the manipulation/use of the returned data, we can convert the JSON to a php object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
require 'vendor/autoload.php';
 
use GuzzleHttp\Client;
 
$client = new Client();
$response = $client->request('GET', 'https://api.github.com/users/rafaelwendel');
 
//converts to php object
$github_user = json_decode($response->getBody());
 
//prints the "name" (Rafael Pinheiro)
echo $github_user->name;
 
//prints the "blog" (https://www.rafaelwendel.com)
echo $github_user->blog;

I hope you like the content and any questions just leave a comment.

See you =)

Holds a university degree in Information Systems, a postgraduate degree in Database Systems and a master's degree in Education with a focus on Sociocommunity Technologies. He works as a professor of technical and technological education at the Federal Institute of Education, Science and Technology of São Paulo, teaching subjects in the areas of programming, database, project development and software engineering.

Posts relacionados

Leave a Reply

Your email address will not be published. Required fields are marked *