我使用cUrl(php)以xml格式将商品代码发送到web服务。我在localhost中得到了正确的响应,但是当它显示在服务器上时
cURL错误(7):无法连接到主机
下面是我的代码:
function xml_post($post_xml, $url)
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_xml);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
// curl_setopt($ch, CURLOPT_PORT, $port);
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
if ($curl_errno > 0) {
echo "cURL Error ($curl_errno): $curl_error\n";
} else {
echo "Data received\n";
}
curl_close($ch);
echo $data;
}
我将商品代码发送到理货台,并从中获取详细信息。我尝试使用php 4+和php5+两个版本,都没有找到任何解决方案。
发布于 2012-03-29 18:06:28
CURL错误代码7 (CURLE_COULDNT_CONNECT)
是非常明确的。意思是Failed to connect() to host or proxy.
以下代码可以在任何系统上运行:
$ch = curl_init("http://google.com"); // initialize curl handle
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
print($data);
如果你看不到google页面,那么..your URL is wrong
,或者您遇到了一些firewall
或restriction
问题。
发布于 2013-08-07 15:49:41
当curl请求被某些防火墙或类似的东西阻止时,会导致“curl错误7无法连接到权限被拒绝”错误。
当curl请求不是使用标准端口时,您将面临这个问题。
例如,如果您确实卷曲到端口1234上的某个URL,您将面临这个问题,其中端口为80的URL将很容易为您提供结果。
最常见的情况是,在CentOS和任何其他带有“SElinux”的操作系统上都会出现此错误。
需要禁用‘SElinux’或将‘permissive’更改为permissive
看看这个
http://www.akashif.co.uk/php/curl-error-7-failed-to-connect-to-permission-denied
希望这能有所帮助
发布于 2020-04-29 03:00:34
如果您已经尝试了所有方法,但都失败了,请尝试以下命令:
setsebool -P httpd_can_network_connect on
https://stackoverflow.com/questions/9922562
复制相似问题