Created
September 25, 2013 22:29
-
-
Save krutz27/6706969 to your computer and use it in GitHub Desktop.
reads RSS feed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//get the q parameter from URL | |
$q=$_GET["q"]; | |
//find out which feed was selected | |
if($q=="Google") | |
{ | |
$xml=("http://news.google.com/news?ned=us&topic=h&output=rss"); | |
} | |
elseif($q=="MSNBC") | |
{ | |
$xml=("http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml"); | |
} | |
$xmlDoc = new DOMDocument(); | |
$xmlDoc->load($xml); | |
//get elements from "<channel>" | |
$channel=$xmlDoc->getElementsByTagName('channel')->item(0); | |
$channel_title = $channel->getElementsByTagName('title') | |
->item(0)->childNodes->item(0)->nodeValue; | |
$channel_link = $channel->getElementsByTagName('link') | |
->item(0)->childNodes->item(0)->nodeValue; | |
$channel_desc = $channel->getElementsByTagName('description') | |
->item(0)->childNodes->item(0)->nodeValue; | |
//output elements from "<channel>" | |
echo("<p><a href='" . $channel_link | |
. "'>" . $channel_title . "</a>"); | |
echo("<br>"); | |
echo($channel_desc . "</p>"); | |
//get and output "<item>" elements | |
$x=$xmlDoc->getElementsByTagName('item'); | |
for ($i=0; $i<=2; $i++) | |
{ | |
$item_title=$x->item($i)->getElementsByTagName('title') | |
->item(0)->childNodes->item(0)->nodeValue; | |
$item_link=$x->item($i)->getElementsByTagName('link') | |
->item(0)->childNodes->item(0)->nodeValue; | |
$item_desc=$x->item($i)->getElementsByTagName('description') | |
->item(0)->childNodes->item(0)->nodeValue; | |
echo ("<p><a href='" . $item_link | |
. "'>" . $item_title . "</a>"); | |
echo ("<br>"); | |
echo ($item_desc . "</p>"); | |
} | |
?> | |
<html> | |
<head> | |
<script> | |
function showRSS(str) | |
{ | |
if (str.length==0) | |
{ | |
document.getElementById("rssOutput").innerHTML=""; | |
return; | |
} | |
if (window.XMLHttpRequest) | |
{// code for IE7+, Firefox, Chrome, Opera, Safari | |
xmlhttp=new XMLHttpRequest(); | |
} | |
else | |
{// code for IE6, IE5 | |
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); | |
} | |
xmlhttp.onreadystatechange=function() | |
{ | |
if (xmlhttp.readyState==4 && xmlhttp.status==200) | |
{ | |
document.getElementById("rssOutput").innerHTML=xmlhttp.responseText; | |
} | |
} | |
xmlhttp.open("GET","getrss.php?q="+str,true); | |
xmlhttp.send(); | |
} | |
</script> | |
</head> | |
<body> | |
<form> | |
<select onchange="showRSS(this.value)"> | |
<option value="">Select an RSS-feed:</option> | |
<option value="Google">Google News</option> | |
<option value="MSNBC">MSNBC News</option> | |
</select> | |
</form> | |
<br> | |
<div id="rssOutput">RSS-feed will be listed here...</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// define variables and set to empty values | |
$nameErr = $emailErr = $genderErr = $websiteErr = ""; | |
$name = $email = $gender = $comment = $website = ""; | |
if ($_SERVER["REQUEST_METHOD"] == "POST") | |
{ | |
if (empty($_POST["name"])) | |
{$nameErr = "Name is required";} | |
else | |
{ | |
$name = test_input($_POST["name"]); | |
// check if name only contains letters and whitespace | |
if (!preg_match("/^[a-zA-Z ]*$/",$name)) | |
{ | |
$nameErr = "Only letters and white space allowed"; | |
} | |
} | |
if (empty($_POST["email"])) | |
{$emailErr = "Email is required";} | |
else | |
{ | |
$email = test_input($_POST["email"]); | |
// check if e-mail address syntax is valid | |
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) | |
{ | |
$emailErr = "Invalid email format"; | |
} | |
} | |
if (empty($_POST["website"])) | |
{$website = "";} | |
else | |
{ | |
$website = test_input($_POST["website"]); | |
// check if URL address syntax is valid (this regular expression also allows dashes in the URL) | |
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) | |
{ | |
$websiteErr = "Invalid URL"; | |
} | |
} | |
if (empty($_POST["comment"])) | |
{$comment = "";} | |
else | |
{$comment = test_input($_POST["comment"]);} | |
if (empty($_POST["gender"])) | |
{$genderErr = "Gender is required";} | |
else | |
{$gender = test_input($_POST["gender"]);} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment