Skip to content

Instantly share code, notes, and snippets.

@kruxor
Last active August 29, 2015 14:06
Show Gist options
  • Save kruxor/3052539c7c78892c2675 to your computer and use it in GitHub Desktop.
Save kruxor/3052539c7c78892c2675 to your computer and use it in GitHub Desktop.
Post back to a URL using curl
function httpPost($url,$params)
{
$postData = '';
//create name value pairs seperated by &
foreach($params as $k => $v)
{
$postData .= $k . '='.$v.'&';
}
rtrim($postData, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
$params = array(
"name" => "Ravishanker Kusuma",
"age" => "32",
"location" => "India"
);
echo httpPost("http://hayageek.com/examples/php/curl-examples/post.php",$params);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment