I'm using the following PHP code to get the comments for a specific video:
<?php
$vid = "G0k3kHtyoqc";
$feedURL = 'http://gdata.youtube.com/feeds/api/videos/' . $vid;
$entry = simplexml_load_file($feedURL);
$gd = $entry->children('http://schemas.google.com/g/2005');
if($gd->comments->feedLink){
$attrs = $gd->comments->feedLink->attributes();
$commentsURL = $attrs['href'];
$commentsCount = $attrs['countHint'];
}
if($commentsURL && $commentsCount > 0){
$commentsFeed = simplexml_load_file($commentsURL);
echo "<ol>";
foreach($commentsFeed->entry as $comment){
echo "<li>";
echo "<a target='_blank' href='http://www.youtube.com/user/" . $comment->author->name . "'>";
echo $comment->author->name;
echo "</a>";
echo " - " . $comment->content;
echo "</li>";
}
echo "</ol>";
}
?>
The problem with the code above is that it only gets the most recent 24 comments. I need a way to paginate through all comments.
Your help is much appreciated.
Thanks
Use the "start-index" param. Begins on 1, and depending on the comments count, add [comments-count] to start-index param.
For example:
First page of comments, getting 25 comments per page, use max-results=25 and start-index=1
Second page of comments, getting 25 comments per page, use max-results=25 and start-index=26
And so on =)
Regards!
Related
I am almost completely on my own trying to solve this problem, know next to nothing about PHP and am in desperate need of help. A Wordpress news site redesign has images included in the description tag of the feed, we already had a way set up to pull those images so now the feed on other sites is seeing double. I want to turn off the image being pulled in the description tag and I'm having trouble figuring out how to accomplish that.
I have tried a few things, like strip_tag, shuffling code/HTML, etc., but I suspect most of my problem is not knowing where or how to implement them.
if ($rss = simplexml_load_file($feed)){ // (replace relative path with variable: $rssPath ).
$count = 0;
foreach($rss->channel->item as $item){
if ($count < $items) {
/*Set variables from RSS*/
$title = $item->title;
$desc = $item->description;
$content = htmlspecialchars_decode($item->children('content', true)->encoded);
preg_match('/(<img[^>]+>)/i', $content,
$content_image);
//if(strstr($content, 'img')){
// $content_trunc =
substr($content,0,strpos($content,'</p>', 550)) . "...";
//}
//else {
//$content_trunc = substr($content,0,strpos($content,'</p>', 400)) .
"...";
//}
$content_image = str_replace('class="',
'class="media-object ', $content_image);
$link = $item->link;
$pubdate = $item->pubDate;
$pubMonth = substr($pubdate, 8, 3);
$pubDay = substr($pubdate, 5, 2);
/*Display items*/
$output= "<div class='row'>\n<div class='col-md-3'>";
$output = $output .
" <a href='". $link ."'>"
. $content_image[0] .
" </div>
<div class='col-md-9'><span class='news-headline' style='vertical-align:top;'>" . $title . "</span>
</a>" . "\n
<p class='news-blurb'>" . $desc . "</p>
</div>\n</div>\n <hr />";
echo $output;
$count++;
}
}
This is the description tag, in short:
<description><![CDATA[<img width="150" height="150" src="http://placehold.it/150.150" class="attachment-thumbnail size-thumbnail wp-post-image" alt="" />news article description here [...]]]></description>
Please help! This is a language I want to learn, but I'm a long way from it yet. All I know is that I need to filter content out of $desc. but I don't know how.
Try preg_replace("/<img[^>]+\>/i", "", $desc); after $desc = $item->description; line.
i am using the following legacy code to pull post type GENRE and display the records on a wordpress site template file.
<?php
$genre_ids = get_posts('fields=ids&posts_per_page=-1&post_status=publish&post_type=genre&order=asc&orderby=title');
$choices = array(array('text' => 'Select Genre From List', 'value' => 0 ));
foreach ( $genre_ids as $genre_id ) {
$categories[] = get_the_title( $genre_id );
}
if (!empty($categories)){
foreach ($categories as $i=>$cat) {
print "<li>";
print "<a href='" . get_site_url() . "/band-search-page/?cat=$cat'>" . $cat . "</a>";
print "</li>";
}
} else {
print "<li>";
print "no categories";
print "</li>";
}
the script works now to say if there is any post type GENRE then run the query. but i want to edit it to check and show the GENRE only if there is a GENRE. right now the query is showing all categories even if there is no data. any help would be greatly appreciated.
Please try this code::
<?php
$genre_ids = get_posts('fields=ids&posts_per_page=-1&post_status=publish&post_type=genre&order=asc&orderby=title');
$choices = array(array('text' => 'Select Genre From List', 'value' => 0));
foreach ($genre_ids as $genre_id)
{
$categories[] = get_the_title($genre_id);
}
if (!empty($categories))
{
foreach ($categories as $i => $cat)
{
if (!empty($cat))
{
print "<li>";
print "<a href='" . get_site_url() . "/band-search-page/?cat=$cat'>" . $cat . "</a>";
print "</li>";
}
}
}
else
{
print "<li>";
print "no categories";
print "</li>";
}
?>
Try to replace this line:
print "<a href='" . get_site_url() . "/band-search-page/?cat=$cat'>" . $cat . "</a>";
With this line
print "<a href='" . get_site_url() . "/band-search-page/?cat=$cat'>" . array_values(array_filter($cat) . "</a>";
Just add array_filter($cat) to remove empty array elements.
Don't know if I understand you correctly, but I'm also working on a site where I only want to show the category name that has posts stored in it. I just checked it by using if(category_count > 0).
Well, you might want to set the category and call it using get_categories(), loop through it then you can do category->count to check for the count.
hope this might help... someone.. :)
I cant get this to work, is it possible? I need to display an banner ad after every 25 wallpapers...
<?php
$xml=simplexml_load_file("wallpaper.xml") or die("Error: Cannot create object");
foreach (array_chunk($xml->children, 25, true) as $array) {
foreach ($array as $wall)
{
echo "<a href='" . $wall->url . "' target='_blank'><img src='"$wall->thumbnail . "' alt='Wallpaper' /></a> \n";
}
echo '<div id="banner">Test</div>';}
?>
When I write things like this I use the $i = 0 thing. Then I use the $i = count++ after each foreach. Then I do the if $i = 25 { echo 'img banner code' }.
This above (unfinished) code counts the number of foreach iterations, and when it has reached 25 it echoes your banner images.
Is the code you want? If so I might finish it for you.
I have an rss feed that I am reading into. I need to retrieve certain data from the field in this feed.
This is the example feed data :
<content:encoded><![CDATA[
<b>When:</b><br />
Weekly Event - Every Thursday: 1:30 PM to 3:30 PM (CT)<br /><br />
<b>Where:</b><br />
100 West Street<BR>2nd floor<BR>Gainesville<BR>
<br>.....
How do I pull out the data for When: and Where: respectively? I attempted to use regex but I am unsure if I am not accessing the data correctly or if my regex expression is wrong. I'm not set on using regex.
This is my code:
foreach ($x->channel->item as $event) {
$eventCounter++;
$rowColor = ($eventCounter % 2 == 0) ? '#FFFFFF' : '#F1F1F1';
$content = $event->children('http://purl.org/rss/1.0/modules/content/');
$contents = $content->encoded;
echo '<tr style="background-color:' . $rowColor . '">';
echo '<td>';
//echo "<a id=buttonRed href='$event->link' title='$event->title' target='_blank'>" . $event->title . "</a>";
echo "" . $event->title . "";
echo '</td>';
echo '<td>';
$re = '%when\:\s*</b>\s*(.|\s)<br \/><br \/>$/i';
if (preg_match($re, $contents, $matches)) {
$date = $matches;
}
echo $date;
echo '</td>';
echo '<td>';
$re = '/^When\:<\/b>()$/';
if (preg_match($re, $contents, $matches)) {
$location = $matches;
}
echo $location;
echo '</td>';
echo '<td>';
echo "<a id=buttonRed href='$event->link' title='$event->title' target='_blank'>Click Here To Register</a>";
echo '</td>';
echo '</tr>';
}
The two $res are just my attempt to get the data out using different regex expressions. Let me know where I am going wrong. Thanks
The following should sort of get you there. (I wrote this from the top of my head and it does not exactly following your XML syntax. But you get the idea.)
<?php
$str = "<root><b>When:</b> whenwhen <b>Where:</b> wherewhere</root>";
$doc = new DOMDocument();
$doc->loadXML($str);
$when = $where = "";
$target = null;
foreach ($doc->documentElement->childNodes as $node) {
if ($node->tagName == "b") {
if (++$i == 1) {
$target = &$when;
} else {
$target = &$where;
}
}
if ($target !== null && $node->nodeType === XML_TEXT_NODE) {
$target .= $node->nodeValue;
}
}
var_dump($when, $where);
I had a problem like this and I ended up using YQL. Take a good look at the page-scraping code given there, especially the select command. Then go the the console and put in your own select statement, specifying the feed url and the xpath to the nodes you're wanting. Select JSON format. Then go down to the bottom of the page, get the REST query url, and use it in a jquery jsonp request. MAGIC!
please, don't extract data from XML-documents via regex.
The long answer is e.g. here: https://stackoverflow.com/a/335446/313145
The short answer is: it is not easier to use regex and will break often.
What i want to do is read rssfeed, so I already did it, but I display as foreach loop, so how can I only display 5 records ? now I get more than 10 records, but I only need top 5 records, Isn't anyway php, javascript or jquery make it only show 5 records?
here is my code to read the rss file:
function getrssFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo "<ul>";
foreach($x->channel->item as $entry) {
echo "<li><a href = '$entry->link' title='$entry->title'><h3>" . $entry->title . "</h3></a>" . $entry->pubDate . "<br /><br />" . strip_tags($entry->description) . "</li>";
}
echo "</ul>"; }
getrssFeed("http://thestar.com.my.feedsportal.com/c/33048/f/534555/index.rss");
thank you
the easiest way would be to stop your loop after 5 iterations:
$i = 0;
foreach($x->channel->item as $entry) {
// do something
$i++;
if($i==5){
break;
}
}
another (more beautiful) way would be to use a for-loop instead of foreach:
for($i=0; $i<=min(5, count($x->channel->item)); $i++) {
$entry = $x->channel->item[$i];
// do something
}
EDIT :
thanks to Juhana, i changed the code to take that into account.
Try putting a counter in your foreach loop with an if statement to check when the counter is over 5. If the counter is under 5 -> display RSS post, counter++. Else -> Exit loop.
Why not use a simple for loop instead?
for($i = 0, $i <= 5, $i++) {
echo "<li><a href = '$x->channel->item[$i]->link' title='$x->channel->item[$i]->title'><h3>" . $x->channel->item[$i]->title . " </h3></a>" . $x->channel->item[$i]->pubDate . "<br /><br />" . strip_tags$x->channel->item[$i]->description) . "</li>";
}
You can use jQuery feed Plugin.
$('div.feed').Feed({
count:5;
});
<div class="feed" link="http://thestar.com.my.feedsportal.com/c/33048/f/534555/index.rss" ></div>
Feeds would be loaded to container feed.