Recommend this page to a friend! |
Classes of Subin Siby | Lobby | includes/src/vendor/rmccue/requests/docs/usage.md | Download |
|
DownloadUsageReady to go? Make sure you have Requests installed before attempting any of the steps in this guide. Loading RequestsBefore we can load Requests up, we'll need to make sure it's loaded. This is a simple two-step:
If you'd like to bring along your own autoloader, you can forget about this completely. Make a GET RequestOne of the most basic things you can do with HTTP is make a GET request. Let's grab GitHub's public timeline:
Using the Response ObjectNow that we have the response from GitHub, let's get the body of the response.
Custom HeadersIf you want to add custom headers to the request, simply pass them in as an associative array as the second parameter:
Make a POST RequestMaking a POST request is very similar to making a GET:
You'll probably also want to pass in some data. You can pass in either a
string, an array or an object (Requests uses [ [build_query]: http://php.net/http_build_query
This gives the output:
To send raw data, simply pass in a string instead. You'll probably also want to set the Content-Type header to ensure the remote server knows what you're sending it:
Note that if you don't manually specify a Content-Type header, Requests has undefined behaviour for the header. It may be set to various values depending on the internal execution path, so it's recommended to set this explicitly if you need to. Status CodesThe Response object also gives you access to the status code:
You can also easily check if this status code is a success code, or if it's an error:
Response HeadersWe can also grab headers pretty easily:
Note that this is case-insensitive, so the following are all equivalent:
If a header isn't set, this will give |