WordPress


Making network requests with HTTP API All Versions

1.0
1.2
1.5
2.0
2.1
2.2
2.3
2.5
2.6
2.7
2.8
2.9
3.0
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
4.0
4.1
4.2
4.3
4.4
4.5

This draft deletes the entire topic.

inline side-by-side expand all collapse all

Examples

  • 1

    This snippet will grab a JSON formatted resource, decode it and print it in PHP array format.

    // Fetch 
    $response = wp_remote_get( 'http://www.example.com/resource.json' );
    
    if ( ! is_wp_error( $response ) ) {
      $headers = wp_remote_retrieve_headers( $response );
    
      if ( isset( $headers[ 'content-type' ] ) && 'application/json' === $headers[ 'content-type' ] ) {
        print_r( json_decode( wp_remote_retrieve_body( $response ) ) );
      }
    }
    

I am downvoting this example because it is...

Syntax

  • $response = wp_remote_get( $url, $args );
  • $response = wp_remote_post( $url, $args );
  • $response = wp_safe_remote_post( $url, $args );

Parameters

ParameterDetails
$url(string) (Required) Site URL to retrieve.
$args(array) (Optional) Request arguments.

Remarks

Returns

(WP_Error | array) The response as an array, or WP_Error on failure.

Still have a Making network requests with HTTP API question? Ask Question

GET a remote JSON resource

1

This snippet will grab a JSON formatted resource, decode it and print it in PHP array format.

// Fetch 
$response = wp_remote_get( 'http://www.example.com/resource.json' );

if ( ! is_wp_error( $response ) ) {
  $headers = wp_remote_retrieve_headers( $response );

  if ( isset( $headers[ 'content-type' ] ) && 'application/json' === $headers[ 'content-type' ] ) {
    print_r( json_decode( wp_remote_retrieve_body( $response ) ) );
  }
}

Topic Outline