无法使用 PHP 从 Wired 读取 RSS 提要
Unable to read RSS feed from Wired using PHP
我在使用 PHP 从 Wired 阅读 RSS 提要时遇到了一个奇怪的问题。我想在我的网站上显示提要。我正在使用下面的代码来获取提要。谁能告诉我我到底哪里做错了。
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/) 中检查了 Wired feed url 的有效性,它说它是一个有效的 RSS feed...
非常感谢任何帮助。谢谢!
我使用 curl
和 simplexml
:
以我的方式尝试了这段代码
$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,
);
}
我在使用 PHP 从 Wired 阅读 RSS 提要时遇到了一个奇怪的问题。我想在我的网站上显示提要。我正在使用下面的代码来获取提要。谁能告诉我我到底哪里做错了。
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/) 中检查了 Wired feed url 的有效性,它说它是一个有效的 RSS feed...
非常感谢任何帮助。谢谢!
我使用 curl
和 simplexml
:
$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,
);
}