WordPress

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.

Rahul Prajapat

I’m Rahul Prajapat. I’m a WordPress developer and Author at TheCodeHubs. I have good skill of Html, CSS, JQuery, SQL, Web API, PSD to HTML/PSD to WordPress, WordPress Theme/Plugin Customization, create or modify short code, customize visual composer short code or integrate new short code with visual composer, can create custom metabox for any post type in WordPress admin.

View Comments

Share
Published by
Rahul Prajapat

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

2 years ago