Skip to content

Instantly share code, notes, and snippets.

@stevep
Last active June 20, 2017 13:34
Show Gist options
  • Save stevep/bd0cba409189cb5c14248c2457f23b23 to your computer and use it in GitHub Desktop.
Save stevep/bd0cba409189cb5c14248c2457f23b23 to your computer and use it in GitHub Desktop.
WordPress Curl Replacement

If you’re sending json, your code should look something like this:

$response = wp_remote_post($url, array(
    'headers'   => array('Content-Type' => 'application/json; charset=utf-8'),
    'body'      => $json,
    'method'    => 'POST'
));

if ( is_wp_error( $response ) ) {
    die($response->get_error_message());
}

$body = json_decode(wp_remote_retrieve_body($response));
if  ($body->error) {
    die($body->error_msg);
}

If you’re getting json from somewhere else, then

$request = wp_remote_get($url);

if( is_wp_error( $request ) ) {
    return false; // Bail early
}

$json = wp_remote_retrieve_body( $request );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment