I'm calling in a RSS feed to my website using PHP. Currently my code below is calling in the entire contents for pubDate:
<pubDate>Thu, 12 Sep 2013 07:23:59 +0000</pubDate>
How do I just display the day and month from the above example i.e. 12 Sep?
EDIT
I should clarify, the above line of code is an example output I currently get but as I'm calling the latest 3 posts from an RSS feed, this date and time will vary. I therefore need the code to be more dynamic (if that's the right term!)
This code is my full code that fetches the contents of an RSS feed:
<?php
$counter = 0;
$xml=simplexml_load_file("http://tutorial.world.edu/feed/");
foreach ($xml->channel->item as $item) {
$title = (string) $item->title; // Title Post
$link = (string) $item->link; // Url Link
$pubDate = (string) $item->pubDate; // date
$description = (string) $item->description; //Description Post
echo '<div class="display-rss-feed"><a href="'.$link.'" target="_blank" title="" >'.$title.' </a><br/><br/>';
echo $description.'<hr><p style="background-color:#e4f;">'.$pubDate.'</p></div>';
if($counter == 2 ) {
break;
} else {
$counter++;
}
} ?>
Use strtotime and date:
$pubDate = 'Thu, 12 Sep 2013 07:23:59 +0000';
$pubDate = date('j M', strtotime($pubDate)); //This is the only one you need!
var_dump($pubDate); //string(6) "12 Sep"
You can parse the date using date_parse and then use the values of month and day in the resulting array.
you can use preg_match() function with desired regular express to fetch particular data.
for example
$content="Thu, 12 Sep 2013 07:23:59 +0000";
preg_match("/.*,(. *)20[0-9][0-9]/"," $content",$g_val) ;
$g_val[1] would have " 12 Sep"
Even this works
<?php
$str="<pubDate>Thu, 12 Sep 2013 07:23:59 +0000</pubDate>";
$str=explode(" ",$str);
echo $str[1]." ".$str[2];//12 Sep
EDIT:
<?php
$counter = 0;
$xml=simplexml_load_file("http://tutorial.world.edu/feed/");
foreach ($xml->channel->item as $item) {
$title = (string) $item->title; // Title Post
$link = (string) $item->link; // Url Link
$pubDate = (string) $item->pubDate; // date
$pubDate=explode(" ",$pubDate);
$pubDate = $pubDate[1]." ".$pubDate[2];
$description = (string) $item->description; //Description Post
echo '<div class="display-rss-feed"><a href="'.$link.'" target="_blank" title="" >'.$title.' </a><br/><br/>';
echo $description.'<hr><p style="background-color:#e4f;">'.$pubDate.'</p></div>';
if($counter == 2 ) {
break;
} else {
$counter++;
}
} ?>
Related
I am working on a small script where I wanna take only my website's latest post which is posted yesterday mean I wanna get all yesterday's links and titles.
I tried with my script but I am getting all URLs I am not sure how can I fix it.
Can anyone help me solve this problem?
I was wondering if I can use 'where' attribute like we usually use in SQL. I want only 1 days posts to be scraped.
<?php
header('Content-Type: application/json');
$url = "https://www.lifegoals.co.in/feed/";
$i=0;
$invalidurl = false;
if(#simplexml_load_file($url)){
$feeds = simplexml_load_file($url);
}else{
$invalidurl = true;
echo "<h2>Invalid RSS feed URL.</h2>";
}
if(!empty($feeds)){
//$site = $feeds->channel->title;
//$sitelink = $feeds->channel->link;
//echo "<h1>".$site."</h1>";
foreach ($feeds->channel->item as $item) {
$title = $item->title;
$link = $item->link;
//$description = $item->description;
$postDate = $item->pubDate;
$pubDate = date('D, d M Y',strtotime($postDate));
$currDate = date('D, d M Y');
if($i>=10) break;
if($pubDate=$currDate){
$rss = "<item>
<title>$title</title>
<link>$link</link>
</item>";
echo $rss;
$i++;
}
}
}
?>
i want only 1 days posts there are 4 days posts
I'd add some debugging to this to ensure that you're getting what you think you want. Try the following in your foreach loop:
print_r([
$postDate,
$pubDate,
$currDate,
($pubDate == $currDate),
]);
if($pubDate==$currDate){
$rss = "<item>
<title>$title</title>
<link>$link</link>
</item>";
echo $rss;
$i++;
}
The == was missing thanks.
I want to adjust the timezone of the twitter api to my local timezone. Right now it displays:
Thu Apr 03 14:34:29 +0000 2014
However, here it is
16:34:29 (GMT+2).
Code:
if(isset($_POST['keyword']))
{
$url = 'https://api.twitter.com/1.1/search/tweets.json?q='.$_POST['keyword']
.'&lang=nl&result_type=recent&count=25';
$tweets = $twitter->get($url);
foreach($tweets as $tweet)
{
foreach ($tweet as $t)
{
echo '<div class = "twitterText">';
echo '<img src = "'.$t->user->profile_image_url.'"/>'.'<br />'
.$t->user->name.'<br />'
.$t->created_at.'<br /><br />'
.'<p class = "description">'.$t->text.'</p>'.'<br />';
echo '</div>';
}
}
}
I figured it out myself, working code is:
// ...
foreach ($tweet as $t) {
$date = new DateTime($t->created_at);
$date->setTimezone(new DateTimeZone('Europe/Amsterdam'));
$formatted_date = $date->format('H:i, M d');
echo $formatted_date;
}
Using the Tweet timestamp_ms JSON attribute is so much easier than using created_at.
I am trying to return twitter titles based on today's date only. I have made the following code below, but it returns every title no matter if its today's date or not.
$dom = new DOMDocument();
#$dom->loadHTMLFile('http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=google');
$xml = simplexml_import_dom($dom);
$twitter = $xml->xpath("//item");
foreach ($twitter as $item) {
$timezone = new DateTimeZone('America/Los_Angeles');
$date = new DateTime($item->pubdate);
$date->setTimeZone($timezone);
$twitter_date = $date->format("F j Y");
$todays_date = date("F j Y");
if ($twitter_date == $todays_date) {
foreach ($twitter as $item) {
$text = $item->title;
echo $text.'<br />';
}
}
}
You are looping again through EVERY $twitter inside the if statement. Try removing the foreach tag inside and just using the current $item:
if ($twitter_date == $todays_date) {
$text = $item->title;
echo $text.'<br />';
}
Here is my code:
<?php
$RSSFEEDS = array(
0 => "http://samnabi.posterous.com/rss.xml",
);
function FormatRow($date, $title, $link, $description) {
return <<<HTML
<p class="blogdate">$date</p><h2 class="blogtitle">$title</h2>
<div class="clearer"> </div>
$description
HTML;
}
ob_start();
if (!isset($feedid)) $feedid = 0;
$rss_url = $RSSFEEDS[$feedid];
$rss_feed = file_get_contents($rss_url);
$rss_feed = str_replace("<![CDATA[", "", $rss_feed);
$rss_feed = str_replace("]]>", "", $rss_feed);
$rss_feed = str_replace("\n", "", $rss_feed);
$rss_feed = preg_replace('#<image>(.*?)</image>#', '', $rss_feed, 1 );
preg_match_all('#<pubDate>(.*?)</pubDate>#', $rss_feed, $date, PREG_SET_ORDER);
preg_match_all('#<title>(.*?)</title>#', $rss_feed, $title, PREG_SET_ORDER);
preg_match_all('#<link>(.*?)</link>#', $rss_feed, $link, PREG_SET_ORDER);
preg_match_all('#<description>(.*?)</description>#', $rss_feed, $description, PREG_SET_ORDER);
if(count($title) <= 1) {
echo "No new blog posts. Check back soon!";
}
else {
for ($counter = 1; $counter <= 3; $counter++ ) {
if(!empty($title[$counter][1])) {
$title[$counter][1] = str_replace("&", "&", $title[$counter][1]);
$title[$counter][1] = str_replace("'", "'", $title[$counter][1]);
$row = FormatRow($date[$counter][1],$title[$counter][1],$link[$counter][1],$description[$counter][1]);
echo $row;
}
}
}
ob_end_flush();
?>
When this script is run, the first item displays the second item's pubDate. The second item displays the third item's pubDate, and so on. So the dates that are shown are not the dates that you see in the original XML file. How do I fix this?
Bonus question: how do I strip characters off the beginning and end of the pubDate tag, so that I end up with "15 May 2010" instead of "Sat, 15 May 2010 03:28:00 -0700" ?
I've said it before, so I'll say it again: Use Magpie RSS to parse your RSS feeds. It takes care of all this stuff for you, and will be much more reliable.
Magpie RSS works great. Here's the code I used to replace what was in my original question:
<?php
define('MAGPIE_INPUT_ENCODING', 'UTF-8');
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
//Tell it to use the fetch script to grab the RSS feed
require_once('magpie/rss_fetch.inc');
//Now it knows how to fetch RSS, tell it which one to fetch
$rss = fetch_rss('http://samnabi.posterous.com/rss.xml');
//In this case, we only want to display the first 3 items
$items = array_slice($rss->items,0,3);
//Now we tell Magpie how to format our output
foreach ($items as $item) {
$title = $item['title'];
$date = date('d M Y', strtotime($item['pubdate']));
$link = $item['link'];
$description = $item['description'];
//And now we want to put it all together.
echo "<p>$date</p><h2>$title</h2><p>$description</p>";
}
?>
Having some trouble selecting some nodes in the rss feed for twitter's search
the rss url is here
http://search.twitter.com/search.rss?q=twitfile
each item looks like this
<item>
<title>RT #TwittBoy: TwitFile - Comparte tus archivos en Twitter (hasta 200Mb) http://bit.ly/xYNsM</title>
<link>http://twitter.com/MarielaCelita/statuses/5990165590</link>
<description>RT <a href="http://twitter.com/TwittBoy">#TwittBoy</a>: <b>TwitFile</b> - Comparte tus archivos en Twitter (hasta 200Mb) <a href="http://bit.ly/xYNsM">http://bit.ly/xYNsM</a></description>
<pubDate>Mon, 23 Nov 2009 22:45:39 +0000</pubDate>
<guid>http://twitter.com/MarielaCelita/statuses/5990165590</guid>
<author>MarielaCelita#twitter.com (M.Celita Lijerón)</author>
<media:content type="image/jpg" width="48" height="48" url="http://a3.twimg.com/profile_images/537676869/orkut_normal.jpg"/>
<google:image_link>http://a3.twimg.com/profile_images/537676869/orkut_normal.jpg</google:image_link>
</item>
My php is below
foreach ($twitter_xml->channel->item as $key) {
$screenname = $key->{"author"};
$date = $key->{"pubDate"};
$profimg = $key->{"google:image_link"};
$link = $key->{"link"};
$title = $key->{"title"};
echo"
<li>
<h5><a href=$link>$author</a></h5>
<p class=info><a href=$link>$title</a></p>
</li>
";
Problem is nothing is being echoed, i mean from the rss feed, if there are 20 results, its looping 20 times, just no data
In the code, $screenname is assigned a value but you are echoing $author.
To get elements within namespaces like google:image_link ,you will have to do this:
$g = $key->children("http://base.google.com/ns/1.0");
$profimg = $g->{"image_link"};
If you are wondering where did I get "http://base.google.com/ns/1.0" from, the namespace is mentioned in the second line of the rss feed.
$url="http://search.twitter.com/search.rss?q=twitfile";
$twitter_xml = simplexml_load_file($url);
foreach ($twitter_xml->channel->item as $key) {
$author = $key->{"author"};
$date = $key->{"pubDate"};
$link = $key->{"link"};
$title = $key->{"title"};
$g = $key->children("http://base.google.com/ns/1.0");
$profimg = $g->{"image_link"};
echo"
<li>
<h5><a href=$link>$author</a></h5>
<p class=info><a href=$link>$title</a></p>
</li>
";
$xml = $twitter_xml;
}
This code works.
Set error_reporting(E_ALL); and you'll see that $author isn't defined.
You can't access <google:image_link/> this way, you'll have to use XPath or children()
$key->children("google", true)->image_link;
If you use SimpleDOM, there's a shortcut that returns the first element of an XPath result:
$key->firstOf("google:image_link");
if (!$xml = simplexml_load_file('http://search.twitter.com/search.atom?q='.urlencode ($terms)))
{
throw new RuntimeException('Unable to load or parse search results feed');
}
if (!count($entries = $xml->entry))
{
throw new RuntimeException('No entry found');
}
for($i=0;$i<count($entries);$i++)
{
$title[$i] = $entries[$i]->title;
//etc.. continue description,,,,,
}
I made this and it works :)) $sea_name is the keyword your looking for...
<?php
function twitter_feed( $sea_name ){
$endpoint = 'http://search.twitter.com/search.rss?q='.urlencode($sea_name); // URL to call
$resp = simplexml_load_file($endpoint);
// Check to see if the response was loaded, else print an error
if ($resp) {
$results = '';
$counter=0;
// If the response was loaded, parse it and build links
foreach($resp->channel->item as $item) {
//var_dump($item);
preg_match("/\((.*?)\)/", $item->author, $blah);
$content = $item->children("http://search.yahoo.com/mrss/" );
$imageUrl = getXmlAttribute( $content, "url" );
echo '
<div class="twitter-item">
<img src="'.$imageUrl.'" />
<span class="twit">'.$blah[1].'</span><br />
<span class="twit-content">'.$item->title.'</span>
<br style="clear:both; line-height:0;margin:0;padding:0;">
</div>';
$counter++;
}
}
// If there was no response, print an error
else {
$results = "Oops! Must not have gotten the response!";
}
echo $results;
}
function getXmlAttribute( SimpleXMLElement $xmlElement, $attribute ) {
foreach( $xmlElement->attributes() as $name => $value ) {
if( $name == $attribute ) {
return (string)$value;
}
}
}
?>
The object will contain somthing like:
<!-- SimpleXMLElement Object
(
[title] => Before I go to bed, I just want to say I've just seen Peter Kay's CIN cartoon video for the 1st time... one word... WOW.
[link] => http://twitter.com/Alex_Segal/statuses/5993710015
[description] => Before I go to bed, I just want to say I've just seen <b>Peter</b> <b>Kay</b>'s CIN cartoon video for the 1st time... one word... WOW.
[pubDate] => Tue, 24 Nov 2009 01:00:00 +0000
[guid] => http://twitter.com/Alex_Segal/statuses/5993710015
[author] => Alex_Segal#twitter.com (Alex Segal)
)
-->
You can use any of it inside the foreach look and echo them such as $item->author, $item->link, etc....any other attributes you can use the getattribute function...