Join Justin Yost for an in-depth discussion in this video Guzzle and HTTP, part of Consuming RESTful APIs in PHP with Guzzle.
HTTP in other words is the how we communicate. REST, that we're going to talk about a little bit later is the what exactly we communicate. REST dictates the type of messages we form and transmit using HTTP verbed messages. Wait, HTTP verbs? You didn't tell me we were taking a grammar course. Joke aside, HTTP verbs are the way we tell a server what to do with this information we are passing along.
So let's start breaking this down a bit. We have five main HTTP verbs that we will work with. GET. POST. PUT. PATCH. And DELETE. We'll look at each in turn. GET is the easiest and most simplest of verbs. It tells the serveR, "Hey, get this record for me and return it." POST says, "Server, post this new record that I'm asking you to add." PUT and PATCH are similar in that they tell the serveR to update an already existing record.
DELETE tells the server to delete a record. There are a few other various HTTP verbs such as HEAD, that we won't really bother with at this point in time. So what does it all mean? I have some verbs and I have HTTP. So do I just throw some verbs and I throw some text at a server, and there I'm talking to an API? Like all things, it's just a little more complicated than that. Here is a pretty simple and basic GET request.
We're using the HTTP verb GET, in this case to tell the server at example dot com, to get the file, file dot html at a certain path. Our first and only header, that's what the colon is identifying is the headers in our message, is the header for host. The host is the server that we are talking to. In this case, it provides the domain name and the matching port number. The final part is the body of our message.
In this case, we transmit just a blank line to signify an empty body. But what about our responses? Responses are the same style of messages with the addition of HTTP codes included in the message. HTTP codes? Wait, when did we introduce those? Well, right now. HTTP codes are the basic layer that the server tells the client, "Here is what I did with your request." There are five main classes of HTTP status codes.
100 level which are informational. Mostly based around reporting. I received your request and I'm still working on it. 200 level are your requests was a success. Anything from just a simple success, to a your new record was created and here it is, or other more complicated answers. 300 are redirects that tell a client, "Okay, I can't answer that question but this other place might be able to answer your question for you." 400 level tell the client, "Look, you messed something up." Simple things like a validation error.
I can't find that record, or you have an authentication issue. 500 level is the server telling the client, "Sorry, something broke and it's all my fault." You might have seen something like this when you are browsing the web. This is a 404 error. The classic file not found. This server is telling you, the client, or more specifically, your browser, that whatever you went looking for doesn't exist here. Sorry. So what does a HTTP response look like? Well, something like this.
We start with saying, "Here is the code for the response." In this case, the most typical and often used, 200, which is the response saying, "Okay, everything worked liked normal. "We received some headers telling us "the date, time of the message being sent. "The content type of the message." In this case, just plain text. The last header is the content length, to tell the client here is how many bytes to expect, if something went wrong and they don't match. And then finally, we end with the body of our message.
In this case, just a sequence of letters and numbers. That was a lot, wasn't it? Well, hopefully, not too much. But hey, HTTP powers the entire web, so it should be a fair bit of information.
- 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