简体   繁体   English

PHP cURL请求失败,但可与Bash的Postman / curl一起使用

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

I encountered a very strange situation. 我遇到了一个非常奇怪的情况。 Making a request in Postman to a web service works correctly (x-www-form-urlencoded) with a few parameters. 在Postman中向Web服务发出请求可以正常工作(x-www-form-urlencoded),其中包含一些参数。

However when I make this request using PHP cURL the request fails with a response similar to this: 但是,当我使用PHP cURL发出此请求时,该请求失败,并返回类似于以下内容的响应:

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

The web service appears to be written using PHP, the endpoint is such as http://foo.com/service.php . 该Web服务似乎是使用PHP编写的,终结点如http://foo.com/service.php

Postman will work 100% of the time, a cURL request made from Bash works 100% of the time. 邮递员将有100%的时间工作,而Bash发出的cURL请求则有100%的时间工作。

For me it is extremely strange that it is trying to resolve an internal IP address and that it only fails when using the php cURL library. 对我来说,它试图解析一个内部IP地址并且仅在使用php cURL库时会失败是非常奇怪的。

Hopefully someone can shed some light on this or has experienced this before. 希望有人可以对此有所了解或曾经经历过。 I have tried pretty much every curl option imaginable to get this to work, but no luck. 我已经尝试了几乎所有可以想到的curl选项,但都没有运气。 Any help would be appreciated. 任何帮助,将不胜感激。

Code: 码:

$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;

Verbose output: 详细输出:

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!

The curl command is OK, but the code on the remote server tries to download some file using fopen . curl命令是可以的,但是远程服务器上的代码尝试使用fopen下载一些文件。 That fails and the error message is returned as part of the response to your calling code. 这将失败,并且错误消息将作为对您的调用代码的响应的一部分返回。 It looks like the remote code tries to open a URL based on some input that it receives from the client; 看起来远程代码试图根据从客户端接收到的一些输入来打开URL。 that input from your curl command differs from the input from Postman. curl命令的输入与Postman的输入不同。 To find out exactly what element in the HTTP headers or POST content is the culprit you need to take a peek in to the remote server code ( service.php ) or do trial-and-error by comparing the exact contents of the Postman request with that of the CURL request. 要找出HTTP标头或POST内容中到底是什么元素,您需要窥视远程服务器代码( service.php )或通过将Postman请求的确切内容与CURL请求的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM