|
復(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)后的頁面
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,(一般國外主機(jī)商都是用這配置)如果獲取的網(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的還真頭痛。
解決辦法其實(shí)很簡單:登陸你的 directadmin
找到 ->>"php SafeMode Configuration" -->>看下圖
把默認(rèn)的 Default Safe 和 Default Open BaseDir 都 OFF,問題就解決了。
php技術(shù):關(guān)于php curl獲取301或302轉(zhuǎn)向的網(wǎng)址問題的解決方法,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。