Last active
August 29, 2015 14:13
-
-
Save jeffward3283/d4334062df40cc3720ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As seen on http://timebath.net/no-need-for-curl/