有没有办法让 simpleXML 在网络主机上工作

发布于 2024-09-24 09:22:22 字数 451 浏览 1 评论 0原文

    <?php
$twitter_url = 'http://twitter.com/statuses/user_timeline/ishrikrishna.xml?count=1';

$buffer = file_get_contents($twitter_url);

$xml = new SimpleXMLElement($buffer);

$status = $xml -> status;

$tweet =  $status -> text;

echo $tweet;

?>

我使用此代码来获取推文,它在本地主机上成功运行,但在我的虚拟主机上失败,我在两个虚拟主机服务上尝试了此脚本。

我注意到的问题是 file_get_contents()、simplexml_load_file() 等函数无法从存储在另一台服务器上的 xml 文件(例如 rss 文件)中获取数据。

    <?php
$twitter_url = 'http://twitter.com/statuses/user_timeline/ishrikrishna.xml?count=1';

$buffer = file_get_contents($twitter_url);

$xml = new SimpleXMLElement($buffer);

$status = $xml -> status;

$tweet =  $status -> text;

echo $tweet;

?>

I used this code to fetch tweets and it works successfully on localhost but not on my webhost, I tried this script on two webhosting services.

The problem i've noticed is that functions like file_get_contents(), simplexml_load_file() failed to fetch data from xml file(ex. rss files) stored on another server.

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

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

发布评论

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

评论(2

原野 2024-10-01 09:22:22

我相信这意味着您已关闭 fopen URL 包装器。请参阅 http://www.php.net /manual/en/filesystem.configuration.php#ini.allow-url-fopen 如果您使用共享 Web 服务器,您可能无法打开这些功能。

您也许可以使用 cURL 来获取远程页面:

$ch = curl_init('http://twitter.com/statuses/user_timeline/ishrikrishna.xml?count=1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$buffer = curl_exec($ch);

这仅在您的网络主机上安装并启用了 cURL 扩展后才有效。

请参阅 PHP cURL 文档: http://www.php.net/manual/ en/book.curl.php

编辑:更正了curl_setopt调用

I believe this means you have the fopen URL wrappers turned off. See http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen You will probably not be able to turn these on if you are using a shared web server.

You may be able to use cURL to fetch remote pages instead:

$ch = curl_init('http://twitter.com/statuses/user_timeline/ishrikrishna.xml?count=1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$buffer = curl_exec($ch);

This will only work you have the cURL extension installed and enabled on your webhost.

See the PHP cURL documentation: http://www.php.net/manual/en/book.curl.php

Edit: corrected curl_setopt call

挽梦忆笙歌 2024-10-01 09:22:22

SimpleXML 是 PHP 5 中的新功能。我认为几乎所有网络主机都安装了 PHP 5,但如果您的主机仍在使用 PHP 4,这可能是您的脚本无法工作的原因。

SimpleXML is new in PHP 5. I think almost all webhosts have PHP 5 installed, but in case your host is still using PHP 4 that may be the reason your script isn't working.

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