Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.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_Simplexml - Fatal编程技术网

Php SimpleXML未拉取正确的链接

Php SimpleXML未拉取正确的链接,php,simplexml,Php,Simplexml,我的代码没有为该项目提取正确的链接。取而代之的是获取上一个项目的链接 有什么建议吗?谢谢 <?php // Load the XML data from the specified file name. // The second argument (NULL) allows us to specify additional libxml parameters, // we don't need this so we'll leave it as NULL. The third ar

我的代码没有为该项目提取正确的链接。取而代之的是获取上一个项目的链接

有什么建议吗?谢谢

<?php
// Load the XML data from the specified file name.  
// The second argument (NULL) allows us to specify additional libxml parameters,
// we don't need this so we'll leave it as NULL.  The third argument however is
// important as it informs simplexml to handle the first parameter as a file name
// rather than a XML formatted string.
$pFile = new SimpleXMLElement("http://example.blogspot.com/feeds/posts/default?alt=rss", null, true);  

// Now that we've loaded our XML file we can begin to parse it.
// We know that a channel element should be available within the,
// document so we begin by looping through each channel
foreach ($pFile->channel as $pChild)
{    

    // Now we want to loop through the items inside this channel
    {

    foreach ($pFile->channel->item as $pItem)
    {

        // If this item has child nodes as it should, 
        // loop through them and print out the data


        foreach ($pItem->children() as $pChild)
        { 
            // We can check the name of this node using the getName() method.
            // We can then use this information, to, for example, embolden
            // the title or format a link
            switch ($pChild->getName())
            {
                case 'pubDate':
                    $date = date('l, F d, Y', strtotime($pChild));
                    echo "<p class=\"blog_time\">$date</p>"; 
                    break;

                case 'link':
                    $link = $pChild;
                    break;

                case 'title':

                    echo "<p class=\"blog_title\"><a href=\"$link\">$pChild</a></p>";
                    break;

               case 'description':
                    // echo substr(strip_tags($pChild), 0 , 270) . "...";
                    break;



                case 'author':
                    echo "";
                    break;    
                case 'category':
                    echo "";
                    break;
                case 'guid':
                    echo "";
                    break;

                default:
                    // echo strip_tags($pChild) . "<br />\n";
                    break;
            }

        }

   }


   }

}

?> 

我突然想到的是,您正在用第三个foreach的$pChild覆盖第一个foreach的$pChild。选择一个不同的变量名。

也许我会说一些愚蠢的话(也许我真的不明白你想做什么?),但这样的话不合适吗:

$pFile = new SimpleXMLElement("http://example.blogspot.com/feeds/posts/default?alt=rss", null, true);  
foreach ($pFile->channel->item as $item) {
    echo "date : " . (string)$item->pubDate . '<br />';
    echo "title : " . (string)$item->title . '<br />';
    echo "URL : " . (string)$item->link . '<br />';
    echo '<hr />';
}
好吧,如果你想显示HTML标签,你必须稍微调整一下,但这至少可以帮助你开始

date : Sun, 06 Feb 2005 13:39:00 +0000
title : ROSHAN & 2ND IN COMMAND, WOULD LIKE TO THANK THE FOLLOWING PEOPLE, FOR UNITING WITH US FOR THE PEOPLE OF SRI LANKA. MAY GOD BLESS YOU ALL!
URL : http://example.blogspot.com/2005/02/roshan-2nd-in-command-would-like-to.html
======
date : Sat, 05 Feb 2005 23:26:00 +0000
title : ROSHAN & 2ND IN COMMAND, UNITED!, FOR THE PEOPLE OF SRI LANKA, JOIN US!
URL : http://example.blogspot.com/2005/02/roshan-2nd-in-command-united-for.html
======
date : Sat, 05 Feb 2005 23:01:00 +0000
title : 2ND IN COMMAND!, A HOT EMERGING NEW ARTIST!
URL : http://example.blogspot.com/2005/02/2nd-in-command-hot-emerging-new-artist.html
======
date : Sat, 05 Feb 2005 22:43:00 +0000
title : ICP (IN COMMAND PRODUCTIONS), Hip-Hop R&B Music & much much more!, UNITED!, FOR THE PEOPLE OF SRI LANKA
URL : http://example.blogspot.com/2005/02/icp-in-command-productions-hip-hop-rb.html
======