file_get_contents的替代?

Jas*_*onS 41 php file-get-contents

$xml_file = file_get_contents(SITE_PATH . 'cms/data.php');
Run Code Online (Sandbox Code Playgroud)

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

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

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

可能吗?

The*_*ter 112

使用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;
}
Run Code Online (Sandbox Code Playgroud)


AMB*_*AMB 8

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

//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;
} 
Run Code Online (Sandbox Code Playgroud)

然后你可以这样做

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