Join Justin Yost for an in-depth discussion in this video Sending basic requests, part of Consuming RESTful APIs in PHP with Guzzle.
- [Instructor] Now that we've done a basic request to a site, asking for a simple GET response, we'll build upon this to improve and send some more complicated requests. To begin with, open up the first.php file we were working with before. We'll copy all of this, and we'll create a new file in our directory labeled request.php, and in here, we'll paste the copied version of first.php. From here, we can build up to sending more complicated requests.
Let's ask some basic questions about our request. What happens when we want to send more than one request to the same server? Do we need to keep re-defining the base URL for the site each and every time? No, in fact we can define this when we create our client instance. We'll try it out by modifying line four to add, as an array of parameters to our client instance, the array, with an initial key of base_uri, and then the value for that key will be http://jsonplaceholder.typicode.com/.
And now, on line eight, we can delete the start of the URI from the POST URL. So instead, it will just read as, without a forward slash, posts/1. Note the absence of the first forward slash. Since it's in the definition of the base_uri, it's not needed. Now, let's save this and run it, just to verify everything works as we expect. To do this, again go to your command line, and run the command php request.php, and everything worked just as we expected.
Let's do some more improvements, defining the HTTP verb for the request is kind of a hassle. It would be a lot easier if we could just call a method like GET, POST, delete, etc., for each type of method, rather than defining the method name as a parameter. To do this, we'll go back to our editor, and we'll replace on line six our client calling request with client will call the method GET, and we will delete line seven.
Save this and re-run it. Again, same thing as before. So this looks like it's doing everything that we expect. But let's really see this in action. After all, we did this to make it easy for us to write multiple requests, so let's go ahead and add in some more requests. Go back to our editor. We'll copy the code on line six through line 11, and paste it three more times. One, two, and three. And now we want to first try accessing a different POST, so on line 14, we can access the POST at POST two, and on line 21, we can access a different resource, so we'll access, rather than POST, we'll access comments, and what if we want to talk to a totally different service than json placeholder? Do we need to create a new instance of the client object and such? No.
We can, in fact, on line 28, add a URL. So on line 28, we'll replace posts/1 with accessing at https://httpbin.org/ip. We'll access the httpbin service, and ask it to return our current IP address. If we save all this, and we can verify everything works as we expect it to, by going back to our command line interface, and re-running the command php request.php.
Notice it takes a little while to run, but eventually we get everything back, including our current IP address. Notice we have started developing an easily swapable, configurable HTTP layer. That's the beauty of Guzzle over cURL. Keep in mind that Guzzle works over a variety of HTTP handlers to actually make the request. The primary fallback is still using cURL, but Guzzle will use whatever your system has available to send a request. This makes it advantageous as well, by not requiring some other specific library to provide your HTTP request library for you.
- Installing Guzzle
- Sending basic requests
- Sending asynchronous requests
- Sending concurrent requests
- Sending query data
- Sending request data
- Advanced Guzzle request options
- Reviewing basic Guzzle responses
- Processing response headers
- Reviewing PSR-7 requests, responses, bodies, and headers