Skip to content

Instantly share code, notes, and snippets.

@RadGH
Last active August 29, 2015 14:26
Show Gist options
  • Save RadGH/0c2deda412b04f9e1866 to your computer and use it in GitHub Desktop.
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
<?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