【问题标题】:There is a error in parsing the rss from stackoverflow.com. using SimpleXML in PHP解析来自 stackoverflow.com 的 rss 时出错。在 PHP 中使用 SimpleXML
【发布时间】:2015-05-02 20:55:43
【问题描述】:

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

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

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

任何帮助将不胜感激!谢谢!干杯!

【问题讨论】:

标签: php xml rss


【解决方案1】:

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

$loaded["entry"]

从文档元素返回名为“entry”的属性

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

$loaded->entry

这会返回名为“entry”的元素

另外注意命名空间。现有的问答材料中已经概述了使用 SimpleXML 解析提要,请与之相关。

【讨论】:

    【解决方案2】:

    试试这个。

    <?php
        $xml = file_get_contents('http://stackoverflow.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 stackoverflow.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
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多