[英]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.