症状:php curl调用https出错
排查方法:在命令行中使用curl调用试试。
原因:服务器所在机房无法验证SSL证书。
解决办法:跳过SSL证书检查。
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
症状:php curl调用curl_exec返回bool(false),命令行curl调用正常。
排查方法:
var_dump(curl_error($ch));
返回:
string(23) "Empty reply from server"
再排查:
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
返回:
HTTP/1.1 100 Continue
Connection: close
原因:php curl接收到HTTP 100就结束了,应该继续接收HTTP 200
解决方案:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
[code]
/**
* Make an HTTP request
* @param string $url
* @param string $method
* @param array $headers
* @param string $postfields
* @param bool $multi
* @return string results
*/
public function _http($url, $method, $headers = array(), $params = null, $multi = false){
if ($this->_checkCurl()) {
$body = '';
if ('GET' == $method) {
$url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($params) ? http_build_query($params) : $params);
} else {
if (! $multi) {
$body = $params;
} else {
$body = $this->_buildHttpQueryMulti($params);
$headers[] = "Content-Type: multipart/form-data; boundary=" . $this->_boundary;
}
}
return $this->_curl($url, $method, $headers, $body);
}else{
return $this->_socket($url, $params);
}
}
public function _curl($url, $method = 'POST', $headers = array(), $postfields = NULL){
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_HEADER, false);
if ('POST' == $method) {
curl_setopt($curl, CURLOPT_POST, TRUE);
if (!empty($postfields)) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
}
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
curl_setopt($curl, CURLINFO_HEADER_OUT, TRUE );
$response = curl_exec($curl);
$this->_httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$this->_httpInfo = curl_getinfo($curl);
print_r($response);
var_dump(curl_error($curl));
return $response;
}
[/code]

下一篇:Zend Studio 性能调整优化建议