关于php curl获取301或302转向的网址问题的解决方法
作者:bea
在使用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, CUR
在使用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 Safe 和 Default Open BaseDir 都 OFF,问题就解决了。
有用 | 无用
代码如下:
<?
$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 Safe 和 Default Open BaseDir 都 OFF,问题就解决了。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- php学习笔记之 函数声明
- php学习之 数组声明
- php学习笔记 数组遍历实现代码
- php学习笔记 [预定义数组(超全局数组)]
- ThinkPHP自动验证失败的解决方法
- PHP下通过exec获得计算机的唯一标识[CPU,网卡 MAC地址]
- 一个PHP缓存类代码(附详细说明)
- php下通过IP获取地理位置的代码(小偷程序)
- php 面试碰到过的问题 在此做下记录
- rephactor 优秀的PHP的重构工具
- php获取post中的json数据的实现方法
- 常用的PHP数据库操作方法(MYSQL版)
- apache+php完美解决301重定向的两种方法
- php错误提示failed to open stream: HTTP request failed!的完美解决方法
- PHP 获取远程网页内容的代码(fopen,curl已测)
- PHP发明人谈MVC和网站设计架构 貌似他不支持php用mvc
- php写的简易聊天室代码
- php结合表单实现一些简单功能的例子
- PHP中对用户身份认证实现两种方法