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
PHP: CURL is enabled but has no effect
提问by Kasim Rangwala
I'm trying to get data from external website using cURL
in PHP
but, somehow it's not working.
I've checked out that CURL
enable in phpinfo()
. It shows cURL
is enabled
我正在尝试使用cURL
in从外部网站获取数据,PHP
但不知何故它不起作用。
我已经CURL
在phpinfo()
. 显示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";
?>
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