PhpCURLOPT error Warning: curl_setopt () [function. curl-setopt]:...

Source: Internet
Author: User
When we use the phpcurl function, the system prompts Warning: curl_setopt () [function. curl-setopt]: CURLOPT_FOLLOWLOCATIONcannotbe... when we use the php curl function, the prompt Warning: curl_setopt () [function. curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set... The following describes how to troubleshoot the problem.

If you run CURLOPT_FOLLOWLOCATION in php and get the php error message:

Warning: curl_setopt () [function. curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set...

Two key safe_mode and open_basedir are mentioned in the error. if you are a VM and do not set the APPCHE permission, you cannot modify the server configuration to solve the problem. Generally, the safe_mode of server configuration is off, then, for some security concerns, you can set open_basedir to restrict the PHP execution folder of VM users. Therefore, when you use CURLOPT_FOLLOWLOCATION (php curl function to deeply capture data, once there is a 301 redirection, the error message mentioned in this article will appear.

After checking the relevant information, I quickly found a solution: http://www.php.net/manual/en/function.curl-setopt.php, which was provided by the PHP official support team.

The specific method is to use the curl statement without using curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, true) to customize a function in the php function. the code is as follows:

 = $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;    }}

After the function is defined, replace the statement curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, true) with curl_redir_exec ($ ch ). in this case, I don't think an error will be prompted for your PHP file. for this code, you can find it in the official PHP connection.


Link to this article:

Add to favorites ^ please keep the tutorial address.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.