Home > php教程 > php手册 > body text

php CURLOPT错误Warning: curl_setopt() [function.curl-setopt]:...

WBOY
Release: 2016-05-24 09:00:06
Original
1163 people have browsed it

在我们使用php curl函数时提示Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set…错误,下面我就来介绍碰到此问题要如何来排除问题吧。

如果当你在php中运行 CURLOPT_FOLLOWLOCATION 然后得到php提示错误信息为:

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

错误中提到两个关键safe_mode和 open_basedir,如果你是虚拟主机的没有设置APPCHE的权限是不能通过修改服务器配置来解决问题的,一般来说,服务器配置safe_mode都为off,然后为了一些安全对用户有一些限制,通过设置open_basedir来限制虚拟主机用户的PHP执行文件夹,因此当你使用CURLOPT_FOLLOWLOCATION (php curl函数,深层抓取数据)的时候,一旦有301转向等就会出现文中提到的错误信息.

在查了相关资料后,很快找到了解决办法,http://www.php.net/manual/en/function.curl-setopt.php,这些方法都在php官方帮助里有.

具体做法是在curl语句用不使用curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true),在php函数中自定义一个函数,代码如下:

<?php
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(&#39;/Location:(.*?)\n/&#39;, $header, $matches);
        $url = @parse_url(trim(array_pop($matches)));
        //print_r($url);
        if (!$url) {
            //couldn&#39;t process the url to redirect to
            $curl_loops = 0;
            return $data;
        }
        $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
        /*    if (!$url[&#39;scheme&#39;])
        $url[&#39;scheme&#39;] = $last_url[&#39;scheme&#39;];
        if (!$url[&#39;host&#39;])
        $url[&#39;host&#39;] = $last_url[&#39;host&#39;];
        if (!$url[&#39;path&#39;])
        $url[&#39;path&#39;] = $last_url[&#39;path&#39;];*/
        $new_url = $url[&#39;scheme&#39;] . &#39;://&#39; . $url[&#39;host&#39;] . $url[&#39;path&#39;] . ($url[&#39;query&#39;] ? &#39;?&#39; . $url[&#39;query&#39;] : &#39;&#39;);
        curl_setopt($ch, CURLOPT_URL, $new_url);
        // debug(&#39;Redirecting to&#39;, $new_url);
        return curl_redir_exec($ch);
    } else {
        $curl_loops = 0;
        return $debbbb;
    }
}
Copy after login

函数定义好后, curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true) 这条语句替换为curl_redir_exec($ch) . 这样以后, 我想你的PHP文件应该不会提示错误了, 关于这段代码, 在提供PHP官方连接用可以找到 . 


本文链接:

收藏随意^^请保留教程地址.

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template