PHP:启用了CURL但没有任何效果 [英] PHP: CURL is enabled but has no effect
问题描述
我正在尝试使用 PHP
中的 cURL
从外部网站获取数据,但是不成功。
我已经检查出 CURL
在 phpinfo()
中启用。它显示 cURL
已启用
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
但是,我的代码不是
<?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.
我没有使用WAMP或XAMPP服务器。上面的代码直接在服务器上运行。
我不知道出了什么问题。
I'm not using WAMP or XAMPP server. The above code runs directly on the server. I've no idea what's going wrong.
推荐答案
您的代码很完美,我已经对它进行了自我测试
Your code is perfect, I have tested it on my own server (data center in Texas) and it worked fine.
我的猜测是您的服务器IP被禁止。尝试获取另一个URL,然后查看它是否适合您。如果这样做,那么您将被禁止,如果没有,则可能是服务器中的防火墙配置问题。
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.
这篇关于PHP:启用了CURL但没有任何效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!