Last active
August 29, 2015 14:26
-
-
Save RadGH/0c2deda412b04f9e1866 to your computer and use it in GitHub Desktop.
Perform a POST request using curl as a function, with arguments as an array
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 | |
/** | |
* Example Usage | |
*/ | |
$url = 'http://example.org/'; | |
$vars = array( | |
'name' => 'Radley', | |
'awesome' => 1, | |
); | |
$result = curl_get_post_url( $url, $vars ); | |
if ( $result ) { | |
echo "Result: "; | |
var_dump($result); // "Radley is awesome" | |
}else{ | |
echo "Error"; // Could set it up to return error number here in the future. | |
} | |
/** | |
* END OF EXAMPLE | |
*/ | |
/** | |
* Get a POST response from a url | |
*/ | |
function curl_get_post_url( $url, $data ) { | |
$query = http_build_query( $data ); | |
$ch = curl_init(); | |
curl_setopt_array( $ch, array( | |
CURLOPT_URL => $url, | |
CURLOPT_POST => count($data), | |
CURLOPT_POSTFIELDS => $query, | |
CURLOPT_RETURNTRANSFER => true, | |
)); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return trim($result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment