PHP:CURL 已启用但无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33993614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:41:47  来源:igfitidea点击:

PHP: CURL is enabled but has no effect

phpcurl

提问by Kasim Rangwala

I'm trying to get data from external website using cURLin PHPbut, somehow it's not working.

I've checked out that CURLenable in phpinfo(). It shows cURLis enabled cURL is enabled

我正在尝试使用cURLin从外部网站获取数据,PHP但不知何故它不起作用。

我已经CURLphpinfo(). 显示cURL已启用卷曲已启用

But, my code is not working.

但是,我的代码不起作用。

<?php
if (! function_exists ( 'curl_version' )) {
    exit ( "Enable cURL in PHP" );
}

$ch = curl_init ();
$timeout = 0; // 100; // set to zero for no timeout
$myHITurl = "http://www.google.com";
curl_setopt ( $ch, CURLOPT_URL, $myHITurl );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
$file_contents = curl_exec ( $ch );
if (curl_errno ( $ch )) {
    echo curl_error ( $ch );
    curl_close ( $ch );
    exit ();
}
curl_close ( $ch );

// dump output of api if you want during test
echo "$file_contents";
?>


It goes timeout.
Connection Timeout


它超时。
Connection Timeout

I'm not using WAMP or XAMPP server. The above code runs directly on the server. I've no idea what's going wrong.

我没有使用 WAMP 或 XAMPP 服务器。上面的代码直接运行在服务器上。我不知道出了什么问题。

回答by Aviram

Your code is perfect, I have tested it on my own server (data center in Texas) and it worked fine.

您的代码很完美,我已经在我自己的服务器(德克萨斯州的数据中心)上对其进行了测试,并且运行良好。

My guess is that your server IP is banned. Try to fetch a different URL, and see if it works for you. If it does then you are banned, if it doesn't then it might be a firewall configuration issue in your server.

我的猜测是您的服务器 IP 被禁止。尝试获取不同的 URL,看看它是否适合您。如果是,那么您将被禁止,如果不是,则可能是您的服务器中的防火墙配置问题。

回答by xenonsmart dev

disable SELinux if you are on Centos or Fedora or any Redhat Distro

如果您使用的是 Centos、Fedora 或任何 Redhat 发行版,请禁用 SELinux

nano /etc/selinux/config

Change

改变

SELINUX=enforcing

to

SELINUX=disabled