服务器配置by allow_url_fopen = 0 in

时间:2014-02-10 12:44:01

标签: php warnings

运行脚本时出现以下错误。错误消息如下......

  

警告:file_get_contents()[function.file-get-contents]:https://在服务器配置中通过第22行/home/satoship/public_html/connect.php中的allow_url_fopen = 0禁用包装器

我知道这是服务器问题,但我需要对服务器做什么才能摆脱上述警告?

6 个答案:

答案 0 :(得分:19)

@blytung有一个很好的功能来替换这个功能

<?php
$url = "http://www.example.org/";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($ch);
if (curl_errno($ch)) {
  echo curl_error($ch);
  echo "\n<br />";
  $contents = '';
} else {
  curl_close($ch);
}

if (!is_string($contents) || !strlen($contents)) {
echo "Failed to get contents.";
$contents = '';
}

echo $contents;
?>    

答案 1 :(得分:14)

如果您无法修改php.ini文件,请使用cURL: PHP Curl And Cookies

这是我创建的示例函数:

function get_web_page( $url, $cookiesIn = '' ){
        $options = array(
            CURLOPT_RETURNTRANSFER => true,     // return web page
            CURLOPT_HEADER         => true,     //return headers in addition to content
            CURLOPT_FOLLOWLOCATION => true,     // follow redirects
            CURLOPT_ENCODING       => "",       // handle all encodings
            CURLOPT_AUTOREFERER    => true,     // set referer on redirect
            CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
            CURLOPT_TIMEOUT        => 120,      // timeout on response
            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
            CURLINFO_HEADER_OUT    => true,
            CURLOPT_SSL_VERIFYPEER => true,     // Validate SSL Cert
            CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
            CURLOPT_COOKIE         => $cookiesIn
        );

        $ch      = curl_init( $url );
        curl_setopt_array( $ch, $options );
        $rough_content = curl_exec( $ch );
        $err     = curl_errno( $ch );
        $errmsg  = curl_error( $ch );
        $header  = curl_getinfo( $ch );
        curl_close( $ch );

        $header_content = substr($rough_content, 0, $header['header_size']);
        $body_content = trim(str_replace($header_content, '', $rough_content));
        $pattern = "#Set-Cookie:\\s+(?<cookie>[^=]+=[^;]+)#m"; 
        preg_match_all($pattern, $header_content, $matches); 
        $cookiesOut = implode("; ", $matches['cookie']);

        $header['errno']   = $err;
        $header['errmsg']  = $errmsg;
        $header['headers']  = $header_content;
        $header['content'] = $body_content;
        $header['cookies'] = $cookiesOut;
    return $header;
}

注意:在重新访问此功能时,我注意到我在此代码中禁用了SSL检查。即使在我的特定情况下,我使用它的网站是本地的并且是安全的,这通常是一件坏事。因此,我已修改此代码以默认启用SSL检查。如果由于某种原因你需要更改它,你可以简单地更新CURLOPT_SSL_VERIFYPEER的值,但是如果有人使用它,我希望默认情况下代码是安全的。

答案 2 :(得分:8)

在php脚本中使用此代码(第一行)

ini_set('allow_url_fopen',1);

答案 3 :(得分:6)

编辑你的php.ini,找到allow_url_fopen并将其设置为allow_url_fopen = 1

答案 4 :(得分:0)

使用相对而不是绝对文件路径为我解决了该问题。 我遇到了同样的问题,并设置了allow_url_fopen=on 没有帮助。例如,这意味着:

使用 $file="folder/file.ext"; 代替 $file="https://website.com/folder/file.ext";

$f=fopen($file,"r+");

答案 5 :(得分:-1)

这是一个非常简单的问题

这是解决此问题的最佳方法。

第1步:登录您的cPanel(http://website.com/cpanelhttp://cpanel.website.com)。

第2步:软件 - &gt;选择PHP版本

第3步:更改当前的PHP版本:5.6

第3步:HIT'设为当前'[ENJOY]

相关问题