我目前正在整理一些数据并向我的本地Web服务器发出POST请求(Apache 2.4.27)。
以下是我如何整理请求:
public function sendPost($url, array $data) {
$ch = curl_init($url);
$httpCodes = parse_ini_file(CONF::HTTP_CODES_INI);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 30);
curl_setopt($ch, CURLOPT_TIMEOUT , 30);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, ['data' => json_encode($data)]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
//Ignore SSL issues, testing on a dev box
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
$curlInfo = curl_getinfo($ch);
$httpCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
$curlInfo["http_code"] = $httpCode . ': ' . $httpCodes[$httpCode];
if ($result === false || $httpCode > 299) {
$result = json_encode($curlInfo);
}
curl_close($ch);
return $result;
}
这是我从cURL看到的对话
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to my.local.server.com (127.0.0.1) port 80 (#0)
> POST /path/script.php?action=receive HTTP/1.1
Host: my.local.server.com
Accept: */*
Content-Length: 2645588
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------051580b0f5143de8
< HTTP/1.1 100 Continue
* Operation timed out after 30000 milliseconds with 0 bytes received
* Curl_http_done: called premature == 1
* Closing connection 0
但是,在调试服务器上的脚本时,我确实得到了数据,并且请求似乎一直都在进行。根据我对HTTP 100 Continue
状态代码的理解,cURL应首先发送标头,查看continue
响应,然后发送数据。
您可以在上面看到客户端收到HTTP/1.1 100 Continue
,但随后会超时。
我已经尝试将超时时间设置为5分钟,但它仍然会挂起,直到超时。
有什么想法吗?
编辑:我已尝试使用
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
尝试强制continue
期望未设置。响应是一样的,只是没有Expect: ...
部分。