搜尋
首頁php教程php手册php的curl获取有301/302跳转文件出错问题解决

在使用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,问题就解决了。



陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能