【发布时间】:2015-05-29 22:46:57
【问题描述】:
当通过 PHP 发出 curl 请求时,curl 得到“无法连接到主机”。当我转储 curl 信息时,primary_ip 不是所请求服务器的 ip。
$soapURL ="www.google.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $soapURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$header[] = "SOAPAction: ". "";
$header[] = "MIME-Version: 1.0";
$header[] = "Content-type: text/xml; charset=utf-8";
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
echo curl_getinfo($ch) . '<br/>';
echo curl_errno($ch) . '<br/>';
echo curl_error($ch) . '<br/>';
$info = curl_getinfo($ch);
var_dump($info);
这在我的测试服务器上运行良好,但在生产环境中却不行。在生产服务器上,这是我得到的响应:
数组
7
无法连接到主机 array(23) { ["url"]=> string(21) "http://www.google.com" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size" ]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int( 0) ["total_time"]=> float(0) ["namelookup_time"]=> float(2.7E-5) ["connect_time"]=> float(0) ["pretransfer_time"]=> float(0) [ "size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["redirect_url"]=> string(0) "" ["primary_ip"]=> 字符串(10) "10.1.99.87" ["certinfo"]=> 数组(0) { } }
primary_ip => 10.1.99.87 是我的本地机器(不运行此 PHP 代码)。似乎 curl 正在尝试连接到我的本地计算机,而不是我给它的 url。我在另一台计算机上尝试过,IP 仍然作为我的机器,它没有更改为新机器的 IP。
当我使用测试服务器时,我得到:
数组
0
array(23) { ["url"]=> string(21) "http://www.google.com" ["content_type"]=> string(24) "text/html; charset=UTF- 8" ["http_code"]=> int(405) ["header_size"]=> int(260) ["request_size"]=> int(948) ["filetime"]=> int(-1) ["ssl_verify_result "]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.120098) ["namelookup_time"]=> float(0.029716) ["connect_time"]=> float( 0.073289) ["pretransfer_time"]=> float(0.073292) ["size_upload"]=> float(815) ["size_download"]=> float(1453) ["speed_download"]=> float(12098) ["speed_upload" ]=> float(6786) ["download_content_length"]=> float(1453) ["upload_content_length"]=> float(0) ["starttransfer_time"]=> float(0.119125) ["redirect_time"]=> float(0 ) ["redirect_url"]=> string(0) "" ["primary_ip"]=> string(14) "74.125.198.106" ["certinfo"]=> array(0) { } }
这是应该发生的事情。
曾经为 http_proxy 设置了 Linux 环境变量,该变量设置为我设置为空的机器。从命令行进行 curl 调用也可以正常工作。这只是 php 试图使失败的任何 curl 调用。
【问题讨论】:
-
proxy_http 变量存在于 phpinfo() 中,但未在任何 .ini 文件中设置。我不知道还能去哪里看。我尝试在设置为代理的 ip 上运行 grep 命令,但我的会话在完成之前就过期了