Php 如何从html表单读取rss提要
我正在尝试构建一个HTML/PHP应用程序,在该应用程序中,您可以在表单字段中键入RSS URL,然后在进入rssfeed.PHP页面后在浏览器中检索RSS提要。目前,我收到一个错误Php 如何从html表单读取rss提要,php,html,forms,rss,rss-reader,Php,Html,Forms,Rss,Rss Reader,我正在尝试构建一个HTML/PHP应用程序,在该应用程序中,您可以在表单字段中键入RSS URL,然后在进入rssfeed.PHP页面后在浏览器中检索RSS提要。目前,我收到一个错误file\u get\u contents()期望参数1是一个有效的路径,数组在第2行C:\xampp\htdocs\week14\rssfeed.php中给出 以下是我的index.html代码: <html> <body> <form action="rssfeed.php" m
file\u get\u contents()期望参数1是一个有效的路径,数组在第2行C:\xampp\htdocs\week14\rssfeed.php中给出
以下是我的index.html代码:
<html>
<body>
<form action="rssfeed.php" method="post">
Please enter the RSS feed URL: <input type="content" name="name"><br>
<input type="submit">
</form>
</body>
</html>
请输入RSS源URL:
以下是rssfeed.php代码:
<?php
$content = file_get_contents($_POST);
?>
我甚至不知道谷歌还能做什么,我对从头开始构建代码还不熟悉
这就是显示来自espn的提要的内容,但我需要将其集成,以便在我将URL键入表单时,它会将该URL放入此代码段中URL当前所在的位置:
$feed_url = "http://rssfeeds.usatoday.com/UsatodaycomGolf-TopStories";
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo '<ul>';
foreach($x->channel->item as $entry) {
echo '<li>';
echo '<a href="'.$entry->link.'" title="'.$entry->title.'" target="_blank">' . $entry->title . '</a>'; // output link & title
echo $entry->description; // return post content
echo '</li>';
}
echo "</ul>";
$feed\u url=”http://rssfeeds.usatoday.com/UsatodaycomGolf-TopStories";
$content=file\u get\u contents($feed\u url);
$x=新的SimpleXmlElement($content);
回声“”;
foreach($x->channel->item as$entry){
回音“- ”;
回显“”;//输出链接和标题
echo$entry->description;//返回帖子内容
回音“
”;
}
回声“
”;
了解如何将一些RSSFeed
的url发布到脚本,然后对其进行处理,这可能会有所帮助
要处理RSSFeed的内容,通常需要使用DOMDocument
或SimpleXML
——这里的代码只需将远程XML文件直接加载到DOMDocument
中,并实例化XPath对象以进一步查询以查找节点。有许多其他的方法可以做到这一点,但它很快就作为一个例子写了出来
<html>
<head>
<title>RSS</title>
</head>
<body>
<form method='post'>
<input type='text' name='name' value='https://www.huffingtonpost.co.uk/feeds/index.xml' />
<input type='submit'>
</form>
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['name'] ) ){
$dom=new DOMDocument;
$dom->load( $_POST['name'] );
$xp=new DOMXPath( $dom );
$col=$xp->query( '//channel/item' );
if( $col->length > 0 ){
foreach( $col as $node ){
printf(
'<h3>%s</h3>',
$xp->query('title',$node)->item(0)->textContent
);
}
}
}
?>
</body>
</html>
RSS
输出中的代码段:
<h3>Where To Travel In 2020: The Top 10 Emerging Destinations</h3>
<h3>Gavin And Stacey Christmas Special: First Reviews Of Reunion Episode Are In</h3>
<h3>Jeremy Corbyn Speaks To The Common People | The People's Election</h3>
2020年旅行地点:十大新兴目的地
加文和斯泰西的圣诞特辑:重逢插曲的第一批评论已经发布
杰里米·科尔宾(Jeremy Corbyn)向普通民众发表讲话|人民选举
一个更完整的示例显示了每篇文章中的多个可能的RSS提要和更多字段:
<html>
<head>
<title>RSS</title>
</head>
<body>
<form method='post'>
<select name='name'>
<optgroup label='World news'>
<option>http://rssfeeds.usatoday.com/UsatodaycomGolf-TopStories
<option>http://feeds.reuters.com/Reuters/worldNews
<option>http://rss.cnn.com/rss/edition_world.rss
</optgroup>
<optgroup label='UK news'>
<option>http://feeds.bbci.co.uk/news/rss.xml
<option>http://feeds.skynews.com/feeds/rss/uk.xml
<option>http://feeds.reuters.com/reuters/UKdomesticNews
</optgroup>
<optgroup label='US news'>
<option>http://rssfeeds.usatoday.com/usatoday-newstopstories
<option>http://feeds.reuters.com/Reuters/domesticNews
<option>http://feeds.skynews.com/feeds/rss/us.xml
</optgroup>
<optgroup label='Miscellaneous'>
<option>https://www.wired.com/feed
<option>http://rss.slashdot.org/Slashdot/slashdot
<option>https://www.huffingtonpost.co.uk/feeds/index.xml
</optgroup>
</select>
<input type='submit'>
</form>
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['name'] ) ){
$url=$_POST['name'];
$max=150;
$ellipsis=str_repeat('.',5);
$dom=new DOMDocument;
$dom->load( $url );
$xp=new DOMXPath( $dom );
$col=$xp->query( '//channel/item' );
if( $col->length > 0 ){
echo '<ul>';
foreach( $col as $node ){
try{
$description=$xp->evaluate('string(description)',$node);
if( strlen( $description ) > $max )$description=substr( $description, 0, $max ) . $ellipsis;
$category=$xp->evaluate( 'string(category)', $node );
printf(
'<li>
<a href="%s" target="_blank">%s</a>
<div>Category: %s</div>
<p>%s</p>
</li>',
$xp->evaluate( 'string(link)', $node ),
$xp->evaluate( 'string(title)',$node ),
$category,
$description
);
}catch( Exception $e ){
continue;
}
}
echo '</ul>';
} else {
echo 'nothing found for given XPath query';
}
}
?>
</body>
</html>
RSS
http://rssfeeds.usatoday.com/UsatodaycomGolf-TopStories
http://feeds.reuters.com/Reuters/worldNews
http://rss.cnn.com/rss/edition_world.rss
http://feeds.bbci.co.uk/news/rss.xml
http://feeds.skynews.com/feeds/rss/uk.xml
http://feeds.reuters.com/reuters/UKdomesticNews
http://rssfeeds.usatoday.com/usatoday-newstopstories
http://feeds.reuters.com/Reuters/domesticNews
http://feeds.skynews.com/feeds/rss/us.xml
https://www.wired.com/feed
http://rss.slashdot.org/Slashdot/slashdot
https://www.huffingtonpost.co.uk/feeds/index.xml
授予此权限,仅读取http链接而非https:
Index.html:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css" type="text/css">
<title>RSS Feed Search</title>
</head>
<body>
<h2>RSS Search</h2>
<form form action="rss.php" method="post">
<input type='text' name='name' value='' />
<input type='submit'>
</form>
</body>
</html>
RSS源搜索
RSS搜索
rss.php:
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['name'] ) ){
$feed_url = ( $_POST['name'] );
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo '<ul>';
foreach($x->channel->item as $entry) {
echo '<li>';
echo '<a href="'.$entry->link.'" title="'.$entry->title.'" target="_blank">' . $entry->title . '</a>'; // output link & title
echo $entry->description; // return post content
echo '</li>';
}
echo "</ul>";
};
?>
type=“content”
<这可能是问题的根源。file\u get\u contents($\u POST)代码>-您需要指定$\u POST
中的哪个字段用作文件名。类型应为URL,因为我正在传递URL,但我希望它发布或嵌入自身,以便在我转到实际RSS URL时显示,但我只能使用实时服务器执行此操作,因此这可能是问题的一半,因为本地主机不是在互联网上。我需要告诉php文件它使用的是什么文件?这很有帮助,因为它为我提供了一个开始的位置,我认为需要XML,但不确定谢谢。如果我找到了如何通过表单实现这一点,我会将其重新发布到问题中。呃,这是通过表单实现的“
[为了本演示的目的,我只是将一个值硬编码到表单字段中]-在这种情况下,表单会将URL发布到同一页面,但可能是您的rssfeed.php
脚本。您的rssfeed.php
脚本可能会处理并显示?如果是,您可以使用上面的PHP代码作为rssfeed.PHP
脚本的基础…因此我在站点中将它们分开,但是这一个只显示文章标题的标题,您将使用什么来显示html中的整个提要?我在看W3学校,玩它,但到目前为止什么都没有。它位于,要将文件\u获取\u内容
与SSL启用链接(不是全部)一起使用,您需要使用该函数的上下文
参数并将其配置为SSL~这可能会有所帮助。