Skip to content

Instantly share code, notes, and snippets.

@corerman
Created May 6, 2017 02:15
Show Gist options
  • Save corerman/b003db7f1c82a3456090ad764995c1e0 to your computer and use it in GitHub Desktop.
Save corerman/b003db7f1c82a3456090ad764995c1e0 to your computer and use it in GitHub Desktop.
PHP post request (custom header and fast)
/*
* Use curl for http to send body for the form array, header for the head array
* Reference: http: //www.q3060.com/list3/list117/28490.html
*/
function sendPostRequest($url,$body,$header=null)
{
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect: ")); //Increase the sending speed :
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
if($header!=null)
{
$headerArray=array();
$i=0;
foreach($header as $key=>$value) //The header array is converted to HTTP header header format
{
$headerArray[$i]=$key.":".$value;
$i++;
}
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headerArray );
}
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
$filde_string=http_build_query($body);
curl_setopt ( $ch, CURLOPT_POSTFIELDS,$filde_string);
$return = curl_exec ( $ch );
curl_close ( $ch );
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment