關(guān)于php curl獲取301或302轉(zhuǎn)向的網(wǎng)址問題的解決方法
更新時(shí)間:2011年06月02日 23:07:49 作者:
關(guān)于php curl獲取301或302轉(zhuǎn)向的網(wǎng)址問題的解決方法,其實(shí)可以通過修改服務(wù)器的配置即可。
在使用php的curl獲取遠(yuǎn)程文件,代碼如下:
<?
$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)后的頁(yè)面
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,(一般國(guó)外主機(jī)商都是用這配置)如果獲取的網(wǎng)址有301/302跳轉(zhuǎn),會(huì)報(bào)錯(cuò):
curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in ***
關(guān)于這個(gè)問題,google,百度一下,都是在大篇長(zhǎng)長(zhǎng)的english,不精通linux的還真頭痛。
解決辦法其實(shí)很簡(jiǎn)單:登陸你的 directadmin
找到 ->>"PHP SafeMode Configuration" -->>看下圖
復(fù)制代碼 代碼如下:
<?
$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)后的頁(yè)面
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,(一般國(guó)外主機(jī)商都是用這配置)如果獲取的網(wǎng)址有301/302跳轉(zhuǎn),會(huì)報(bào)錯(cuò):
curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in ***
關(guān)于這個(gè)問題,google,百度一下,都是在大篇長(zhǎng)長(zhǎng)的english,不精通linux的還真頭痛。
解決辦法其實(shí)很簡(jiǎn)單:登陸你的 directadmin
找到 ->>"PHP SafeMode Configuration" -->>看下圖
把默認(rèn)的 Default Safe 和 Default Open BaseDir 都 OFF,問題就解決了。
相關(guān)文章
php實(shí)現(xiàn)將Session寫入數(shù)據(jù)庫(kù)
這篇文章主要介紹了php實(shí)現(xiàn)將Session寫入數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下2015-07-07
微信公眾號(hào)用戶與網(wǎng)站用戶的綁定解決方案分析
這篇文章主要介紹了微信公眾號(hào)用戶與網(wǎng)站用戶的綁定解決方案,結(jié)合實(shí)例形式分析了微信公眾號(hào)用戶與網(wǎng)站用戶綁定的原理、操作步驟、技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2019-04-04
PHP iconv()函數(shù)字符編碼轉(zhuǎn)換的問題講解
今天小編就為大家分享一篇關(guān)于PHP iconv()函數(shù)字符編碼轉(zhuǎn)換的問題講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03

