2017-07-19 29 views
0

我使用的檢索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

回答

0

正如如何使用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 
0

毫無疑問,你現在已經有了這個工作,但如果不是這樣,下面的內容可能會有用。 鑑於下面的網址和幾個小幫手函數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()); 
} 
相關問題