Skip to content

Instantly share code, notes, and snippets.

@stefanocudini
Created March 24, 2012 23:43
Show Gist options
  • Save stefanocudini/2189156 to your computer and use it in GitHub Desktop.
Save stefanocudini/2189156 to your computer and use it in GitHub Desktop.
send http post request and return http headers in array form
<?
//send http post request and return http headers in array form
function http_post_request($url,$pdata,$getHeaders=false)
{
$pdata = is_array($pdata) ? http_build_query($pdata) : $pdata;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_POSTFIELDS, $pdata);
curl_setopt($ch, CURLOPT_POST, 1);
if($getHeaders)
curl_setopt($ch, CURLOPT_HEADER, 1);
else
curl_setopt($ch, CURLOPT_HEADER, 0);
# curl_setopt($ch, CURLOPT_HTTPHEADER, array("Connection: close"));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, CJAR);
curl_setopt($ch, CURLOPT_COOKIEFILE, CJAR);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$resp = curl_exec($ch);
if($getHeaders)
{
$info = curl_getinfo($ch);
$head = array();
foreach( explode("\r\n",substr($resp,0,$info['header_size'])) as $row )
if($row!='')
$head[ current(explode(': ',$row)) ] = next(explode(': ',$row));
$body = substr($resp, -$info['download_content_length']);
$resp = array($head, $body);
}
curl_close($ch);
return $resp;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment