简体   繁体   English

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

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

Morning all大家早

Basically, I am unable to make successful cURL requests to internal and external servers from my Windows 7 development PC because of an issue involving a proxy server.基本上,由于涉及代理服务器的问题,我无法从我的 Windows 7 开发 PC 向内部和外部服务器发出成功的 cURL 请求。 I'm running cURL 7.21.2 thru PHP 5.3.6 on Apache 2.4.我在 Apache 2.4 上通过 PHP 5.3.6 运行 cURL 7.21.2。

Here's a most basic request that fails:这是一个最基本的请求失败:

<?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}";

The following (complete) response is received.收到以下(完整)响应。

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.

The log file generated by cURL contains the following. 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

Explicitly stating the proxy and user credentials, as in the following, makes no difference: the response is always the same.如下所示,明确说明代理和用户凭据没有任何区别:响应始终相同。

<?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}";

I was surprised to see an absolute URL in the request line ('GET ...'), but I think that's fine when dealing with proxy servers - according to the HTTP spec.我很惊讶在请求行中看到一个绝对 URL ('GET ...'),但我认为在处理代理服务器时这很好 - 根据 HTTP 规范。

I've tried all sorts of combinations of options - including sending a user-agent, following this and that, etc, etc - having been through Stack Overflow questions, and other sites, but all requests end in the same response.我已经尝试了各种选项组合 - 包括发送用户代理,关注这个和那个等等 - 已经通过 Stack Overflow 问题和其他网站,但所有请求都以相同的响应结束。

The same problem occurs if I run the script on the command line, so it can't be an Apache issue, right?如果我在命令行上运行脚本也会出现同样的问题,所以它不可能是 Apache 问题,对吧?

If I make a request using cURL from a Linux box on the same network, I don't experience a problem.如果我在同一网络上的Linux 机器上使用 cURL 发出请求,我不会遇到问题。

It's the "Bad Request" thing that's puzzling me: what on earth is wrong with my request?让我困惑的是“错误的请求”:我的请求到底有什么问题? Do you have any idea why I may be experiencing this problem?你知道为什么我会遇到这个问题吗? A Windows thing? Windows 的东西? A bug in the version of PHP/cURL I'm using?我使用的 PHP/cURL 版本有问题吗?

Any help very gratefully received.非常感谢收到任何帮助。 Many thanks.非常感谢。

You might be looking at an issue between cURL (different versions between Windows and Linux) and your IronPort version.您可能正在查看 cURL(Windows 和 Linux 之间的不同版本)和 IronPort 版本之间的问题。 In IronPort documentation:在 IronPort 文档中:

Fixed: Web Proxy uses the Proxy-Connection header instead of the Connection header, causing problems with some user agents修复:Web Proxy 使用 Proxy-Connection 标头而不是 Connection 标头,导致某些用户代理出现问题

Previously, the Web Proxy used the Proxy-Connection header instead of the Connection header when communicating with user agents with explicit forward requests.以前,当与具有显式转发请求的用户代理通信时,Web 代理使用 Proxy-Connection 标头而不是 Connection 标头。 Because of this, some user agents, such as Real Player, did not work as expected.因此,某些用户代理(例如 Real Player)无法按预期工作。 This no longer occurs.这不再发生。 Now, the Web Proxy replies to the client using the Connection header in addition to the Proxy-Connection header.现在,除了 Proxy-Connection 标头之外,Web 代理还使用 Connection 标头回复客户端。 [Defect ID: 46515] 【缺陷编号:46515】

Try removing the Proxy-Connection (or add a Connection ) header and see whether this solves the problem.尝试删除Proxy-Connection (或添加Connection )标头,看看这是否能解决问题。

Also, you might want to compare the cURL logs between Windows and Linux hosts.此外,您可能想要比较 Windows 和 Linux 主机之间的 cURL 日志。

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

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