我有一段代码(让我们命名为Code A)在框架中工作,我想让它在另一个框架中工作.工作代码使用CURL成功发出POST请求(使用CURLOPT_VERBOSE的请求):
* Connected to android.clients.google.com (216.58.209.238) port 443 (#0)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
* CAfile: /Applications/MAMP/Library/OpenSSL/cert.pem
CApath: none
* SSL connection using TLSv1.2 / ECDHE-ECDSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.google.com
* start date: Jan 18 19:17:59 2017 GMT
* expire date: Apr 12 18:51:00 2017 GMT
* subjectAltName: host "android.clients.google.com" matched cert's "android.clients.google.com"
* issuer: C=US; O=Google Inc; CN=Google Internet Authority G2
* SSL certificate verify ok.
> POST /auth HTTP/1.1
Host: android.clients.google.com
Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 97
Run Code Online (Sandbox Code Playgroud)
这里的http框架(yii2/httpclient,具体)有太多的依赖关系将它带到另一个项目,所以我试图在这个低级别重新创建它(让我们把它命名为代码B):
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'post-data-here');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'https://android.clients.google.com/auth');
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/x-www-form-urlencoded; charset=UTF-8"]); // just because I'm desperate
curl_setopt($ch, CURLOPT_VERBOSE, true);
$content = curl_exec($ch);
Run Code Online (Sandbox Code Playgroud)
我期望得到相同的结果,但这就是我得到的:
* Connected to android.clients.google.com (216.58.209.238) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
* Server certificate: *.google.com
* Server certificate: Google Internet Authority G2
* Server certificate: GeoTrust Global CA
> POST /auth HTTP/1.1
Host: android.clients.google.com
Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 97
* upload completely sent off: 97 out of 97 bytes
< HTTP/1.1 200 OK
< Content-Type: text/plain; charset=utf-8
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: Mon, 01 Jan 1990 00:00:00 GMT
< Date: Fri, 27 Jan 2017 12:07:12 GMT
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< Server: GSE
< Alt-Svc: clear
< Accept-Ranges: none
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
<HTML>
<HEAD>
<TITLE>HTTP Version Not Supported</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>HTTP Version Not Supported</H1>
<H2>Error 505</H2>
</BODY>
</HTML>
Run Code Online (Sandbox Code Playgroud)
而不是有效的响应,我得到"错误505:不支持HTTP版本".我看到的唯一区别是工作代码试图"ALPN,提供http/1.1",而后一个代码不这样做.之后的证书部分,但它从未在代码A中提及过,所以我不确定它是如何提供它的.
两个版本的代码都在同一台服务器上运行,相同版本的PHP(5.6)和CURL(7.51.0).详细日志的差异在发送任何数据之前就开始了,所以我猜这不是关于任何数据或标题设置错误.
到目前为止我尝试了什么(影响很小或没有影响):
我试图尽可能深地学习工作代码,但似乎它对简单的HTTP POST没有任何作用.我跟踪了它生成的每个curl_setopt,似乎我的代码中只使用了这些curl_setopt,没有什么额外的.尽管如此,它仍然有效,而我的代码却没有.
我尝试使用命令行进行相同的操作:
$ curl https://android.clients.google.com/auth -v --http1.1 -X POST --no-alpn --no-npn --data "copypasted-post-data-from-code-B-and-yes-its-urlencoded"
Run Code Online (Sandbox Code Playgroud)
得到了正确的结果:
* Trying 216.58.209.238...
* TCP_NODELAY set
* Connected to android.clients.google.com (216.58.209.238) port 443 (#0)
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
* CAfile: /Applications/MAMP/Library/OpenSSL/cert.pem
CApath: none
...
> POST /auth HTTP/1.1
> Host: android.clients.google.com
> User-Agent: curl/7.51.0
> Accept: */*
> Content-Length: 97
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 97 out of 97 bytes
< HTTP/1.1 200 OK
< Content-Type: text/plain; charset=utf-8
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: Mon, 01 Jan 1990 00:00:00 GMT
< Date: Fri, 27 Jan 2017 13:22:27 GMT
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< Server: GSE
< Alt-Svc: clear
< Accept-Ranges: none
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
<
SID=BAD_COOKIE
LSID=BAD_COOKIE
Auth=here-s-the-data-i-need
Run Code Online (Sandbox Code Playgroud)
Dan*_*erg 10
Error 505: HTTP Version Not Supported不是curl/libcurl返回的错误字符串,听起来就像是从正在与之通信的服务器收到的内容.如果您向我们展示包括标题的完整HTTP响应,我们可能已经看到了.
所以,你所有玩弄不同的卷曲选项都没有收获,因为卷曲每次都很好.您还可以使用curl命令行工具验证您尝试开始工作的主机:
curl https://android.clients.google.com/auth -v --http1.1 -X POST --no-alpn --no-npn
Run Code Online (Sandbox Code Playgroud)
此命令行显示TLS和HTTP"层"都很好.
当他们传入错误的数据(不是url编码)时,这里的另一个问题得到了类似的错误.也许你有类似的东西,因为你切换到一个你错过它的新框架?
好吧,我明白了。
实际上,在代码 B 中的那段代码之前,使用另一个库(确切地说是 Httpful)执行了另一个请求,并且该请求似乎以某种方式搞砸了事情。我不知道某些事情会影响使用干净的curl_init()执行的请求。
无论如何,当我将相关请求之前的所有 Httpful 调用替换为低级别调用时,一切正常。
| 归档时间: |
|
| 查看次数: |
16514 次 |
| 最近记录: |