php 替代file_get_contents?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3979802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:37:59  来源:igfitidea点击:

Alternative to file_get_contents?

phpfile-get-contents

提问by JasonS

$xml_file = file_get_contents(SITE_PATH . 'cms/data.php');

The problem is that a server has URL file-access disabled. I cannot enable it, its a hosting thing.

问题是服务器禁用了 URL 文件访问。我无法启用它,它是一个托管的东西。

So the question is this. The data.phpfile generates xml code.

所以问题是这个。该data.php文件生成 xml 代码。

How can I execute this and get the xml data without doing the above method?

如何在不执行上述方法的情况下执行此操作并获取xml数据?

Is it possible?

是否可以?

回答by The Pellmeister

Use cURL. This function is an alternative to file_get_contents.

使用卷曲。此功能是 的替代品file_get_contents

function url_get_contents ($Url) {
    if (!function_exists('curl_init')){ 
        die('CURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $Url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

回答by AMB

You should try something like this, I am doing this for my project, its a fallback system

你应该尝试这样的事情,我正在为我的项目做这个,它是一个后备系统

//function to get the remote data
function url_get_contents ($url) {
    if (function_exists('curl_exec')){ 
        $conn = curl_init($url);
        curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($conn, CURLOPT_FRESH_CONNECT,  true);
        curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1);
        $url_get_contents_data = (curl_exec($conn));
        curl_close($conn);
    }elseif(function_exists('file_get_contents')){
        $url_get_contents_data = file_get_contents($url);
    }elseif(function_exists('fopen') && function_exists('stream_get_contents')){
        $handle = fopen ($url, "r");
        $url_get_contents_data = stream_get_contents($handle);
    }else{
        $url_get_contents_data = false;
    }
return $url_get_contents_data;
} 

then later you can do like this

然后你可以这样做

$data = url_get_contents("http://www.google.com");
if($data){
//Do Something....
}

回答by Alin Purcaru

Yes, if you have URL wrappers disabled you should use sockets or, even better, the cURLlibrary.

是的,如果您禁用了 URL 包装器,您应该使用套接字,或者更好的是cURL库。

If it's part of your site then refer to it with the file system path, not the web URL. /var/www/..., rather than http://domain.tld/....

如果它是您站点的一部分,则使用文件系统路径而不是 Web URL 来引用它。/var/www/..., 而不是http://domain.tld/...

回答by Nev Stokes

If you're trying to read XML generated from a URL without file_get_contents()then you'll probably want to have a look at cURL

如果您正在尝试读取从 URL 生成的 XML,file_get_contents()那么您可能需要查看cURL

回答by Julien Roncaglia

If the file is local as your comment about SITE_PATHsuggest, it's pretty simple just execute the script and cache the result in a variable using the output control functions:

如果文件是本地的,如您对SITE_PATH建议的评论,则非常简单,只需执行脚本并使用输出控制函数将结果缓存在变量中:

function print_xml_data_file()
{
    include(XML_DATA_FILE_DIRECTORY . 'cms/data.php');
}

function get_xml_data()
{
    ob_start();
    print_xml_data_file();
    $xml_file = ob_get_contents();
    ob_end_clean();
    return $xml_file;
}

If it's remote as lot of others said curlis the way to go. If it isn't present try socket_createor fsockopen. If nothing work... change your hosting provider.

如果它像很多其他人所说curl的那样偏远,那就是要走的路。如果它不存在,请尝试socket_createfsockopen. 如果没有任何效果……请更改您的托管服务提供商。

回答by Alan Geleynse

If you have it available, using curl is your best option.

如果你有它,使用 curl 是你最好的选择。

You can see if it is enabled by doing phpinfo()and searching the page for curl.

您可以通过phpinfo()在页面上搜索 curl来查看它是否已启用。

If it is enabled, try this:

如果已启用,请尝试以下操作:

$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL, SITE_PATH . 'cms/data.php');
$xml_file = curl_exec($curl_handle);
curl_close($curl_handle);