Ordering content from the YouTube API with PHP & XML - php

I've got a nice little web app working that brings down playlists and videos from YouTube using the API. However, YouTube seems to fire back the playlists in the wrong order.
Any idea how I can do that?
Here is my very simple PHP code that does everything I need it to do (apart from date ordering)
<?php
// set feed URL
$feedURL = 'http://gdata.youtube.com/feeds/api/users/popcorncomedy/playlists?v=2';
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
?>
<div class="container">
<h1><?php echo $sxml->title; ?></h1>
<?php
// iterate over entries in feed
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get <yt:duration> node for video length
$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
?>
<a href="videos.php?playlist=<?php echo $yt->playlistId; ?>"><div class="item">
<span class="title"><?php echo $entry->title; ?></span><br /><br />
<span class="summary"><?php echo $entry->summary; ?></span>
</div></a>
<?php
} // closes the loop
?>
The XML element that contains the date is called: published in the date format: 2010-03-03T17:37:34.000Z
Any thoughts on how to achieve this simply?
I can't find a function within the API itself that orders content for you.

What you want is the orderby parameter http://code.google.com/apis/youtube/2.0/developers_guide_protocol_api_query_parameters.html#orderbysp however I haven't gotten it to work for playlists yet. As far as I can find its supports work with palylists but I never seen it work, hopefully you will have better luck!

Related

PHP RSS Feed Reader Data filtering problem

So, I wrote an RSS feed Reader in PHP and I am almost done(I think) but I haven't quite got around the part where I can filter keywords. When I tried to fix it, it said that there is an "}" missing at the end of the file. I am not sure what to do with this problem, hence why I am here. I use Visual Studio Code and any help would be much appreciated.
Here is the code I wrote:
<?php
/*
* To access the localhost for this program type in the address bar: "localhost/feedreader" then
* click on RSSFeedReader.php when you install the two files by the names of "RSSFeedReader.php" and "RSSConfig.json" into the Sites
* folder in the Users section.
*/
?>
<div class="content">
<form method="post" action="">
<input type="text" name="feedurl" placeholder="Enter website RSS Link"> <input type="submit" value="Submit" name="submit">
</form>
<div class="content">
<form method="post" action="">
<input type="text" name="keyword" placeholder="Enter keyword here"> <input type="submit" value="Submit" name="submit">
</form>
<?php
/*
The piece of code above will create the box that says "Enter Website RSS Link" and the submit
button that will process through the code to search the inputted RSS link.
Line 1 will create the div class in which the content of the client side of things(Front-End),and the form POST method is a way of
retrieving information and data that is being inputted through the sumbit section
*/
if(isset($_POST['submit'])){
if($_POST['feedurl'] != ''){
$url = $_POST['feedurl'];
}
}
$invalidurl = false;
if(#simplexml_load_file($url)){
$feeds = (simplexml_load_file($url));
}
else{
$invalidurl = true;
echo "<h2>Invalid RSS Feed Link.
Please try a valid RSS Feed Link.
</h2>";
}
/*
This block of code above generally allows the screen to output "Invalid RSS Feed Link. Please try a valid RSS Feed Link."
when typing out a URL/XML link which is not an RSS Feed Link.
First, the program will assign the variable: $invalidurl to false and will load the simple xml file, however if the url turned out
to be invalid, the program will output "Invalid RSS Feed Link" to the user's screen.
The first line will assume that the is no invalid url link so it is assigned to be false, then the simplexml_load_file will load
up the xml files and if the url is a feed link, it will store the the link into the variable "$feeds". However, if the url is
not a feed link, then it will make the program assign the variable "$invalidurl" as true which will the return the string "Invalid
RSS Feed Link. Please try a valid RSS Feed Link" with a level 2 headline.
*/
$i=0;
if(!empty($feeds)){
$site = $feeds->channel->title;
$sitelink = $feeds->channel->link;
if(isset($_POST['submit'])){
if($_POST['keyword'] != ''){
string == $_POST['keyword'];
}
}
function parse($xml)
{
return [
'title' => (string) $xml->channel->title
];
}
}
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));
/*
The block of code from line 67 to line 71 represent how the program will extract the dtaa from the article in terms
of the title, a hyperlink, the description of the article, and the date that that the article was published(Day, Month and Year)
The first line sets the variable "$i" is set to zero which identifies future into the code how many results the program will output
onto the results page.
Then the next two lines of code will represents how the program will extract the title of the artcile by going though the page
source and into the feed area where the title is stored. It then gets the link to the article to make make the title a hyperlink.
the next piece of code from line 75 to 79 will store the title of the articel into the variable "$title" as an item. It will also
store the link of the article into the variable "$link" as an item. It will also store what the page source identitifies as the
description of the article into the variale "$description" as an item. It wil also the extract the date that the article was
published from the "pubDate" in the page source as the variable "$postDate". the $pubDate will store the postDate in terms of
days, months and years.
*/
if($i>=15) break;
?>
<div class="post">
<div class="post-head">
<h2><a class="feed_title" href="<?php echo $link; ?>"><?php echo $title; ?></a></h2>
<span><?php echo $pubDate; ?></span>
</div>
<div class="post-content">
<?php echo implode(' ', array_slice(explode(' ', $description), 0, 20)) . "..."; ?> Read more
</div>
</div>
<?php
$i++;
if(!$invalidurl){
echo "<h2>No item found</h2>";
}}
?>

how to get the private meta data from vimeo private video

Hi i cant able to fetch the private meta data like title, duration,image,etc... from the private access video's in Vimeo. Can any one help me to find a solution for this?
I uploaded one video in my account with private mode. I cant able to fetch the meta data details too.
I am using PHP to fetch the details.
This is very similar to: Get URL/Embed code to private Vimeo videos programatically
Unfortunately the answer there has not been marked as the answer, a necessary step to link the two questions together, so I'll repost it here.
Register an API app on https://developer.vimeo.com/apps
This is necessary for every API app. We need to know who is using our system, and how to contact them if necessary.
Generate an access token.
There is general documentation at https://developer.vimeo.com/api/authentication, but you will likely be using the "single user application" workflow. It's a lofty title for "generate an access token via the UI on your application page, then hard code it into your app". This access token will interact with the API on behalf of the user who registered the application.
Request your video information.
There are many different API calls to get video information. You can find these at https://developer.vimeo.com/api/endpoints. /me/videos will show all of the authenticated users videos, /videos/{video_id} will show a single video.
One extra note, if you are using PHP you should use the official Vimeo PHP library: https://github.com/vimeo/vimeo.php
Visite https://github.com/leandrocfe/PHPVimeoAPI_List_Private_Video
List private videos from Vimeo
Modify config.json info vimeo account;
Access video.php and
add vimeo_video_id get param. Ex:
localhost/vimeo/video.php?id=123123123
<?php
//utf-8
header('Content-Type: text/html; charset=utf-8');
//lib vimeo
use Vimeo\Vimeo;
//métodos de inicialização
$config = require(__DIR__ . '/init.php');
//vimeo video id
#$id = $_GET["id"];
//isset get
if(isset($id)){
// vimeo class send config.json paramns
$lib = new Vimeo($config['client_id'], $config['client_secret'], $config['access_token']);
//get data vimeo video
$me = $lib->request("/me/videos/$id");
//iframe vídeo
$embed = $me["body"]["embed"]["html"];
//edit video size
$default_size = 'width="'.$me["body"]["width"].'" height="'.$me["body"]["height"].'"';
$new_size = 'width="420" height="220"';
$embed = str_replace($default_size, $new_size, $embed);
//autoplay
$embed = str_replace('player_id=0', 'player_id=0&autoplay=1', $embed);
}else{
echo("Not find get id video");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Vimeo Vídeo</title>
</head>
<body>
<div><?php echo $embed ?></div>
<div>
<p><b>Name: </b><?php print_r($me["body"]["name"]); ?></p>
<p><b>Description: </b><?php print_r($me["body"]["description"]); ?></p>
<p><b>Link: </b><?php print_r($me["body"]["link"]); ?></p>
<p><b>Likes: </b><?php print_r($me["body"]["embed"]["buttons"]["like"]); ?></p>
<p><b>Data Created: </b><?php print_r($me["body"]["created_time"]); ?></p>
<p><b>Data Modified: </b><?php print_r($me["body"]["modified_time"]); ?></p>
<p><b>Images: </b>
<?php print_r($me["body"]["pictures"]["uri"]); ?> |
<?php print_r($me["body"]["pictures"]["sizes"][0]["link"]); ?> |
<?php print_r($me["body"]["pictures"]["sizes"][1]["link"]); ?> |
<?php print_r($me["body"]["pictures"]["sizes"][2]["link"]); ?> |
<?php print_r($me["body"]["pictures"]["sizes"][3]["link"]); ?> |
<?php print_r($me["body"]["pictures"]["sizes"][4]["link"]); ?> |
<?php print_r($me["body"]["pictures"]["sizes"][5]["link"]); ?>
</p>
</div>
<div><?php //print_r($me); //use for show all options ?></div>
</body>
</html>

Instagram PHP API: getTagMedia only works locally

I'm working with the Instagram PHP API (https://github.com/cosenary/Instagram-PHP-API) and I have used a solution I found which have been working perfectly (see #kexxcream 's answer for the solution here: Get all photos from Instagram which have a specific hashtag with PHP) until now obviously.. I can only get it to work locally but not at my referred domain that I have put in at my Instagram app. I have no idea if it has to do with my domain-bindings; my company (like most companies) got a dev-server which I've then binded to my domain. Though this has not been a problem before when I've used this solution for grabbing an instagram feed.
Anyway, does anyone know what the problem could be?
Here's my code:
<?php
// Get class for Instagram
// More examples here: https://github.com/cosenary/Instagram-PHP-API
require_once 'instagram.class.php';
// Initialize class with client_id
// Register at http://instagram.com/developer/ and replace client_id with your own
$instagram = new Instagram('09b639fa2d2845d99d7c3c748f273b30');
// Set keyword for #hashtag
$tag = 'insta';
// Get latest photos according to #hashtag keyword
$media = $instagram->getTagMedia($tag);
// Set number of photos to show
$limit = 8;
// Show results
// Using for loop will cause error if there are less photos than the limit
foreach(array_slice($media->data, 0, $limit) as $data)
{
#print('<pre>'); print_r($data); print('</pre>');
?>
<div class="insta-item">
<a href="<?php echo $data->link; ?>" target="_blank">
<img src="<?php echo $data->images->standard_resolution->url; ?>" height="46px" width="46px" alt="Pic" />
</a>
</div>
<?php
}
?>
First, I would recommend you to check your Instagram parameters.
There's a known issue about the usage on localhost of the api.
If this still doesn't work, try adding a limit to the number of image loaded:
$media = $instagram->getTagMedia($tag, 10);

Echo image - PHP & JSON - Twitter API

New to PHP, and found a solid code that's able to retrieve JSON data from Twitter API and echo it out. Here's the PHP code that retrieves the user-pic of #Guardian (newspaper)
<?php
$data = json_decode(file_get_contents('https://api.twitter.com/1/users/lookup.json?screen_name=guardian'), true);
echo $data[0]['profile_image_url'];
?>
The JSON data for the image is:
profile_image_url":"http://a0.twimg.com/profile_images/1857791844/G_twittermain_normal.jpg"
So obviously it echoes the url (text), i'd like to output the actual image and possibly have control over the sizing of it too if possible.
Thanks in advance!
<img src=" <?php echo $data[0]['profile_image_url'];?>" />
</body>

Grab image url from description in rss feed using php and simplexml

I'm trying to set up a php page made up of images from 3 different RSS feeds, in 3 columns. The first feed is from a Wordpress blog, the second from an Etsy store, and the third from a Flickr feed.
What I would like to do is grab the link to the images from the first 3 items in each feed.
Relevant information:
The Wordpress feed puts the link in the description, which is formatted as CDATA.
The Etsy feed puts the image link as the first item in the description (E.g. <img src="http://ny-image2.etsy.com/il_155x125.126568958.jpg" />), but also has it in a <g:image_link> tag.
The Flickr feed has the link in the description, but it comes third after links for the user's profile and a link to the photo page. There is also <media:content url="http://farm5.static.flickr.com/4016/4377189674_d6f3aafa81_m.jpg"
type="image/jpeg"
height="159"
width="240"/>.
I've read over the basics of both php and simplexml but it seems what I'd like to do is too complicated for me to figure out on my own. I'd prefer to have a separate functions.php file so I only have to call the function on the web page.
ETA:
I've got the images for the Etsy feed pulled using
<?php
function getEtsyFeed($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'><img src=" . $entry->children('g', true)->image_link . " /></a>
</li>";
}
echo "</ul>";
}
?>
and the images from the Flickr feed pulled using
<?php
function getFlickrFeed($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'><img src=" . $entry->children('media', true)->thumbnail->attributes()->url . " /></a>
</li>";
}
echo "</ul>";
}
?>
I'm still not sure what to do about the blog feed, or how to only show the first 3 images without breaking the code that I do have.
You should try MagPie.
http://magpierss.sourceforge.net/

Categories