[英]curl_setopt() CURLOPT_FOLLOWLOCATION issue with TCPDF
我已經將一個網站移動到另一個網站托管。 當我在localhost上測試時一切正常,但是當我在線試用時,我收到以下內容:
curl_setopt() [<a href='function.curl-setopt'>function.curl-setopt</a>]:
CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or
an open_basedir is set
當我嘗試使用TCPDF生成PDF文件時(7542行生成錯誤)
7534 if ($imsize === FALSE) {
7535 if (function_exists('curl_init')) {
7536 // try to get remote file data using cURL
7537 $cs = curl_init(); // curl session
7538 curl_setopt($cs, CURLOPT_URL, $file);
7539 curl_setopt($cs, CURLOPT_BINARYTRANSFER, true);
7540 curl_setopt($cs, CURLOPT_FAILONERROR, true);
7541 curl_setopt($cs, CURLOPT_RETURNTRANSFER, true);
7542 curl_setopt($cs, CURLOPT_FOLLOWLOCATION, true);
我該怎么做才能避免這種情況?
如果托管公司/部門不願意關閉safe_mode,那么解決方法可能是在php.net http://php.net/manual/ro/function.curl-setopt.php#71313上找到的這個有用的片段。
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_array($ch, array(CURLOPT_HEADER => true, 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));
foreach(array('scheme', 'host', 'path') as $component) {
if (!$url[$component]) {
$url[$component] = $last_url[$component];
}
}
$new_url = $url['scheme'] . '://' . $url['host'] . $url['path']
. ($url['query'] ? '?' . $url['query'] : '');
curl_setopt($ch, CURLOPT_URL, $new_url);
return curl_redir_exec($ch);
} else {
$curl_loops = 0;
return $data;
}
}
錯誤消息告訴您錯誤:
CURLOPT_FOLLOWLOCATION
safe_mode或設置open_basedir時,無法激活CURLOPT_FOLLOWLOCATION
您應該能夠以這種方式獲取當前配置:
var_dump(array_map('ini_get', array('safe_mode', 'open_basedir')));
要刪除錯誤,請與您的托管服務商的支持部門聯系,並告訴他們您對PHP設置的技術要求。 如果主機無法滿足您的要求,則您為PHP腳本選擇了錯誤的主機。
您應該在主機中關閉safe_mode
和open_basedir
。 您可以通過托管支持詢問。 如果它不可用,您可以將open_basedir
的值更改為(0)。
例:
curl_setopt($rConnect, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($rConnect, CURLOPT_FOLLOWLOCATION, 1);
應該改為
curl_setopt($rConnect, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($rConnect, CURLOPT_FOLLOWLOCATION, 0);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.