Interacting with remote data and APIs

Go through Making HTTP requests handbook page and its sub-pages to learn more about HTTP API.

Sometimes, while developing a WordPress plugin, you might find the need to integrate your plugin with remote data. That’s where wp_remote_get(), wp_remote_post(), and wp_remote_retrieve_body() come in. See also wp_remote_get() arguments, such as headers.

See all arguments and filters at WP_Http::request().

wp_remote_get() is a part of the WordPress HTTP API and it allows you to get a response from a remote server. You can use this function to get data from external sources and display it on your website.

For more information, please refer using wp_remote_get() to parse JSON from remote APIs.

When querying remote APIs, it’s important to note the potential for the query to block execution of PHP. (Requests run synchronously.) To throttle remote connections or queries through caching, see the transients API. In the linked example, results of an expensive WP_Query() are cached. This cached action could just as well have been results from wp_remote_get():

// Check cache.
$data = get_transient( 'remote_data' );
if ( false === $data ) {
    // If cache empty, send remote request.
    $data = wp_remote_retrieve_body(
                // @see WP_Http::request()
                // @see
                'headers' => [
                    // 'Authorization' => 'Basic ' . base64_encode( USERNAME . ':' . PASSWORD )
    // Set cache.
    set_transient( 'remote_data', $data, 12 * HOUR_IN_SECONDS );