2010-10-08 132 views
10

我得到一個錯誤PHP的捲曲與CURLOPT_FOLLOWLOCATION錯誤

CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in

我谷歌很多的解決方案,但這個網站他們不工作。只需要CURLOPT_FOLLOWLOCATION。愚蠢的主機不想啓用safe_mode或open_basedir。我可以自己啓用它們,可以使用一些參數創建htaccess嗎?

回答

13

錯誤意味着safe_mode或open_basedir啓用(可能是open_basedir)如果您的主機有任何體面的安全設置,則您可能無法重寫該錯誤。

所以,你有一個選擇

1)改變主機(不是真的希望我想象)

2)使用類似PHP curl_setopt頁面上找到那些函數,即http://www.php.net/manual/en/function.curl-setopt.php#95027

的以下是第2點中

function curl_redirect_exec($ch, &$redirects, $curlopt_header = false) { 
    curl_setopt($ch, CURLOPT_HEADER, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    $data = curl_exec($ch); 

    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    if ($http_code == 301 || $http_code == 302) { 
     list($header) = explode("\r\n\r\n", $data, 2); 

     $matches = array(); 
     preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches); 
     $url = trim(str_replace($matches[1], "", $matches[0])); 

     $url_parsed = parse_url($url); 
     if (isset($url_parsed)) { 
      curl_setopt($ch, CURLOPT_URL, $url); 
      $redirects++; 
      return curl_redirect_exec($ch, $redirects, $curlopt_header); 
     } 
    } 

    if ($curlopt_header) { 
     return $data; 
    } else { 
     list(, $body) = explode("\r\n\r\n", $data, 2); 
     return $body; 
    } 
} 
+0

有趣,但腳本在我的主機上運行良好(廉價和虛擬),但老闆使用愚蠢的主機,並沒有改變的願望。關於這個功能 - 我知道它,當我開始寫腳本我手動調用每個url,但最後一個url不會不會工作,只有使用CURLOPT_FOLLOWLOCATION幫助我 – kusanagi 2010-10-08 13:25:37

+0

Eek對不起,我沒有實際測試函數,重新對它不起作用(它假設位置標題不是最後一個)我已經添加了一個我測試過的修正版本給我的答案。 – Rwky 2010-10-08 17:31:08

+1

您的解決方案有一些錯誤。正則表達式應該是'[^ \ n]'。當遞歸調用'curl_redirect_exec'時,你應該傳遞'$ curlopt_header'。 – 2011-11-01 22:16:15

0

您好我修理添加此條件中指定的功能的一個固定的版本:

$safeMode = @ini_get('safe_mode'); 
$openBasedir = @ini_get('open_basedir'); 
if (empty($safeMode) && empty($openBasedir)) { 
    curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true); 
}else 
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 

,並加入我的.htaccess文件

php_flag safe_mode off 
php_flag open_basedir off 
+2

你的$ curl和$ curl_handle變量來自哪裏? – Craig 2015-02-10 20:14:47

1

如果你指定只有HTTP和HTTPS協議使用CURLOPT_REDIR_PROTOCOLS重定向過程中允許,你將能夠使用CURLOPT_FOLLOWLOCATION沒有safe_modeopen_basedir

+2

將'CURLOPT_REDIR_PROTOCOLS'設置爲'CURLPROTO_HTTP |可悲的是,CURLPROTO_HTTPS'似乎對'open_basedir'沒有幫助。 – Mahn 2016-06-20 02:54:41

+0

對我而言...... – zeflex 2016-07-27 18:58:07