關(guān)于php curl獲取301或302轉(zhuǎn)向的網(wǎng)址問題的解決方法
更新時間:2011年06月02日 23:07:49 作者:
關(guān)于php curl獲取301或302轉(zhuǎn)向的網(wǎng)址問題的解決方法,其實可以通過修改服務器的配置即可。
在使用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); //是否抓取跳轉(zhuǎn)后的頁面
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,(一般國外主機商都是用這配置)如果獲取的網(wǎng)址有301/302跳轉(zhuǎn),會報錯:
curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in ***
關(guān)于這個問題,google,百度一下,都是在大篇長長的english,不精通linux的還真頭痛。
解決辦法其實很簡單:登陸你的 directadmin
找到 ->>"PHP SafeMode Configuration" -->>看下圖
復制代碼 代碼如下:
<?
$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); //是否抓取跳轉(zhuǎn)后的頁面
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,(一般國外主機商都是用這配置)如果獲取的網(wǎng)址有301/302跳轉(zhuǎn),會報錯:
curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in ***
關(guān)于這個問題,google,百度一下,都是在大篇長長的english,不精通linux的還真頭痛。
解決辦法其實很簡單:登陸你的 directadmin
找到 ->>"PHP SafeMode Configuration" -->>看下圖
把默認的 Default Safe 和 Default Open BaseDir 都 OFF,問題就解決了。
相關(guān)文章
PHP cookie,session的使用與用戶自動登錄功能實現(xiàn)方法分析
這篇文章主要介紹了PHP cookie,session的使用與用戶自動登錄功能實現(xiàn)方法,結(jié)合實例形式分析了php使用cookie與session進行數(shù)據(jù)存取以及實現(xiàn)自動登陸功能的相關(guān)操作技巧,需要的朋友可以參考下2019-06-06PHP實現(xiàn)動態(tài)壓縮js與css文件的方法
這篇文章主要介紹了PHP實現(xiàn)動態(tài)壓縮js與css文件的方法,涉及php文件讀寫及字符串替換等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05

微信公眾平臺開發(fā)關(guān)注及取消關(guān)注事件的方法
這篇文章主要介紹了微信公眾平臺開發(fā)關(guān)注及取消關(guān)注事件的方法,較為詳細的分析了微信公眾平臺設(shè)置關(guān)注的技巧,并附帶了相關(guān)參數(shù)的說明,具有一定的參考借鑒價值,需要的朋友可以參考下
2014-12-12