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.
Related
I'm using Simplepie to parse different RSS feeds, passing them to a Smarty template, and I need to return the attribute from each item line that in this example reads: NEWSX
<source url="http://whatever.url/"><![CDATA[NEWSX]]></source>
I have found the get_item_tags method will select the line and attribute having used the following:
$newssource = $item->get_item_tags('','source');
Here is my problem. I don't know how to attach each source to an item when using the following code (so that basically I can display the different source element each time alongside the usual title, link, description and so on):
$RSS = array();
foreach($items as $item){
$feed = $item->get_feed();
$tmp=array();
$newssource = $item->get_item_tags('','source');
echo $newssource[0]["data"];
if ($feed){
if ($enclosure = $item->get_enclosure()){
$tmp['title'] = $item->get_title();
$tmp['permalink'] = $item->get_permalink();
$tmp['thumbnail'] = $enclosure->get_thumbnail();
$tmp['description'] = $enclosure->get_description();
$tmp['image'] = $enclosure->get_link();
}
$tmp['date'] = $item->get_date('j M Y');
$tmp['content'] = $item->get_content();
$tmp['title'] = $item->get_title();
$tmp['link'] = $item->get_link();
$tmp['description'] = $item->get_description();
array_push($RSS, $tmp);
}
}
Can it be done? Thanks in advance for any help or advice.
So this is the solution:
$RSS = array();
foreach($items as $item){
$feed = $item->get_feed();
$tmp=array();
if ($feed){
$tmp['date'] = $item->get_date('j M Y, g:i a');
$tmp['content'] = $item->get_content();
$tmp['title'] = $item->get_title();
$tmp['link'] = $item->get_link();
$tmp['description'] = $item->get_description();
$tmp['source'] = $item->get_item_tags('','source')[0]["data"];
array_push($RSS, $tmp);
}
}
$smarty->assign( $params['assign'], $RSS );
And in the smarty template:
<div class="cont">
{$entry.title}
<br />
<span class="date">Published on: <strong>{$entry.date}</strong></span><br />
<span class="source">Via : <strong>{$entry.source}</strong></span><br />
</div>
I have a simple newsfeed which is pulling from an xml file which works great. The news feed is broken down into separate XML files for each year. I want the page to show only one year at a time with links to other years above. Ideally when a new year is clicked I would like the content below to update without going to a new page.
I am trying to avoid repeating the PHP code which is parsing the XML for each year and instead would like to simply update the $dom_object->load source when the user clicks a different year.
I am a PHP newbie so some help would be appreciated!
<?php
$dom_object = new DOMDocument();
$dom_object->load("http://EXAMPLE.com/XML-Feed-10100524539?year=2013");
$item = $dom_object->getElementsByTagName("item");
foreach( $item as $value )
{
$titles = $value->getElementsByTagName("title");
$title = $titles->item(0)->nodeValue;
$pubDates = $value->getElementsByTagName("pubDate");
$pubDate = $pubDates->item(0)->nodeValue;
$pdf_urls = $value->getElementsByTagName("pdf_url");
$pdf_url = $pdf_urls->item(0)->nodeValue;
//Trims after last space - REMOVES EST
$pubDate=substr($pubDate, 0, strrpos($pubDate, ' '));
//Trims after remaining space - REMOVES TIME
$pubDate=substr($pubDate, 0, strrpos($pubDate, ' '));
$pubDater = str_replace('/', '-', $pubDate);
$newDate = DateTime::createFromFormat("m/d/Y", $pubDate);
$newDate = $newDate->format('F d, Y');
echo "<div style=\"width:33%; float:left; display:inline-block; height:150px;\"><div style=\"padding:10px;\"><h4>$newDate</h4><p>$title</p></div></div>";
}
?>
You would need to use ajax by using $.get or $.post so you could do something like:
$('#yourElement').click(function(){
var path = $('#path'); // for example
$.post("your/path/getXML.php", { path: path },
function (dat) {
console.log(dat);
}
);
});
Where getXML would be a file similar to this one:
$dom_object = new DOMDocument();
$dom_object->load($_POST["path"]);
$item = $dom_object->getElementsByTagName("item");
foreach( $item as $value )
{
$titles = $value->getElementsByTagName("title");
$title = $titles->item(0)->nodeValue;
$pubDates = $value->getElementsByTagName("pubDate");
$pubDate = $pubDates->item(0)->nodeValue;
$pdf_urls = $value->getElementsByTagName("pdf_url");
$pdf_url = $pdf_urls->item(0)->nodeValue;
//Trims after last space - REMOVES EST
$pubDate=substr($pubDate, 0, strrpos($pubDate, ' '));
//Trims after remaining space - REMOVES TIME
$pubDate=substr($pubDate, 0, strrpos($pubDate, ' '));
$pubDater = str_replace('/', '-', $pubDate);
$newDate = DateTime::createFromFormat("m/d/Y", $pubDate);
$newDate = $newDate->format('F d, Y');
echo "<div style=\"width:33%; float:left; display:inline-block; height:150px;\"><div style=\"padding:10px;\"><h4>$newDate</h4><p>$title</p></div></div>";
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++;
}
} ?>
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 />';
}
I have a fetch function that injects rss content into a page for me. This returns an xml which contains the usual RSS elements like title, link, description but the problem is the returned description is a table with two tds which one contains an image the other the text. I am not sure how I can remove the table, img and the tds and be left only with the text using php and not javascript.
Any help is much appreciated.
<?php
require_once('rss_fetch.inc');
$url = 'http://www.domain.com/rss.aspx?typeid=0&imagesize=120&topcount=20';
if ( $url ) {
$rss = fetch_rss( $url );
//echo "Channel: " . $rss->channel['title'] . "<p>";
echo "<ul>";
foreach ($rss->items as $item) {
$href = $item['link'];
$title = $item['title'];
$description = $item['description'];
$pubdate = date('F dS, Y', strtotime($item['pubdate']));
echo "<li><h3>$title<em>$pubdate</em></h3>$description <p><a href='$href' target='_blank'>ادامه مطلب</a></p><br/></li>";
}
echo "</ul>";
}
?>
strip_tags() will do the job..