Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试遍历节点时PHP SimpleXML中断_Php_Xml_Simplexml - Fatal编程技术网

尝试遍历节点时PHP SimpleXML中断

尝试遍历节点时PHP SimpleXML中断,php,xml,simplexml,Php,Xml,Simplexml,我正在尝试阅读tumblr提供的xml信息,以创建一种来自tumblr的新闻提要,但是我被卡住了 <?php $request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text'; $xml = simplexml_load_file($request_url); if (!$xml) { exit('Fail

我正在尝试阅读tumblr提供的xml信息,以创建一种来自tumblr的新闻提要,但是我被卡住了

<?php
    $request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
    $xml = simplexml_load_file($request_url);

    if (!$xml) 
    {
        exit('Failed to retrieve data.');
    }
    else 
    {
        foreach ($xml->posts[0] AS $post) 
        {
            $title = $post->{'regular-title'};
            $post = $post->{'regular-body'};
            $small_post = substr($post,0,320);

            echo .$title.;
            echo '<p>'.$small_post.'</p>';
        }
    }
?>
将[0]发布为$post)
{
$title=$post->{'regular-title'};
$post=$post->{'regular-body'};
$small_post=substr($post,0320);
echo.$title。;
回显“”.$small_post.“

”; } } ?>
当它试图通过节点时总是会断开。所以基本上“tumblr->posts;…ect”显示在我的html页面上

我已尝试将信息保存为本地xml文件。我尝试过使用不同的方法来创建simplexml对象,比如将其作为字符串加载(可能是一个愚蠢的想法)。我仔细检查了我的网络主机是否运行PHP5。所以基本上,我一直在想为什么这不起作用

编辑:好的,我试着从我开始的地方更改(回到原来的方式,从tumblr开始只是另一种尝试修复它的方式(实际上很愚蠢)。它在第一个->之后仍然会断开,所以在屏幕上显示“posts[0]作为$post…ect”

这是我在PHP中做的第一件事,因此可能有一些明显的事情我应该事先设置好或做些什么。但我不知道也找不到类似的事情。

这应该可以:

<?php
$request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
$xml = simplexml_load_file($request_url);

if ( !$xml ){
    exit('Failed to retrieve data.');
}else{
    foreach ( $xml->posts[0] AS $post){
        $title = $post->{'regular-title'};
        $post  = $post->{'regular-body'};
        $small_post = substr($post,0,320);

        echo $title;
        echo '<p>'.$small_post.'</p>';
        echo '<hr>';
    }
}
将[0]发布为$post){
$title=$post->{'regular-title'};
$post=$post->{'regular-body'};
$small_post=substr($post,0320);
echo$标题;
回显“”.$small_post.“

”; 回声“
”; } }
$xml->posts
返回posts节点,因此如果要迭代post节点,应该尝试
$xml->posts->post
,这样您就可以在第一个posts节点内迭代post节点

正如Needhi所指出的,您不应该通过根节点(tumblr),因为
$xml
本身就是根节点。(所以我修正了我的答案)。

代码中的第一件事是使用了不应该使用的根元素。
posts->post as$post)
{
$title=$post->{'regular-title'};
$post=$post->{'regular-body'};
$small_post=substr($post,0320);
echo.$title。;
回显“”.$small_post.“

”; } } ?>
First thing in you code is that you used root element that should not be used.

    <?php
        $request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
        $xml = simplexml_load_file($request_url);

        if (!$xml) 
        {
            exit('Failed to retrieve data.');
        }
        else 
        {

           foreach ($xml->posts->post as $post) 
            {
                $title = $post->{'regular-title'};
                $post = $post->{'regular-body'};
                $small_post = substr($post,0,320);
                echo .$title.;
                echo '<p>'.$small_post.'</p>';
            }
        }
    ?>