繁体   English   中英

在 Windows 上发出 PHP cURL 请求会产生来自代理的“400 Bad Request”

[英]Making PHP cURL request on Windows yields “400 Bad Request” from proxy

大家早

基本上,由于涉及代理服务器的问题,我无法从我的 Windows 7 开发 PC 向内部和外部服务器发出成功的 cURL 请求。 我在 Apache 2.4 上通过 PHP 5.3.6 运行 cURL 7.21.2。

这是一个最基本的请求失败:

<?php

$curl = curl_init('http://www.google.com');

$log_file = fopen(sys_get_temp_dir() . 'curl.log', 'w');

curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_VERBOSE => TRUE,
    CURLOPT_HEADER => TRUE,
    CURLOPT_STDERR => $log_file,
));

$response = curl_exec($curl);

@fclose($log_file);

print "<pre>{$response}";

收到以下(完整)响应。

HTTP/1.1 400 Bad Request
Date: Thu, 06 Sep 2012 17:12:58 GMT
Content-Length: 171
Content-Type: text/html
Server: IronPort httpd/1.1

Error response

Error code 400.

Message: Bad Request.

Reason: None.

cURL 生成的日志文件包含以下内容。

* About to connect() to proxy usushproxy01.unistudios.com port 7070 (#0)
*   Trying 216.178.96.20... * connected
* Connected to usushproxy01.unistudios.com (216.178.96.20) port 7070 (#0)
> GET http://www.google.com HTTP/1.1
Host: www.google.com
Accept: */*
Proxy-Connection: Keep-Alive

< HTTP/1.1 400 Bad Request
< Date: Thu, 06 Sep 2012 17:12:58 GMT
< Content-Length: 171
< Content-Type: text/html
< Server: IronPort httpd/1.1
< 
* Connection #0 to host usushproxy01.unistudios.com left intact

如下所示,明确说明代理和用户凭据没有任何区别:响应始终相同。

<?php

$curl = curl_init('http://www.google.com');

$log_file = fopen(sys_get_temp_dir() . 'curl.log', 'w');

curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_VERBOSE => TRUE,
    CURLOPT_HEADER => TRUE,
    CURLOPT_STDERR => $log_file,
    CURLOPT_PROXY => 'http://usushproxy01.unistudios.com:7070',
    CURLOPT_PROXYUSERPWD => '<username>:<password>',
));

$response = curl_exec($curl);

@fclose($log_file);

print "<pre>{$response}";

我很惊讶在请求行中看到一个绝对 URL ('GET ...'),但我认为在处理代理服务器时这很好 - 根据 HTTP 规范。

我已经尝试了各种选项组合 - 包括发送用户代理,关注这个和那个等等 - 已经通过 Stack Overflow 问题和其他网站,但所有请求都以相同的响应结束。

如果我在命令行上运行脚本也会出现同样的问题,所以它不可能是 Apache 问题,对吧?

如果我在同一网络上的Linux 机器上使用 cURL 发出请求,我不会遇到问题。

让我困惑的是“错误的请求”:我的请求到底有什么问题? 你知道为什么我会遇到这个问题吗? Windows 的东西? 我使用的 PHP/cURL 版本有问题吗?

非常感谢收到任何帮助。 非常感谢。

您可能正在查看 cURL(Windows 和 Linux 之间的不同版本)和 IronPort 版本之间的问题。 在 IronPort 文档中:

修复:Web Proxy 使用 Proxy-Connection 标头而不是 Connection 标头,导致某些用户代理出现问题

以前,当与具有显式转发请求的用户代理通信时,Web 代理使用 Proxy-Connection 标头而不是 Connection 标头。 因此,某些用户代理(例如 Real Player)无法按预期工作。 这不再发生。 现在,除了 Proxy-Connection 标头之外,Web 代理还使用 Connection 标头回复客户端。 【缺陷编号:46515】

尝试删除Proxy-Connection (或添加Connection )标头,看看这是否能解决问题。

此外,您可能想要比较 Windows 和 Linux 主机之间的 cURL 日志。

暂无
暂无

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

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