简体   繁体   English

PHP cURL 请求 primary_ip 与 cURL url 不同

[英]PHP cURL request primary_ip is different than cURL url

When making a curl request through PHP, curl gets "couldn't connect to host".通过 PHP 发出 curl 请求时,curl 会“无法连接到主机”。 When I dump the curl info the primary_ip is not the ip of the requested server.当我转储 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);

This works fine on my test server but not on production.这在我的测试服务器上运行良好,但不适用于生产。 On the production server this is the response I get:在生产服务器上,这是我得到的响应:

Array大批

7 7

couldn't connect to host 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"]=> string(10) "10.1.99.87" ["certinfo"]=> array(0) { } }无法连接到主机数组(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"]=> string(10) "10.1.99.87" ["certinfo"]=> array(0) { } }

primary_ip => 10.1.99.87 is my local machine (not running this PHP code). primary_ip => 10.1.99.87是我的本地机器(未运行此 PHP 代码)。 It seems like curl is trying to connect to my local machine instead of the url I gave it.似乎 curl 试图连接到我的本地机器而不是我给它的 url。 I tried this from another computer and the IP stayed as my machine, it didn't change to that of the new machine.我在另一台计算机上尝试过,IP 保留为我的机器,它没有更改为新机器的 IP。

When I use the test server I get:当我使用测试服务器时,我得到:

Array大批

0 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) { } } 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) { } }

Which is what is supposed to happen.这是应该发生的事情。

There used to be Linux environment variables set for http_proxy which was set to my machine that I set to be empty.曾经有为 http_proxy 设置的 Linux 环境变量,它被设置为我设置为空的机器。 Also making a curl call from the command line works fine.从命令行进行 curl 调用也可以正常工作。 It's just any curl call that php tries to make that fails.这只是 php 试图使其失败的任何 curl 调用。

Similar Problem with workaround solution 解决方法的类似问题

SUSE specifically SLES 11 has some proxy settings you can access in the directory or via YAST like I did. SUSE 特别是 SLES 11 有一些代理设置,您可以像我一样在目录中或通过 YAST 访问。 Changing these settings and enabling or disabling the proxy would affect calls made via the command line in Linux but did not affect curl calls made via PHP.更改这些设置以及启用或禁用代理会影响在 Linux 中通过命令行进行的调用,但不会影响通过 PHP 进行的 curl 调用。 Restarting Apache also didn't change anything.重新启动 Apache 也没有任何改变。 PHP still had environmental variables set for the http_proxy. PHP 仍然为 http_proxy 设置了环境变量。 Restarting the entire server(init 6) after I removed all the proxy settings via YAST fixed the proxy problem for me.通过 YAST 删除所有代理设置后重新启动整个服务器(init 6)为我解决了代理问题。 Simply disabling the proxy wasn't enough.仅仅禁用代理是不够的。 I had to clear out the proxy setting in yast.我必须清除 yast 中的代理设置。

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

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