不能抓取远程内容,但可以在浏览器中查看


cant grab remote content but can view in browser

我真的被这个问题弄糊涂了。我做了一个网址缩短网站,并尝试了它的API调用。

所以我把这个脚本放到了我的主机目录

<?php 
$homepage = file_get_contents("http://klik.pw/api?api=BEHbDA19Z3e1&url=http://google.com");
echo $homepage; 
?>

我得到了空白页。我尝试了这个脚本与我的3个不同的服务器和所有产生空白页。

然后我取出http://klik.pw/api?api=BEHbDA19Z3e1&url=http://google.com,在浏览器中打开它,瞧,它显示了我想用php代码抓取的输出…

谁知道为什么我的PHP不能抓取结果,但我的浏览器可以查看它?

然后我尝试在php脚本

中删除url中的部分参数
<?php
$homepage = file_get_contents("http://klik.pw/api?api=BEHbDA19Z3e1");
echo $homepage;
?>

,它显示了一些错误信息,要求放置"url"参数。为什么会这样??????

可以使用CURL函数

function file_get_contents_curl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 500);
    $return = curl_exec($ch);
    curl_close($ch);
    return $return;
}
echo file_get_contents_curl("http://klik.pw/api?api=BEHbDA19Z3e1&url=http://google.com");
相关文章: