How I used Guzzle to invoke a 3rd party api in my project

Tram Ho

1. Foreword:

So how is calling the 3rd party api. You can understand with some pre-coded functions, processing, optimization …, our job is to read and understand what it does and make sure. tells what inputs it needs, sometimes custom, override it to match, but usually this is very rare. Generally you can understand that I have already handled it for you, your job is to pass the input I need and you will have an output as I committed.

2. What is Guzzle:

According to the document:

Understandably it makes it easy for you to generate querry strings, streaming large downloads, use HTTP cookies etc … And it doesn’t depend on cURL, PHP stream, sockets or an unbounded loop. And it will certainly support middleware: v

3. Install Guzzle:

Since it is a package, you will have to install it in your project already. You can run the script.

Let’s find out how it runs through the basic example:

First we can basically look at an example on the document to understand how it works:

First, I will create a variable of type GuzzleHttp Client (), then I can point to the request I am aiming for, here the problem is just the syntax, you will need to pay attention to what the input requirements of the api you need (yaoming). Then you can use the command echo $res->getStatusCode(); to check the status your code returns. And how you check it depends on what framework you use already: v. And as I wrote above echo $res->getBody(); to retrieve your response.

For example:

At the end of the basic syntax course, let’s move to something closer to reality:

I have an image processing server, I just import the image and that server will style it according to an online trend. My job now is to import an image, when I have imported that image it will send the image I just imported so the other server will return a response for an image, my job is to just render it. Pretty easy, right.

Ok let’s go.

First I need to connect to the other server, of course:

Next I need headers. Maybe the other party will need some special data types: v.

Ukm box then my job is to send the image file

OK everything is complete and now I just need to get this response again:

OK, let’s summarize the code:

ok that’s it

So how to test whether the request I called is correct: v, in addition to the passive way of checking getStatusCode() above, you can also check by:


I use laravel framework so I don’t use the middleware of guzzle. But dear, who we are, the developer but who. What we like is that in addition to sabotage we look for new things to apply and build projects, to make the next bug bigger than the previous one. But we will never give up, never give up because we have a “knowledge sharing community”.

According to the document, the middleware can be understood as follows:

you can learn more here:

In simple terms it allows you to control client behavior (yaoming).

Because I have not learned in depth about this part, I dare not dance the ax through the eyes of the worker. So I have to link the article I’m looking for here


Reading here, you will have an idea of ​​what Guzzle is as well as its usage and operation, right?  If you see a good article, don’t forget to leave a like and comment, thank you !!!

Share the news now

Source : Viblo