如果當你在PHP中執行CURLOPT_FOLLOWLOCATION 然後得到php提示錯誤訊息為:
Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated openen in safedepall or based …
錯誤中提到兩個關鍵safe_mode和open_basedir,如果你是虛擬主機的沒有設定APPCHE的權限是不能透過修改伺服器設定來解決問題的,一般來說,伺服器設定safe_mode都為off,然後為了一些安全對用戶有一些限制,通過設置open_basedir來限制虛擬主機用戶的PHP執行文件夾,因此當你使用CURLOPT_FOLLOWLOCATION (php curl函數,深層抓取數據)的時候,一旦有301轉向等就會出現文中提到的錯誤訊息.
在查了相關資料後,很快找到了解決方案,http://www.php.cn/php-weizijiaocheng-362563.html,這些方法都在php官方幫助裡有.
具體做法是在curl語句用不使用curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true),在php函數中自訂一個函數,
#程式碼如下:
function curl_redir_exec($ch,$debug="") { static $curl_loops = 0; static $curl_max_loops = 20; if ($curl_loops++ >= $curl_max_loops) { $curl_loops = 0; return FALSE; } curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); $debbbb = $data; list($header, $data) = explode("\n\n", $data, 2); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code == 301 || $http_code == 302) { $matches = array(); preg_match('/Location:(.*?)\n/', $header, $matches); $url = @parse_url(trim(array_pop($matches))); //print_r($url); if (!$url) { //couldn't process the url to redirect to $curl_loops = 0; return $data; } $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL)); /* if (!$url['scheme']) $url['scheme'] = $last_url['scheme']; if (!$url['host']) $url['host'] = $last_url['host']; if (!$url['path']) $url['path'] = $last_url['path'];*/ $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:''); curl_setopt($ch, CURLOPT_URL, $new_url); // debug('Redirecting to', $new_url); return curl_redir_exec($ch); } else { $curl_loops=0; return $debbbb; } }
函數定義好後,curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true)這條語句替換為curl_redir_exec($ch)
這樣以後,我想你的PHP檔案應該不會提示錯誤了,關於這段程式碼,在提供PHP官方連接用可以找到.
php官方的程式碼如下
/safe 模式不能curl的函数 function curl_redir_exec($ch) { static $curl_loops = 0; static $curl_max_loops = 20; if ($curl_loops++ >= $curl_max_loops) { $curl_loops = 0; return FALSE; } curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); list($header, $data) = explode("\n\n", $data, 2); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code == 301 || $http_code == 302) { $matches = array(); preg_match('/Location:(.*?)\n/', $header, $matches); $url = @parse_url(trim(array_pop($matches))); if (!$url) { //couldn't process the url to redirect to $curl_loops = 0; return $data; } $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL)); if (!$url['scheme']) $url['scheme'] = $last_url['scheme']; if (!$url['host']) $url['host'] = $last_url['host']; if (!$url['path']) $url['path'] = $last_url['path']; $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:''); curl_setopt($ch, CURLOPT_URL, $new_url); debug('Redirecting to', $new_url); return curl_redir_exec($ch); } else { $curl_loops=0; return $data; } } //end
【相關文章推薦】
2.使用php curl_setopt()函數實作抓取網頁與POST資料的簡單範例
# 3.php curl_setopt函數模擬使用者登入範例
#以上是使用php curl函數時提示Warning: curl_setopt() [function.curl-setopt]: CURLO…錯誤該怎麼解決的詳細內容。更多資訊請關注PHP中文網其他相關文章!