Prompt warning:curl_setopt () When using PHP curl function [function.curl-setopt]: Curlo ... How to solve the mistake

Source: Internet
Author: User
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 ...

The error mentions two key safe_mode and Open_basedir, if you are a virtual host without setting Appche permissions is not possible by modifying the server configuration to solve the problem, in general, the server configuration safe_mode are off, Then for some security to the user some restrictions, by setting Open_basedir to restrict the virtual host user's PHP execution folder, so when you use Curlopt_followlocation (php curl function, deep crawl data), Once the 301 turns and so on, the error message mentioned in the text will appear.

After checking the relevant information, quickly found a solution, http://www.php.cn/php-weizijiaocheng-362563.html, these methods are in the official PHP help.

The practice is to customize a function in a PHP function using the CURL statement without using curl_setopt ($ch, Curlopt_followlocation, True).

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", $data, 2); $http _code = Curl_getinfo ($ch, Curlinfo_http_code); if ($http _ Code = = 301 | | $http _code = = 302) {$matches = array ();p reg_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, curl_setopt ($ch, Curlopt_followlocation, true) This statement is replaced with Curl_redir_exec ($ch).

After this, I think your php file should not prompt the error, about this code, in the provision of PHP official connection with can be found.

The official PHP code is as follows

/safe mode cannot 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", $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

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.