How To Use WordPress HTTP API

Here we will learn about how to use WordPress HTTP API.

HTTP stands for Hypertext Transfer Protocol.

The HTTP API can be used to interact with other APIs like the Google Maps API.

There are three most common pre-built functions in WordPress.

GET :

It is used to retrieve data.

Every time you view a website data from an API you are seeing the result of a GET request.

POST :

It is used to send data to the server for the server to act upon in some way.

HEAD :

It is essentially the same as a GET request except that it does not retrieve the data, only information about the data.

Common Codes :

Status Code Description
200 OK – Request was successful
301 Resource was moved permanently
302 Resource was moved temporarily
403 Forbidden – Usually due to an invalid authentication
404 Resource not found
500 Internal server error
503 Service unavailable

Here are two methods to get and post with an example.

1) Function wp remote get :

Get the response from the HTTP request using the GET method.

Syntax:

<?php $response = wp_remote_get( $url, $args ); ?>

Parameters:

$url :- (string)(required) Site URL to retrieve.

$args :- (array)(optional) Override the defaults.

Default Usage for args:

<?php 
    global $wp_version;
    $args = array(
      'timeout'     => 5,
      'redirection' => 5,
      'httpversion' => '1.0',
      'user-agent'  => 'WordPress/' . $wp_version . '; ' . home_url(),
      'blocking'    => true,
      'headers'     => array(),
      'cookies'     => array(),
      'body'        => null,
      'compress'    => false,
      'decompress'  => true,
      'sslverify'   => true,
      'stream'      => false,
      'filename'    => null
    ); 
?>

 

The following defaults are changed via the $args parameter:

  • method – GET
  • timeout – Define time to wait before giving up.
  • redirection –  How many times to follow redirections.
  • httpversion – 1.0
  • blocking – Page wait to finish operation is complete?
  • headers – array()
  • body – null
  • cookies – array()

Examples :

1. Get a remote URL:

<?php
$response = wp_remote_get( 'http://www.API_DOMAIN.com/index.html' );
if ( is_array( $response ) ) {
  $header = $response['headers']; // array of http header lines
  $body = $response['body']; // use the content
}
?>

2. Get a remote URL with arguments:

<?php wp_remote_get( 'http://www.example.com/index.php?action=foo', array( 'timeout' => 120, 'httpversion' => '1.1' ) ); ?>

2) Function wp remote post :

The URL using the HTTP POST method and returning array value.

Syntax:

<?php wp_remote_post( $url, $args ); ?>

Parameters:

$url :- (string)(required)(URL).

$args :- (array)(optional) Optional.

Examples :

wp_remort_post data sent as an array.

<?php
$response = wp_remote_post( $url, array(
  'method' 	  => 'POST',
  'timeout' 	  => 45,
  'redirection' => 5,
  'httpversion' => '1.0',
  'blocking' 	  => true,
  'headers' 	  => array(),
  'body' 		  => array( 'username' => 'bob', 'password' => '1234xyz' ),
  'cookies'     => array()
  )
);

if ( is_wp_error( $response ) ) {
   $error_message = $response->get_error_message();
   echo "Something went wrong: $error_message";
} else {
   echo 'Response:<pre>';
   print_r( $response );
   echo '</pre>';
}
?>

Above example, $response[‘body’] returned page content by the server.

1 Comment

  1. Mannan

    Good article…

    0
    0
    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories