【发布时间】:2013-08-21 09:47:31
【问题描述】:
我正在尝试使用带有 php curl 的 Web 服务,但我收到了 http 代码“100”和“Recv failure: Connection was reset”错误。我使用 google chrome 的扩展程序“Postman”测试了 Web 服务-REST 客户端”,它运行良好。我搜索了这个 http 代码,看起来服务器正在等待正文请求数据,但我不知道它希望我发送什么,因为我没有发布任何数据,只是标题。
我的代码如下所示:
error_reporting(-1);
$header = array('Content-Length: 1831', 'Content-Type: application/json', 'Authorization: '. $authorization, 'Authorization-Admin: '. $authorization_admin);
$url = 'http://api.bookingreports.com/public/1/analytics/reports/departures';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$result = curl_exec($ch);
$errorCode = curl_getinfo($ch, CURLINFO_HEADER_OUT);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $result;
echo curl_error($ch);
echo $errorCode;
echo $httpcode;
//log_message('debug', 'HTTP HEADER '. $errorCode);
curl_close($ch);
我发送的标题如下所示:
POST /public/1/analytics/reports/departures HTTP/1.1
Host: api.bookingreports.com
Accept: */*
Content-Length: 1831
Content-Type: application/json
Authorization: myauthcode
Authorization-Admin: myauthadmin
Expect: 100-continue
【问题讨论】:
-
我注意到 cURL 为您添加了一个额外的
Expect标头,所以我认为这个问题可能会对您有所帮助:stackoverflow.com/questions/14158675/… -
@AgreeOrNot 我已经尝试过这两种解决方案.. FAILONERROR 没有任何区别,如果我从标头中删除 Expect,则响应的 http 代码为“0”,我是意识到意味着找不到主机。我不明白这一点,因为就像我说的那样,我已经使用 google chrome 的 REST CLIENT 扩展测试了这个网络服务,它运行良好。..
-
问题可能出在
Content-Length: 1831上吗?如果您不发布任何数据,则应该是0。 -
@AgreeOrNot 我也刚刚注意到,当我从头文件中删除 Expect 时,头文件中的头卷曲会发生变化,所以它看起来像这样“头 /public/1/analytics/reports/departures HTTP/1.1".. 这一定是问题,因为 api 需要 POST 方法。知道为什么 curl 这样做或如何解决它吗?
-
@AgreeOrNot 问题解决了!正如你所说,我将 content-length 更改为 0 并解决了 HEAD 问题,这要归功于这个问题stackoverflow.com/questions/9474971/… .. 我添加了这一行 curl_setopt($ch, CURLOPT_NOBODY, true);尝试的东西.. 非常感谢你1
标签: php web-services rest curl http-status-code-100