的DomDocument不看<机箱URL =“” />

问题描述:

我使用的检索RSS,把它作为JSON的PHP脚本。的DomDocument不看<机箱URL =“” />

它开始通过

$feed = new DOMDocument(); 
$feed->load($_GET['url']); 

我使用的饲料样子(网址:RSS FEED

enter image description here

,并有一个很好的下。

我看到了,我无法访问这些数据。其实当我 var_dump($ feed); 我没有看到机箱的任何概念,没有https://MYURL.COM/MYPATH

所以问题的概念:为什么和怎样:-)

谢谢!

编辑:

以下是完整的脚本和VAR转储内容:当https://www.dealabs.com/rss/new.xml网址在params为通过$饲料(太长,在这里)的

<?php 
header('Content-Type: application/json'); 
$feed = new DOMDocument(); 
$feed->load($_GET['url']); 

$json = array(); 

$json['title'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('title')->item(0)->firstChild->nodeValue; 
$json['description'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('description')->item(0)->firstChild->nodeValue; 
$json['link'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('link')->item(0)->firstChild->nodeValue; 


$items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item'); 
$json['items'] = array(); 
$i = 0; 
foreach($items as $item) { 
    $json['items'][$i]['title'] = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue; 
    $json['items'][$i]['description'] = $item->getElementsByTagName('description')->item(0)->firstChild->nodeValue; 
    $json['items'][$i]['pubdate'] = $item->getElementsByTagName('pubDate')->item(0)->firstChild->nodeValue; 
    $json['items'][$i]['guid'] = $item->getElementsByTagName('guid')->item(0)->firstChild->nodeValue; 
    $json['items'][$i]['link'] = $item->getElementsByTagName('link')->item(0)->firstChild->nodeValue; 
    //$json['items'][$i]['url'] = $item->getELementsByTagName('nodeValue')->item(0)->firstChild->getAttribute('url'); 

    $i++; 
} 

echo json_encode($json); 
?> 

VAR DUMP:pastebin

+0

可能有助于分享$ feed的转储。 – Devon

+0

请提供可重现此问题的示例代码。 – ThW

+0

@Devon我更新了帖子! – clement

正如如何使用DOM文档和提取数据从XML文档进行简单演示...

$feed = new DOMDocument(); 
$feed->load($_GET['url']); 

$xpath=new DOMXPath($feed); 

foreach ($xpath->query("//enclosure") as $enclosure) { 
    echo "Element=".$feed->saveXML($enclosure)."\n"; 
    var_dump($enclosure); 
    echo "Url=".$enclosure->getAttribute("url")."\n"; 
} 

正如可以看到的,我使用XPath来从所述源外壳元件和第一打印XML列(必须使用文档saveXML方法来输出XML)。下一行显示了var_dump为您提供了什么 - 基本上有很多内部的东西支持DOM结构。最后,打印url属性的值。

使用像我可以从你的样本数据(总是更好的包括数据,而不是图像)获得。输出给...

Element=<enclosure url="https://something/url"/> 
/home/nigel/workspace/PHPTest/XML/test2.php:13: 
class DOMElement#3 (18) { 
    public $tagName => 
    string(9) "enclosure" 
    public $schemaTypeInfo => 
    NULL 
    public $nodeName => 
    string(9) "enclosure" 
    public $nodeValue => 
    string(0) "" 
    public $nodeType => 
    int(1) 
    public $parentNode => 
    string(22) "(object value omitted)" 
    public $childNodes => 
    string(22) "(object value omitted)" 
    public $firstChild => 
    NULL 
    public $lastChild => 
    NULL 
    public $previousSibling => 
    string(22) "(object value omitted)" 
    public $nextSibling => 
    string(22) "(object value omitted)" 
    public $attributes => 
    string(22) "(object value omitted)" 
    public $ownerDocument => 
    string(22) "(object value omitted)" 
    public $namespaceURI => 
    NULL 
    public $prefix => 
    string(0) "" 
    public $localName => 
    string(9) "enclosure" 
    public $baseURI => 
    string(40) "/home/nigel/workspace/PHPTest/XML/t1.xml" 
    public $textContent => 
    string(0) "" 
} 
Url=https://something/url 

毫无疑问,你现在已经有了这个工作,但如果不是这样,下面的内容可能会有用。 鉴于下面的网址和几个小帮手函数getchildgetvalue,您可以简单地遍历XML/RSS提要中的每个item像这样 - 从您想要捕获的enclosure中选择任何属性。事实上,你很可能希望使辅助函数更健壮,但你应该明白。

define('BR','<br />'); 
$url='https://www.dealabs.com/rss/new.xml'; 

function getchild($node,$index){ 
    $child=$node->childNodes->item($index); 
    if(!$child)throw new Exception(__FUNCTION__ .' -> Unable to find child node',$index); 
    return $child; 
} 
function getvalue($node){ 
    return $node->nodeValue; 
} 

try{ 

    libxml_use_internal_errors(true); 
    $dom=new DOMDocument; 
    $dom->preserveWhiteSpace = false; 
    $dom->validateOnParse = false; 
    $dom->standalone=true; 
    $dom->strictErrorChecking=false; 
    $dom->substituteEntities=true; 
    $dom->recover=true; 
    $dom->formatOutput=false; 
    $dom->load($url); 

    $errors = libxml_get_errors(); 
    libxml_clear_errors(); 


    if(!empty($errors)) { 
     throw new Exception(implode(PHP_EOL, $errors)); 
    } 

    $items=$dom->getElementsByTagName('item'); 

    if(!empty($items)){ 

     foreach($items as $index => $item){ 
      try{ 

       $title=getvalue(getchild($item, 0)); 
       $link=getvalue(getchild($item,1)); 
       $description=getvalue(getchild($item,2)); 
       $content=getvalue(getchild($item,3)); 
       $guid=getvalue(getchild($item,4)); 
       $pubDate=getvalue(getchild($item,5)); 
       $enclosure=getchild($item, 6); 

       $x=getvalue(getchild($item, 69)); 

       /* elected to get the url only but same method for other attributes */ 
       echo $enclosure->getAttribute('url').BR; 

      }catch(Exception $e){ 
       printf('Caught Exception: %s @ index %d<br />', $e->getMessage(), $e->getCode()); 
       continue; 
      } 
     } 
    } 
    $dom=null; 
}catch(Exception $e){ 
    printf('Caught Exception -> Trace:%s Message:%s Code:%d', $e->getTraceAsString(), $e->getMessage(), $e->getCode()); 
}