在Dreamhost的主机上如何抓取远程文件
在Dreamhost的主机上,php的allow_url_fopen选项是关闭的,换言之,没法直接使用file_get_contents来获取远程web页面。不过dreamhost也有提供解决办法,那就是curl!!没有棒棒糖,给个甜甜圈也不错,是吧?
下面是file_get_contents和curl,同样功用的写法
使用file_get_contents的写法
<?php
$file_contents = file_get_contents('http://www.ooso.net/');
// display file
echo $file_contents;
?>
换成curl的写法
<?php
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://www.ooso.net');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); //直接获取抓取的内容
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
// display file
echo $file_contents;
?>
把上面这段封装成一个小function,就能很便利的获取远程页面了。
curl参考
http://wiki.dreamhost.com/index.php/CURL
dreamhost优惠码
WAHAHA – 购买一年空间可以使用最大优惠97$
作者: Volcano 发表于June 6, 2007 at 7:17 am
Mgccl 于 2007-06-06 @ 07:55:37 留言 :
n好东西…搜藏啦…
姜运涛 于 2007-06-07 @ 11:33:30 留言 :
还是习惯 fsockopen()
volcano 于 2007-06-07 @ 11:43:10 留言 :
在allow_url_fopen关闭的情况下,不能用fscokeopen()打开远程文件吧?