Skip to content

Instantly share code, notes, and snippets.

@jeffward3283
Last active August 29, 2015 14:13
Show Gist options
  • Save jeffward3283/d4334062df40cc3720ff to your computer and use it in GitHub Desktop.
Save jeffward3283/d4334062df40cc3720ff to your computer and use it in GitHub Desktop.
<?php
///////////////////////////////////////
# WordPress Remote Request (Retreive)
///////////////////////////////////////
$url = 'https://external-site.com/api/?json=1';
// $response = file_get_contents($url); // old method
$request = wp_remote_get( $token_url, array( 'sslverify' => false, 'timeout' => 120 ) );
$response = wp_remote_retrieve_body( $request );
echo '<pre>';
print_r( $response );
echo '</pre>';
///////////////////////////////////////
# WordPress Remote Request (Submit)
///////////////////////////////////////
$url = 'https://external-site.com/api/?json=1';
$args = array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'body' => array( 'username' => 'bob', 'password' => '1234xyz' ),
'cookies' => array()
);
$request = wp_remote_post( $url, $args );
$response = wp_remote_retrieve_body( $request );
////////////////////////////////////////////////////
# WordPress Remote Request (Ignoring 404 Errors)
////////////////////////////////////////////////////
$url = 'https://external-site.com/api/?json=1';
$response = wp_remote_get($url); // Test out Response first....
if( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) return false;
$result = wp_remote_retrieve_body( $response ); // Now grab the contents
?>
@jeffward3283
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment