PHP cURL 请求失败,但适用于 Bash 的 Postman/curl


PHP cURL request fails, but works with Postman/curl from Bash

我遇到了一个非常奇怪的情况。在Postman中向Web服务发出请求可以正常工作(x-www-form-urlencoded),只需几个参数。

但是,当我使用 PHP cURL 发出此请求时,请求失败,响应类似于以下内容:

Failed to Connect, fopen(http://10.0.0.8:8080/posts?foo=bar&bar=baz): failed to open stream: HTTP request failed! 

Web 服务似乎是使用 PHP 编写的,端点如 http://foo.com/service.php。

邮递员将 100% 的时间工作,从 Bash 发出的 cURL 请求将 100% 工作。

对我来说,它试图解析内部 IP 地址并且仅在使用 php cURL 库时失败是非常奇怪的。

希望有人能对此有所了解或以前经历过这一点。我已经尝试了几乎所有可以想象的卷曲选项来让它工作,但没有运气。任何帮助将不胜感激。

法典:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::SERVICE_ENDPOINT);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $paramString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
var_dump(curl_exec($ch));
die;

详细输出:

HTTP/1.1 200 OK
Date: Tue, 06 Jan 2015 09:27:20 GMT
Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Last-Modified: Tue, 06 Jan 2015 09:27:20 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Vary: Accept-Encoding
Content-Length: 324
Content-Type: text/html
Failed to Connect, fopen(http://10.0.0.8:8080/posts?foo=bar&bar=baz): failed to open stream: HTTP request failed!

curl 命令正常,但远程服务器上的代码尝试使用 fopen 下载某些文件。此操作将失败,错误消息将作为对调用代码的响应的一部分返回。看起来远程代码试图根据它从客户端收到的一些输入打开 URL;来自 curl 命令的输入与来自 Postman 的输入不同。要准确找出HTTP标头或POST内容中的哪个元素是罪魁祸首,您需要查看远程服务器代码(service.php),或者通过将Postman请求的确切内容与CURL请求的确切内容进行比较来进行试错。