ob_get_contents 由于某种未知原因停止工作

发布于 2024-09-24 01:51:09 字数 657 浏览 0 评论 0原文

该脚本在几周内运行良好,然后无故停止运行。

1.
2.ob_start();
3.include "天气xml网站";
4.$data=ob_get_contents();
5.ob_clean();
6.
7.$xmlFile = '文件位置\weatherData.xml';
8.
9.
10.$fh = fopen($xmlFile, 'w') 或 die("无法创建或打开 $xmlFile");
11.
12.fwrite($fh, $data);
13.fclose($fh);
14.?>

我已经使用了 Google 和 Msn 的天气 API,我可以通过浏览很好地接收 xml 数据,文件处理程序可以创建和编辑本地 xml。我将此脚本设置为计划任务,每 30 分钟运行一次。

我应该使用另一种方法吗?缓存?任何帮助将不胜感激

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东风软 2024-10-01 01:51:09

为什么不使用 PHP file_get_contents()-function 来获取您的 URL?那么您就不需要 ob_* 函数了。您的 php.ini 也可能设置了一些与包含外部 URL 相关的限制。我记得在该文件的注释中读过一些有关此内容的内容。
此外,您可以将文件操作简化为对 file_put_contents() 函数的调用

编辑:正如salathe指出的,php.ini选项allow_url_fopenallow_url_include与您的问题相关。您应该检查您的配置是否有这些。

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.

夏日浅笑〃 2024-10-01 01:51:09

哦,这段代码非常不安全。包含远程文件是非常危险的。您的连接可能会被拦截,因此攻击者可以在您的服务器上执行几乎任意代码(包括删除所有文件和内容)。

所以,问题是,您的托管服务商设置了 allow_url_fopen 或 allow_url_include关闭。这些选项允许或禁止使用 PHP 文件函数和 include 语句访问远程文件。

您想要做的事情可以使用更少的代码来完成,并使您的代码更安全:

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

您可以在那里进行一些错误检查,但这基本上就是您所需要的 - 并且它可以通过操纵您的连接来防止执行任意代码!

如果仍然不起作用,可能不仅 allow_url_include 被禁用,而且 allow_url_fopen 也被禁用。在这种情况下,您别无选择,只能使用 CURL

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文