Skip to content

Instantly share code, notes, and snippets.

@TravelingTechGuy
Created September 7, 2012 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TravelingTechGuy/3669985 to your computer and use it in GitHub Desktop.
Save TravelingTechGuy/3669985 to your computer and use it in GitHub Desktop.
Posting to service using CURL from PHP
public static function callService($url, $postData) {
if(stripos($_SERVER['HTTP_HOST'], "localhost") !== FALSE)
$postData .= "&XDEBUG_SESSION_START=netbeans-xdebug";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // return into a variable
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REMOTE_ADDR']);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec($ch); // run!
if($result === FALSE) {
$result = array(
'error' => curl_error($ch)
);
error_log("Curl error: {$result['error']}");
}
curl_close($ch);
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment