Warning: curl_setopt() [function.curl-setopt]: CURLO...how to solve the error when using php curl function

怪我咯
Release: 2023-03-07 19:40:01
Original
1846 people have browsed it

If you run CURLOPT_FOLLOWLOCATION in PHP and then get the php prompt error message:
Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set…

Error Mentioning the two key words safe_mode and open_basedir, if you are a virtual host and do not have permission to set APPCHE, you cannot solve the problem by modifying the server configuration. Generally speaking, the server configuration safe_mode is off, and then there are some security requirements for users. Restriction, by setting open_basedir to limit the PHP execution folder of the virtual host user, so when you use CURLOPT_FOLLOWLOCATION (php curl function, deep capture data), the error message mentioned in the article will appear once there is a 301 redirect, etc.

After checking the relevant information, I quickly found the solution, http://www.php.cn/php-weizijiaocheng-362563.html. These methods are all available in the official php help.

The specific method is in curl Whether to use curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true) in the statement, customize a function in the php function,

The code is as follows:

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;
}
}
Copy after login

After the function is defined, replace the statement curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true) with curl_redir_exec($ch)

After this, I think your PHP file should not prompt an error. Regarding this code, you can find it in the official PHP connection.

The official code of PHP is as follows

/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
Copy after login

【Related articles Recommended】

1. Introduction to the concept and usage of php curl_setopt function

2.A simple example of using the php curl_setopt() function to capture web pages and POST data

3.An example of php curl_setopt function simulating user login

4.Detailed explanation of usage examples of PHP curl_exec function

The above is the detailed content of Warning: curl_setopt() [function.curl-setopt]: CURLO...how to solve the error when using php curl function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!