解析来自 stackoverflow.com 的 rss 时出错。在 PHP 中使用 SimpleXML

There is a error in parsing the rss from stackoverflow.com. using SimpleXML in PHP

我试图从 http://whosebug.com 解析标签 PHP 的 rss,并尝试使用 DOM 模型以外的东西,所以我研究了 SimpleXML。这是我的代码:

<?php
    error_reporting(-1);
    $xml = file_get_contents('https://whosebug.com/feeds/tag/php');
    $loaded = simplexml_load_string($xml) or die("There is a problem");
    $str1 = $loaded["entry"]["entry"][0]->title;
    echo $str1;
?>

但是屏幕上什么也没有显示,也没有报错! 来自 https://whosebug.com/feeds/tag/php 的示例数据可以在以下位置找到 http://gourabt2.cloudapp.net/sample-data/sample_data.xml

非常感谢任何帮助!谢谢!干杯!

试试这个。

<?php
    $xml = file_get_contents('http://whosebug.com/feeds/tag/php');
    $loaded = simplexml_load_string($xml) or die("There is a problem");
    foreach($loaded->entry as $post) {
       echo $post->title . "\n";
    }

输出:

join 2 tables - group by id order by date price asc
using php variable in an sql query
Clear browser cache memory by php code
There is a error in parsing the rss from whosebug.com. using SimpleXML in PHP
Modify Laravel Validation message response
chained dropdown from database
Php database handling
How to load model with Codeigniter Webservice - Nusoap Server?
multiple report download php adwords api
Unable to confirm Amazon SNS subscription for SES bounces
Comparing if values exist in database
Better way to CURL to WCF
PHP SaaS Facebook app: Host Page Tab ID and User ID
PHP and Mysql - running over PDOStatement
How to change form textbox value in Zend form annotation?
connect Android with PHP, MySQL , unfortunately app has stopped in android emulator
Auto increment a SESSION key ID
Call PHP function in a class from a HTML form
PHP SQL Preventing Duplicate User names: Catching Exception vs Select Query
I am only able to grab the first group of text in between the tr need helping fixing my code
How to run an external program in php
How to connect to php in android using an async task?
PHP Return HTML (Laravel / Lumen Framework)
prestashop smarty if statement
Cakephp OR condition Implementation
Progress bar HTML5/PHP
preg_match file url from jwplayer
Does Cloudflare Cache HTML5 Video Embedded on PHP Page?
how to INSERT INTO JOIN
PHP web service returns "403 forbidden" error

那是你走错了路。这是更正。

(string)$loaded->entry->title;

您在 SimpleXML 中使用数组访问来访问 属性 所以:

$loaded["entry"]

returns 文档元素中名为 "entry" 的属性。

使用箭头访问获取名为 "entry" 的元素:

$loaded->entry

此 returns 名为 "entry" 的元素。

另外还要注意命名空间。使用 SimpleXML 解析提要已在现有问答 material 中概述,请与它相关。