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 服务器。上面的代码直接在服务器上运行。
我不知道出了什么问题。
你的代码是完美的,我已经在我自己的服务器(德克萨斯州的数据中心)上测试了它,并且运行良好。
我的猜测是你的服务器IP被禁止了。尝试获取不同的 URL,看看它是否适合您。如果是,那么您将被禁止,如果不是,则可能是您服务器中的防火墙配置问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)