0

我在共享服务器上托管的网站存在问题,该服务器设置了“open_basedir”...因此信用系统会引发错误并且不会向买家的信用卡收费。

错误信息

警告:curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION 在安全模式或 open_basedir 设置时无法激活

代码

function http_post($method, $server, $port, $url, $vars) {

$postdata = "";
foreach($vars as $key => $value) {
    $postdata .= urlencode($key) . "=" . urlencode($value) . "&";
}

$postdata = substr($postdata,0,-1);
$content_length = strlen($postdata);

$headers = "POST $url HTTP/1.1\r\n".
    "Accept: */*\r\n".
    "Accept-Language: en-nz\r\n".
    "Content-Type: application/x-www-form-urlencoded\r\n".
    "Host: $server\r\n".
    "Connection: Keep-Alive\r\n".
    "Cache-Control: no-cache\r\n".
    "Content-Length: $content_length\r\n\r\n";

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $method . '://' . $server .":". $port . $url);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);

$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}

无论如何,在没有访问根 PHP.ini 并且必须切换主机的情况下,是否存在这种情况?谢谢你。

4

1 回答 1

0

没有办法覆盖 in 的值,open_basedir因为php.ini这会在某种程度上违背目的。另一种方法是编写自己的函数来执行CURLOPT_FOLLOWLOCATIONopen_basedir未设置相同的功能。我使用了从http://php.benscom.com/manual/en/function.curl-setopt.php#102121找到的此代码的变体,它循环您的请求并Location:在响应标头中进行正则表达式匹配,如下所示需要一个新的请求:

function curl_exec_follow(/*resource*/ $ch, /*int*/ &$maxredirect = null) { 
    $mr = $maxredirect === null ? 5 : intval($maxredirect); 
    if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) { 
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0); 
        curl_setopt($ch, CURLOPT_MAXREDIRS, $mr); 
    } else { 
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); 
        if ($mr > 0) { 
            $newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 

            $rch = curl_copy_handle($ch); 
            curl_setopt($rch, CURLOPT_HEADER, true); 
            curl_setopt($rch, CURLOPT_NOBODY, true); 
            curl_setopt($rch, CURLOPT_FORBID_REUSE, false); 
            curl_setopt($rch, CURLOPT_RETURNTRANSFER, true); 
            do { 
                curl_setopt($rch, CURLOPT_URL, $newurl); 
                $header = curl_exec($rch); 
                if (curl_errno($rch)) { 
                    $code = 0; 
                } else { 
                    $code = curl_getinfo($rch, CURLINFO_HTTP_CODE); 
                    if ($code == 301 || $code == 302) { 
                        preg_match('/Location:(.*?)\n/', $header, $matches); 
                        $newurl = trim(array_pop($matches)); 
                    } else { 
                        $code = 0; 
                    } 
                } 
            } while ($code && --$mr); 
            curl_close($rch); 
            if (!$mr) { 
                if ($maxredirect === null) { 
                    trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.', E_USER_WARNING); 
                } else { 
                    $maxredirect = 0; 
                } 
                return false; 
            } 
            curl_setopt($ch, CURLOPT_URL, $newurl); 
        } 
    } 
    return curl_exec($ch); 
} 
于 2012-10-25T02:03:49.887 回答