1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| function curlPost($url , $fields , $unixSocket = null ,$headers = array(),$useCert = array(),$cOnnectTimeout=2,$requestTimeout=3000)
{
if(strlen(trim($url)) == 0){ return ;}
$isSSL = substr($url, 0, 8) == "https://" ? true : false;
$curl = curl_init();
curl_setopt($curl , CURLOPT_URL , $url);
curl_setopt($curl , CURLOPT_HEADER , false);
curl_setopt($curl , CURLOPT_CONNECTTIMEOUT , $connectTimeout);//连接超时
if($unixSocket)
{
curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, $unixSocket);
echo "use unix socket:".$unixSocket."\r\n";
}
if($requestTimeout ==0)
{
curl_setopt($curl, CURLOPT_NOSIGNAL, true); //注意,毫秒超时一定要设置这个
curl_setopt($curl, CURLOPT_TIMEOUT_MS, 10); //注意,毫秒超时一定要设置这个
}else
{
curl_setopt($curl , CURLOPT_TIMEOUT , $requestTimeout);//执行超时
}
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); //302重定向
if(!empty($fields))
{
curl_setopt($curl , CURLOPT_POST , true);
curl_setopt($curl , CURLOPT_POSTFIELDS , $fields);
}
//处理post字符串时特殊的header
if(is_string($fields))
{
for($i=0;$i {
if(strpos($headers[$i] , "Content-Length:"))
{
array_splice($headers, $i,1);
}else
{
$i++;
}
}
array_push($headers, 'Content-Length: ' . strlen($fields));
}
if(!empty($headers))
{
for($i=0;$i {
if(strpos($headers[$i] , "Expect:"))
{
array_splice($headers, $i,1);
break;
}else
{
$i++;
}
}
$headers[] = "Expect:";
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}else
{
$headers = array("Expect:");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($curl , CURLOPT_RETURNTRANSFER , true);
if ($isSSL == true)
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl , CURLOPT_SSL_VERIFYHOST , false);
}
if(!empty($useCert)){
//设置证书
//使用证书:cert 与 key 分别属于两个.pem文件
curl_setopt($curl,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($curl,CURLOPT_SSLCERT, $useCert["cert"]);
curl_setopt($curl,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($curl,CURLOPT_SSLKEY, $useCert["key"]);
}
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
$rs = curl_exec($curl);
// echo "shit:";print_r(curl_getinfo($curl));
if($requestTimeout >0)
{
$downloadCOntentLength= curl_getinfo($curl,CURLINFO_CONTENT_LENGTH_DOWNLOAD);
$httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
if($httpCode != 200 && $downloadContentLength <0)
{
//没任何下载内容
SystemUtil::$s_lastCurlHttpCode = -404;
}else
{
SystemUtil::$s_lastCurlHttpCode = $httpCode;
}
}
curl_close($curl);
return $rs;
}
//echo curlPost("http://fpm2.okguo.com/test.php", array("a"=>"呵呵哒","b"=>'abcde'),"/run/fmp2.okguo.com.sock");
echo curlPost("http://127.0.0.1/test.php", array("a"=>"呵呵哒","b"=>'abcde'),"/home/htserver.sock"); |