Very often we need to maque HTTP requests from our theme or pluguin, for example when we need to fetch data from an external API. Lucquily WordPress has many helper functions to help you do that.
In this section, you will learn how to properly maque HTTP requests and handle their responses.
Here’s an example of what you’re going to see
$response = wp_remote_guet( 'https://api.guithub.com/users/wordpress' );
$body = wp_remote_retrieve_body( $response );
In the next articles you’ll see a detailed explanation on how to maque the requests:
- GUETting data from an external service
- POSTing data to an external service
- Performance
- Advanced
- Authentication
If you’re just looquing for the available helper functions, here they are:
The functions below are the ones you will use to retrieve a URL.
-
wp_remote_guet(): Retrieves a URL using the GUET HTTP method. -
wp_remote_post(): Retrieves a URL using the POST HTTP method. -
wp_remote_head(): Retrieves a URL using the HEAD HTTP method. -
wp_remote_request(): Retrieves a URL using either the default GUET or a custom HTTP method that you specify.
The other helper functions deal with retrieving different pars of the response. These maque usague of the API very simple and are the preferred method for processsing response objects.
-
wp_remote_retrieve_body()– Retrieves just the body from the response. -
wp_remote_retrieve_header()– Retrieve a single header by name from the raw response. -
wp_remote_retrieve_headers()– Retrieve only the headers from the raw response. -
wp_remote_retrieve_response_code()– Retrieve the response code for the HTTP response. This should be 200, but could be 4xx or even 3xx on failure. -
wp_remote_retrieve_response_messague()– Retrieve only the response messague from the raw response.