Php 问题:相同的请求,不同的响应

Php 问题:相同的请求,不同的响应,php,http,curl,guzzle,Php,Http,Curl,Guzzle,这里有个问题 我需要向API发送一个请求,并从响应中获取字符串。 在这种情况下,我想得到一个字符串“订单已经取消” 首先,我在邮递员那里查过了。在那里很好用。 之后,我编写了一个php代码,使用GuzzleHttp包发出请求。 但在那里,我第一次面对这个问题 回复的标题是“Content-Length:4”,看起来主体是空的。 这就是我试图调用$response->getBody()时得到NULL的原因 然后,我在调试模式下运行GuzzleHttp以查看原始http请求/响应,并与邮递员进行比较

这里有个问题

我需要向API发送一个请求,并从响应中获取字符串。 在这种情况下,我想得到一个字符串“订单已经取消”

首先,我在邮递员那里查过了。在那里很好用。 之后,我编写了一个php代码,使用GuzzleHttp包发出请求。 但在那里,我第一次面对这个问题

回复的标题是“Content-Length:4”,看起来主体是空的。 这就是我试图调用$response->getBody()时得到NULL的原因

然后,我在调试模式下运行GuzzleHttp以查看原始http请求/响应,并与邮递员进行比较

一般来说,它们之间没有区别,除了一些不重要的标题,如“用户代理”、“接受编码”和“邮递员令牌”

我认为问题出在GuzzleHttp上,我用干净的内置curl函数重写了代码

$basic_auth_base64 = base64_encode("{$this->_http_auth_login}:{$this->_http_auth_pass}");

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "{$this->_base_url}{$uri}?" . http_build_query($params),
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Basic $basic_auth_base64",
    "Cache-Control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);        
curl_close($curl);
然后我也遇到了同样的问题!来自API的错误空响应,其标题为:

Content-Type: application/json; charset=UTF-8
Content-Length: 4
但我需要得到这样的回应:

Content-Type: text/html
Content-Length: 23
我甚至用curl检查了一个bash的请求,结果很好

curl -X GET \
  'http://website-server.com/api/order/delete/?OrderId=6693' \
  -H 'Accept: */*' \
  -H 'Accept-Encoding: deflate, gzip' \
  -H 'Authorization: Basic base64LoginAndPass' \
  -H 'Cache-Control: no-cache' \
  -H 'User-Agent:' -v
我从api得到了很好的响应,所以问题只在于我的php代码和环境

这是curl的原木。从bash curl和php curl:

bash curl:

*   Trying 82.236.10.100...
* TCP_NODELAY set
* Connected to website-server.com (82.236.10.100) port 80 (#0)
> GET /api/order/delete/?OrderId=6693 HTTP/1.1
> Host: website-server.com
> Accept: */*
> Accept-Encoding: deflate, gzip
> Authorization: Basic base64LoginPassword
> Cache-Control: no-cache
> 
< HTTP/1.1 200 OK
< Server: nginx-reuseport/1.13.4
< Date: Fri, 13 Apr 2018 17:04:27 GMT
< Content-Type: text/html
< Content-Length: 23
< Connection: keep-alive
< Keep-Alive: timeout=30
< X-Powered-By: PHP/5.6.30
< 
* Connection #0 to host website-server.com left intact
order already cancelled    
*   Trying 82.236.10.100...
* TCP_NODELAY set
* Connected to website-server.com (82.236.10.100) port 80 (#0)
> GET /api/order/delete/?OrderID=6693 HTTP/1.1
Host: website-server.com
Accept: */*
Accept-Encoding: deflate, gzip
Authorization: Basic base64LoginPassword
Cache-Control: no-cache

< HTTP/1.1 200 OK
< Server: nginx-reuseport/1.13.4
< Date: Fri, 13 Apr 2018 16:44:59 GMT
< Content-Type: application/json; charset=UTF-8
< Content-Length: 4
< Connection: keep-alive
< Keep-Alive: timeout=30
< X-Powered-By: PHP/5.6.30
< 
* Curl_http_done: called premature == 0
* Connection #0 to host website-server.com left intact
*正在尝试82.236.10.100。。。
*TCP_节点集
*已连接到website-server.com(82.236.10.100)端口80(#0)
>GET/api/order/delete/?OrderId=6693 HTTP/1.1
>主持人:website-server.com
>接受:*/*
>接受编码:deflate,gzip
>授权:基本base64LoginPassword
>缓存控制:没有缓存
> 
php curl:

*   Trying 82.236.10.100...
* TCP_NODELAY set
* Connected to website-server.com (82.236.10.100) port 80 (#0)
> GET /api/order/delete/?OrderId=6693 HTTP/1.1
> Host: website-server.com
> Accept: */*
> Accept-Encoding: deflate, gzip
> Authorization: Basic base64LoginPassword
> Cache-Control: no-cache
> 
< HTTP/1.1 200 OK
< Server: nginx-reuseport/1.13.4
< Date: Fri, 13 Apr 2018 17:04:27 GMT
< Content-Type: text/html
< Content-Length: 23
< Connection: keep-alive
< Keep-Alive: timeout=30
< X-Powered-By: PHP/5.6.30
< 
* Connection #0 to host website-server.com left intact
order already cancelled    
*   Trying 82.236.10.100...
* TCP_NODELAY set
* Connected to website-server.com (82.236.10.100) port 80 (#0)
> GET /api/order/delete/?OrderID=6693 HTTP/1.1
Host: website-server.com
Accept: */*
Accept-Encoding: deflate, gzip
Authorization: Basic base64LoginPassword
Cache-Control: no-cache

< HTTP/1.1 200 OK
< Server: nginx-reuseport/1.13.4
< Date: Fri, 13 Apr 2018 16:44:59 GMT
< Content-Type: application/json; charset=UTF-8
< Content-Length: 4
< Connection: keep-alive
< Keep-Alive: timeout=30
< X-Powered-By: PHP/5.6.30
< 
* Curl_http_done: called premature == 0
* Connection #0 to host website-server.com left intact
*正在尝试82.236.10.100。。。
*TCP_节点集
*已连接到website-server.com(82.236.10.100)端口80(#0)
>GET/api/order/delete/?OrderID=6693 HTTP/1.1
主持人:website-server.com
接受:*/*
接受编码:deflate,gzip
授权:基本base64LoginPassword
缓存控制:没有缓存
正如你所看到的,这里有一个完全相同的请求!但是不同的反应! 主要区别在于标题“Content-Type:application/json;charset=UTF-8”和响应的主体


我该怎么办?

我不知道可能的原因,但我会尝试推荐如何调试

您看到相同的请求会产生不同的结果。现在让我们做一个合理的假设:如果请求确实完全相同,我们可以非常确定服务器将返回完全相同的结果

到目前为止,您只相信来自PHP和CURL的查询是相同的。我不确定CURL和PHP CURL是如何编写日志的(也就是说,我不确定如何以及是否可以将它们配置为记录所有的头)


值得注意的是,查看CURL和PHP CURL在请求中发送的头信息会很有趣。我会使用tcpdump或Wireshark记录网络流量。我确信这些请求结果会不一样。

奇怪。您有权访问服务器吗?试着从那一边调试。可能是第一次呼叫服务器的应答与连续呼叫不同。(即,在对象被删除后)顺便说一句,请求删除对象并不是最好的主意(安全方面)哦,我当然同意你的观点,即请求删除对象并不是一个好主意。但这不是我的API和服务器,这是一个问题。如果该对象在过去已被删除,那么它的答案会有所不同,这一猜测又如何呢?你能排除这一点吗?主要的魔法是我只能从php代码中得到“奇怪的错误响应”。当我使用邮递员、curl或其他软件时,一切都正常。它根本不取决于请求的数量。在这种情况下,我只需要从服务器上得到一条消息,比如“订单已经取消”,我昨天花了整整一天的时间来处理这个问题,今天花了4个小时:D我会喝醉的谢谢你的建议。给你。我认为这是相同的更新:实际上不是。GET param中存在差异。OrderID而不是OrderID