English 中文(简体)
文件集内容的替代方案?
原标题:Alternative to file_get_contents?
$xml_file = file_get_contents(SITE_PATH .  cms/data.php );

问题是服务器禁用了URL文件访问。我不能启用它,这是一个主机的事情。

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

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

有可能吗?

最佳回答

使用cURL。此函数是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;
}
问题回答

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

那以后你可以这样做

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

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

如果它是你网站的一部分,那么用文件系统路径而不是网址来引用它<代码>/var/www/,而不是http://domain.tld/...

如果文件是本地的,正如您对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;
}

如果它像许多其他人所说的那样遥远curl是最好的方法。如果不存在,请尝试socket_createfsockopen。如果什么都不起作用…请更改您的托管提供商。

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

如果你有,使用卷曲是你最好的选择。

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

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

$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);




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签