arrow_back
Back

WordPress HTTP API: wp_remote_get, wp_remote_post, and REST helpers

Andrew Dorokhov Andrew Dorokhov schedule 2 min read
menu_book Table of Contents

Requests

wp_remote_get();
wp_remote_post();
wp_remote_head();

Example:

$response = wp_remote_get( 'https://api.github.com/users/everhard' );
Array(
    [headers] => Array(
        [server] => nginx
        [date] => Fri, 05 Oct 2012 04:43:50 GMT
        [content-type] => application/json; charset=utf-8
        [connection] => close
        [status] => 200 OK
        [vary] => Accept
        [x-ratelimit-remaining] => 4988
        [content-length] => 594
        [last-modified] => Fri, 05 Oct 2012 04:39:58 GMT
        [etag] => "5d5e6f7a09462d6a2b473fb616a26d2a"
        [x-github-media-type] => github.beta
        [cache-control] => public, s-maxage=60, max-age=60
        [x-content-type-options] => nosniff
        [x-ratelimit-limit] => 5000
    )
 
    [body] => {"type":"User","login":"blobaugh","gravatar_id":"f25f324a47a1efdf7a745e0b2e3c878f","public_gists":1,"followers":22,"created_at":"2011-05-23T21:38:50Z","public_repos":31,"email":"ben@lobaugh.net","hireable":true,"blog":"http://ben.lobaugh.net","bio":null,"following":30,"name":"Ben Lobaugh","company":null,"avatar_url":"https://secure.gravatar.com/avatar/f25f324a47a1efdf7a745e0b2e3c878f?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png","id":806179,"html_url":"https://github.com/blobaugh","location":null,"url":"https://api.github.com/users/blobaugh"}
    [response] => Array(
        [preserved_text 5237511b45884ac6db1ff9d7e407f225 /] => 200
        [message] => OK
    )
 
    [cookies] => Array()
    [filename] =>
)

Getting body:

$response = wp_remote_get( 'https://api.github.com/users/blobaugh' );
$body     = wp_remote_retrieve_body( $response );

Getting the response code:

$response = wp_remote_get( 'https://api.github.com/users/blobaugh' );
$http_code = wp_remote_retrieve_response_code( $response ); 
// If successful $http_code will contain 200.

Getting all the headers of some specific header:

$response      = wp_remote_get( 'https://api.github.com/users/blobaugh' );
$last_modified = wp_remote_retrieve_header( $response, 'last-modified' );
// $last_modified will contain [last-modified] => Fri, 05 Oct 2012 04:39:58 GMT

// All the headers:
wp_remote_retrieve_headers( $response ).

GET using basic authentication

$args = [
    'headers' => [
        'Authorization' => 'Basic ' . base64_encode( YOUR_USERNAME . ':' . YOUR_PASSWORD )
    ],
];
wp_remote_get( $url, $args );

POSTing data to an API

To send data to the server you will need to build an associative array of data. This data will be assigned to the body value. From the server side of things the value will appear in the $_POST variable as you would expect. i.e. if body => array( 'myvar' => 5 ) on the server $_POST['myvar'] = 5.

$body = array(
    'name'    => 'Jane Smith',
    'email'   => 'some@email.com',
);

$response = wp_remote_post( 'https://...', ["body" => $body] );

Make any sort of request

$args     = array(
    'method' => 'DELETE',
);
$response = wp_remote_request( 'http://some-api.com/object/to/delete', $args );
code

Need Help with Development?

Happy to help — reach out via the contacts or go straight to my Upwork profile.

work View Upwork Profile arrow_forward
Next Article

WordPress admin screens: settings API, list tables, and UI patterns

arrow_forward