当前位置: 首页 > 图文教程 > 网络编程 > PHP > php的curl获取有301/302跳转文件出错问题解决

PHP
接触 solaris : 安装 apache + php + mysql
图片上传到数据库,该怎么做(包括读取)
初探 PHP5
定制php4的session功能
php写的发送附件的程序(一)
php写的发送附件的程序(二)
Cookie的作用
MySQL数据库备份
基于PHP的聊天室(一)
基于PHP的聊天室(二)
基于PHP的聊天室(三)
管理小型的邮件列表
MySQL用户管理
PHP4与MySQL数据库操作函数详解(一)
PHP4与MySQL数据库操作函数详解(二)
PHP4与MySQL数据库操作函数详解(三)
PHP4与MySQL数据库操作函数详解(四)
PHP4与MySQL数据库操作函数详解(五)
PHP4与MySQL数据库操作函数详解(六)
PHP新手上路(一)

PHP 中的 php的curl获取有301/302跳转文件出错问题解决


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-30   浏览: 262 ::
收藏到网摘: n/a

在使用php的curl获取远程文件,代码如下:


<?
$ghurl = isset($_GET['id']) ? $_GET['id']:'http://www.baidu.com/';
// php 获取
function getContents($url){
$header = array("Referer: http://www.baidu.com/");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);  //是否抓取跳转后的页面
ob_start();
curl_exec($ch);
$contents = ob_get_contents();
ob_end_clean();
curl_close($ch);

return $contents;
}

$contents = getContents($ghurl);
echo $contents;
?>

一般来说在win2003+iis下如,把php_curl.dll配置好就没问题了。

但笔者在 linux+apahe2.0+php5.2.12+directadmin,(一般国外主机商都是用这配置)如果获取的网址有301/302跳转,会报错:

curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in ***

关于这个问题,google,百度一下,都是在大篇长长的english,不精通linux的还真头痛。

解决办法其实很简单:登陆你的 directadmin

找到 ->>"PHP SafeMode Configuration" -->>看下图

 

把默认的 Default SafeDefault Open BaseDir OFF,问题就解决了。