簡體   English   中英

無法使用PHP從Wired讀取RSS feed

[英]Unable to read RSS feed from Wired using PHP

我面臨一個奇怪的問題,即使用PHP從Wired讀取RSS feed。 我想在我的網站上顯示提要。 我正在使用以下代碼來獲取供稿。 誰能讓我知道我到底在做什么錯。

try {
    $rss = new DOMDocument();
    $rss->load('https://www.wired.com/category/reviews/feed/');
    if ($rss->validate()) {
        echo "This document is valid!\n";
    }else{
        echo "This document is not valid!\n";
    }
}catch (Exception $e) {
    return array('error'=>'Error raised in reading the feed','exception'=>$e);
}

$feeds =  array();
foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
    );
    array_push($feeds, $item);
}

執行完上面的代碼后,出現以下錯誤

Warning: DOMDocument::load(): Start tag expected, '<' not found in https://www.wired.com/category/reviews/feed/, line: 1 in D:\xampp\htdocs\my_functions\index.php on line 4

Warning: DOMDocument::validate(): no DTD found! in D:\xampp\htdocs\my_functions\index.php on line 5
This document is not valid!

我還在“ Feed驗證器( https://www.feedvalidator.org/ )”中檢查了有線feed URL的有效性,並說這是有效的RSS feed ...

任何幫助,不勝感激。 謝謝!

我使用curlsimplexml以我的方式嘗試了這段代碼:

$handle = curl_init('https://www.wired.com/category/reviews/feed/');

curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, TRUE); 
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($handle, CURLOPT_ENCODING, 'identity');

/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);

curl_close($handle);

$xml = simplexml_load_string($response, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$data = json_decode($json, true);
// print_r($data);
$feeds =  array();
if(isset($data['channel']['item'])) {
    foreach($data['channel']['item'] as $item) {
        $feeds[] =  array(
            'title' =>  $item['title'],
            'desc'  =>  $item['description'],
            'link'  =>  $item['link'],
        );
    }
}
print_r($feeds);

謝謝@Gaurav ..和我的Domdocument版本

define('RSS_FEED_URL', 'https://www.wired.com/category/reviews/feed/');
try {
    // fetch the rss feeds from wired!
    $handle = curl_init(RSS_FEED_URL);

    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($handle, CURLOPT_FOLLOWLOCATION, TRUE); 
    curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($handle, CURLOPT_ENCODING, 'identity');

    $response = curl_exec($handle);
    curl_close($handle);

    $rss = new DOMDocument();
    $rss->loadXML($response);
}catch (Exception $e) {
    echo "Failed to fetch RSS Feed properly for Blogs!!";
    return array();
}

foreach ($rss->getElementsByTagName('item') as $node) {
    $feeds[] = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        // 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
    );
}

暫無
暫無

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

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