簡體   English   中英

無法從PHP的Feed中提取標題?

[英]Unable to extract title from feed in PHP?

我正在嘗試從此提要中提取數據。 這是我的代碼:

$xml = file_get_contents_curl($feed_url);
$rss  = new DOMDocument();
$rss->load($xml);

函數file_get_contents_curl從網頁獲取數據。 在像這樣將其轉儲到var中時:

var_dump($xml);

它回顯了所有預期的內容(我的意思是所有標題,鏈接等標記)。 但是,如果我在$rss上使用var_dump

var_dump($rss);

我得到這個回應:

object(DOMDocument)#1 (34) { ["doctype"]=> NULL ["implementation"]=> string(22) "(object value omitted)" ["documentElement"]=> NULL ["actualEncoding"]=> NULL ["encoding"]=> NULL ["xmlEncoding"]=> NULL ["standalone"]=> bool(true) ["xmlStandalone"]=> bool(true) ["version"]=> string(3) "1.0" 
["xmlVersion"]=> string(3) "1.0" ["strictErrorChecking"]=> bool(true) ["documentURI"]=> NULL ["config"]=> NULL ["formatOutput"]=> bool(false) ["validateOnParse"]=> bool(false) ["resolveExternals"]=> bool(false) ["preserveWhiteSpace"]=> bool(true) 
["recover"]=> bool(false) ["substituteEntities"]=> bool(false) ["nodeName"]=> string(9) "#document" ["nodeValue"]=> NULL ["nodeType"]=> int(9) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL 
["lastChild"]=> NULL ["previousSibling"]=> NULL ["attributes"]=> NULL ["ownerDocument"]=> NULL ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> NULL ["baseURI"]=> NULL ["textContent"]=> string(0) "" } 

現在,我無法從Feed中提取標題或其他內容。 我的代碼是這樣的:

foreach ($rss->getElementsByTagName('item') as $node) {
  $title = $node->getElementsByTagName('title')->item(0)->nodeValue;

但是,如果您error on line 273 at column 11: Encoding error Chrome error on line 273 at column 11: Encoding error打開了feed,則會出現錯誤error on line 273 at column 11: Encoding error但是在Firefox中將其打開。 但是我想我應該能夠解析提要到錯誤的第一點。

這是提要的示例:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
    <title>eBaum's World - Featured Media</title>
    <link>http://www.ebaumsworld.com</link>
    <atom:link href="http://www.ebaumsworld.com/rss/featured/" rel="self" type="application/rss+xml" />
    <description>The latest featured media</description>
    <language>en-us</language>
    <copyright>eBaum's World (c) 1998-2015</copyright>
    <lastBuildDate>Wed, 25 Nov 2015 03:31:12 -0500</lastBuildDate>
    <pubDate>Wed, 25 Nov 2015 03:31:12 -0500</pubDate>
            <item>
        <title>24 People Being Complete A$$holes</title>
        <link>http://www.ebaumsworld.com/pictures/view/84832600/</link>
        <description>
            <![CDATA[
            <table cellspacing="0" cellpadding="2" width="100%" border="0">
                <tr>
                    <td valign="top" width="120">
                        <a href="http://www.ebaumsworld.com/pictures/view/84832600/"><img width="320" height="220" src="http://cdn.ebaumsworld.com/thumbs/2015/11/24/070634/84832600/assholes.jpg" border="0" /></a>
                    </td>
                    <td valign="top">
                        People acting like such mega-jerks it might send you into a blind rage!                     </td>
                </tr>
            </table>
            ]]>
        </description>
        <pubDate>Tue, 24 Nov 2015 23:02:00 -0500</pubDate>
        <enclosure type="image/jpg" url="http://cdn.ebaumsworld.com/thumbs/2015/11/24/070634/84832600/assholes.jpg" length="10000"/>
        <guid isPermaLink="false">http://www.ebaumsworld.com/pictures/view/84832600/</guid>
    </item>

這是我對file_get_contents_curl函數定義:

function file_get_contents_curl($url) {
  $agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)';
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

  $data = curl_exec($ch);
  curl_close($ch);

  return $data;
}

只需使用SimpleXMLElement並訪問xml節點即可。

$xml = file_get_contents_curl($feed_url);
$x = new SimpleXMLElement($xml);

foreach ($x as $node) {
  print $node->title . PHP_EOL;
  print $node->description . PHP_EOL;
}

將輸出

eBaum's World - Featured Media
The latest featured media

使用實際XML提要的url,您可以簡單地通過simplexml_load_file()加載它:

<?php
$str = '<?xml version="1.0" encoding="UTF-8"?>
$url = "http://feeds.feedburner.com/ebaumsworld/aUjW":
$xml = simplexml_load_file($url);
foreach ($xml->channel->item as $item)
    echo $item->title;
// possible output:24 People Being Complete A$$holes
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM