Alternative to file_get_contents?

前端 未结 6 814
死守一世寂寞
死守一世寂寞 2020-12-04 23:44
$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 h

相关标签:
6条回答
  • 2020-12-05 00:12

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

    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 curl is the way to go. If it isn't present try socket_create or fsockopen. If nothing work... change your hosting provider.

    0 讨论(0)
  • 2020-12-05 00:16

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

    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/....

    0 讨论(0)
  • 2020-12-05 00:17

    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....
    }
    
    0 讨论(0)
  • 2020-12-05 00:21

    Use cURL. This function is an alternative to 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;
    }
    
    0 讨论(0)
  • 2020-12-05 00:21

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

    You can see if it is enabled by doing phpinfo() and searching the page for 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);
    
    0 讨论(0)
  • 2020-12-05 00:33

    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

    0 讨论(0)
提交回复
热议问题