Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Wordpress functions wp_remote_get() and wp_remote_post()

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.87k
    Comment on it

    Welcome to FindNerd.

    Today we are going to discuss two important functions of HTTP API in wordPress that are wp_remote_get() and wp_remote_post.

    If we check with PHP then you can get many ways to send the HTTP request but WordPress defines the standard methods to do the same. We can easily hit the third party API by passing the API URL and data to HTTP function. We all knows about the GET and POST methods available in HTTP request so we are going to discuss  both the functions available in HTTP API. Let's start with function wp_remote_get(). Please have a look.

    wp_remote_get()

    wp_remote_get function uses the GET method to fetch the response data from the specific URL. Response or result will return the header and content in it. If you have third party API then you can easily hit the API and get the data in response.

    Syntax wp_remote_get ( string $url, array $args = array() )
    Return Values On failure there will be WP_ERROR object and on success you will get the response in array.

     

    Parameters Description
    $url(string) This is required one. It is a site URL to get the result.
    $args(array)

    This is optional one but it can override the default options. We are going to mention the default ones.
                   global $wp_version;

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

     

    You can check the parameters in above table. In this, we have mentioned the default options as well. Now we are going to take a small example for better understanding.

     

    $response_arr = wp_remote_get( 'http://www.findnerd.com/data.php?range=evon', array( 'timeout' => 60, 'httpversion' => '1.1' ) );
    if ( is_array( $response_arr ) && ! is_wp_error( $response_arr ) ) {
    // get the response here
    }
    else {
    	// something wrong
    }

    In above example, we passed the URL as well options in an array format. Now we are going to discuss the function wp_remote_post().

     

    wp_remote_post()

    wp_remote_post function uses the POST method to make a HTTP request. This function is working the same as above function but using POST method for the HTTP request.

     Syntax  wp_remote_post ( string $url, array $args = array() )
     Return Value  You will get the WP_ERROR object on failure and on success there will be a response in array format.

     

    Parameter Description
     $url(string)  This is required one. It is a site URL to get the result.
     $args(array)

     This is optional one but it can override the default options. We are going to mention the default ones.

    array(
        'method'      => 'POST',
        'timeout'     => 45,
        'redirection' => 5,
        'httpversion' => '1.0',
        'blocking'    => true,
        'headers'     => array(),
        'body'        => array(
            'username' => 'findnerd',
            'password' => '123456'
        ),

     

    $response_arr = wp_remote_post( $url, array(
    	'method' => 'POST',
    	'timeout' => 60,
    	'redirection' => 5,
    	'httpversion' => '1.0',
    	'blocking' => true,
    	'headers' => array(),
    	'body' => array( 'username' => 'evon', 'password' => 'doon' ),
    	'cookies' => array()
        )
    );
    
    if ( !is_wp_error( $response_arr ) ) {
    // get the response here
    }

    In above example, we have passed the URL and different to send with POST method.

     

    Thank you for being with us.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: