开发者

ob_get_contents stopped working for some unknown reason

开发者 https://www.devze.com 2023-01-17 05:17 出处:网络
This script worked fine for a few weeks then stopped working for no reason. 1.<?php 开发者_开发百科2.ob_start();

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


Why not use PHPs file_get_contents()-function to fetch your URL? You would not need the ob_*-functions then. Its also possible that your php.ini has some restrictions set that are related to including external URLs. I remember reading something about this in the comments of that file.
Additionally, you could simplify the file operations to a call to the file_put_contents()-function.

EDIT: As salathe pointed out, the php.ini options allow_url_fopen and allow_url_include are relevant to your problem. You should check your configuration for these.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号