50
$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.

So the question is this. The data.php file generates xml code.

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

Is it possible?

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
ComputerUser
  • 4,828
  • 15
  • 58
  • 71
  • Is SITE_PATH part of *your* site? Or is it somewhere else? – VoteyDisciple Oct 20 '10 at 15:56
  • It is part of my site. SITE_PATH = 'http://mydomain.com/'; – ComputerUser Oct 20 '10 at 15:57
  • possible duplicate of [How to scrape websites when cURL and allow_url_fopen is disabled](http://stackoverflow.com/questions/3880628/how-to-scrape-websites-when-curl-and-allow-url-fopen-is-disabled) – Gordon Oct 20 '10 at 16:10
  • 3
    if mydomain.com is the same site that the code is running on, you shouldn't need to use a http:// request to load the file. In fact, doing so means you're ramping up your own bandwidth (the http request and response will both get counted twice toward your bandwidth total). Even if you need to execute a local PHP program and just get the ouput, there are other ways to achieve it that don't involve using http. – Spudley Oct 20 '10 at 16:10
  • Have you tried using curl instead? – camomileCase Oct 20 '10 at 16:11

6 Answers6

124

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;
}
Brane
  • 3,257
  • 2
  • 42
  • 53
Pelle
  • 6,423
  • 4
  • 33
  • 50
  • but still I'm getting "CURL is not installed!" But I enabled php_curl and php_openssl extensions – CJ Ramki Mar 12 '15 at 08:06
  • 1
    "CURL is not installed" means that you need to install cURL. Which is a rare situation these days, most systems have it installed by default. Most dpkg-based distros serve a package called *php5_curl* that comes with the right dependencies and configuration directives. – Pelle Mar 13 '15 at 11:05
15

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....
}
3

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

Alin Purcaru
  • 43,655
  • 12
  • 77
  • 90
3

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.

Julien Roncaglia
  • 17,397
  • 4
  • 57
  • 75
2

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

Nev Stokes
  • 9,051
  • 5
  • 42
  • 44
0

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);
Alan Geleynse
  • 24,821
  • 5
  • 46
  • 55