phpxmlob-get-contents

ob_get_contents stopped working for some unknown reason


This script worked fine for a few weeks then stopped working for no reason.

1.<?php
2.ob_start();
3.include "weather xml website";
4.$data=ob_get_contents();
5.ob_clean();
6.
7.$xmlFile = 'filelocation\weatherData.xml';
8.
9.
10.$fh = fopen($xmlFile, 'w') or die("can not create or open $xmlFile");
11.
12.fwrite($fh, $data);
13.fclose($fh);
14.?>

I have used Google's and Msn's weather APIs and I can recieve the xml data fine by browsing, the file handler can create and edit the local xml. I had this script setup as a scheduled task to be run every 30 minutes.

Is there another method I should be using? caching? any help would be greatly appreciated


Solution

  • Oh, this code is so insecure. Including a remote file is highly dangerous. Your connection might be intercepted and thus an attacker could execute nearly arbitrary code on your server (including removing all files and stuff).

    So, the problem is, that your hoster has set either allow_url_fopen or allow_url_include to Off. These options allow or disallow access to remote files using PHPs file functions and using the include statement.

    What you want to do may be accomplished using far less code and making your code more secure:

    file_put_contents('filelocation\weatherData.xml', file_get_contents('weather xml website'));
    

    You could but some error checking in there, but that's basically all you need - and it prevents execution of arbitrary code by manipulating your connection!

    If that still doesn't work probably not only allow_url_include is disabled, but allow_url_fopen is too. In this case you have no choice then to use CURL.