在Dreamhost的主机上如何抓取远程文件

Dreamhost的主机上,php的allow_url_fopen选项是关闭的,换言之,没法直接使用file_get_contents来获取远程web页面。不过dreamhost也有提供解决办法,那就是curl!!没有棒棒糖,给个甜甜圈也不错,是吧?

下面是file_get_contentscurl,同样功用的写法

使用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$

作者: 发表于June 6, 2007 at 7:17 am

版权信息: 可以任意转载, 转载时请务必以超链接形式标明文章原始出处作者信息及此声明

Tags: ,

3 条评论 »

  1. Mgccl 于 2007-06-06 @ 07:55:37 留言

    n好东西…搜藏啦…

  2. 姜运涛 于 2007-06-07 @ 11:33:30 留言

    还是习惯 fsockopen()

  3. volcano 于 2007-06-07 @ 11:43:10 留言

    在allow_url_fopen关闭的情况下,不能用fscokeopen()打开远程文件吧?

RSS 为此帖反馈评论 · 反向跟踪 网站

留条评论