I am using this code to show all videos of a user in my website:
<?php
$xml = simplexml_load_file('http://gdata.youtube.com/feeds/api/users/XXXXXXX/uploads');
$server_time = $xml->updated;
$return = array();
foreach ($xml->entry as $video) {
$vid = array();
$vid['id'] = substr($video->id,42);
$vid['title'] = $video->title;
array_push($return, $vid);
}
?>
<h2>Video Gallery</h2>
<?php
foreach($return as $video) {
?>
<div class="col-md-4">
<div style="height: auto;" class="featured-box featured-box-secundary">
<div class="box-content clearfix">
<h4><?= $video['title'] ?></h4>
<iframe width="270" height="203" src="https://www.youtube.com/embed/<?=$video['id']?>?rel=0&showinfo=0" allowfullscreen></iframe>
</div></div></div>
<?php
}
?>
But I received a notice that is deprecated. How I can have $vid['id'] and $vid['title'] to put into HTML with API v3?
Yes, Youtube API V2 is dead.
To make requests to Youtube API V3 you need to be authenticated. Get {YOUR_API_KEY} at
Google Developers Console. You'll need to create new project and enable Youtube API.
After that, you can issue the following commands:
First
The channels#list method will return a JSON with some information about the channel, including the {PLAYLIST_ID} for the "uploads" playlist:
https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername={USERNAME}&key={YOUR_API_KEY}
Second
With the {PLAYLIST_ID} you can get the user uploaded videos with the playlistItems#list method:
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId={PLAYLIST_ID}&key={YOUR_API_KEY}
You can test the above code on:
Youtube apis-explorer
Related
I'm having trouble figuring out how to use PHP Simple HTML DOM Parser for pulling information from a website.
require('simple_html_dom.php');
$html = file_get_html('https://example.com');
$ret = array();
foreach($html->find(".project-card-mini-wrap") as $element) {
echo $element;
}
The output of $element is:
<div class="project-card-mini-wrap">
<a class="project_item block mb2 green-dark" href="/projects/andrewkostirev/kostirev-the-real-you">
<div class="project_thumbnail hover-group border border-box mb1">
<img alt="Project image" class="hover-zoomin fit" src="https://ksr-ugc.imgix.net/projects/2123706/photo-original.png?v=1444253259&w=218&h=162&fit=crop&auto=format&q=92&s=9d6c437e96b720dce82fc9b598b3e8ae" />
<div class="funding_tag highlight">10 days to go</div>
<div class="hover-zoomout bg-green-90">
<p class="white p2 h5">A clothing brand like never seen before</p>
</div>
</div>
<div class="project_name h5 bold"> KOSTIREV - THE REAL YOU </div>
</a>
</div>
This is the information I'd like to pull from the website:
1: Link href
2: Image src
3: Project name
Hopefully this will provide some insight to you as well as other users of PHP Simple HTML DOM Parser
foreach($html->find(".project-card-mini-wrap") as $element) {
echo "Project name: ",$element->find('.project_name',0)->innertext,"<br/>\n";
echo "Image source: ",$element->find('img',0)->src,"<br/>\n";
echo "Link: ",$element->find('a',0)->href,"<br/>\n";
}
Produces this output:
Project name: KOSTIREV - THE REAL YOU
Image source: https://ksr-ugc.imgix.net/projects/2123706/photo-original.png?v=1444253259&w=218&h=162&fit=crop&auto=format&q=92&s=9d6c437e96b720dce82fc9b598b3e8ae
Link: /projects/andrewkostirev/kostirev-the-real-you
I tried this and it worked, thanks for the help! Here is something i made using primewire.ag as a example.... The goal here was to extract all the links of a given page.
<?php
require('simple_html_dom.php');
// Create DOM from URL or file
$html = file_get_html('http://www.primewire.ag/watch-2805774-Star-Wars-The-Last-Jedi-online-free');
// Find All Movie Links
$linkPrefix = 'http://primewire.ag';
$linkClass;
foreach($html->find(".movie_version_link") as $linkClass) {
echo "Link: ",$linkPrefix,$linkClass->find('a',0)->href,"<br/>\n";
}
?>
This is also a good library for scraping and traversing via HTML
https://github.com/paquettg/php-html-parser
I'm trying to get some fb feeds on a website. I've searched the web and found a couple things I could try. After failing quite a bit, I've managed to get it. Today the access token wasn't valid anymore, so I got a new one and it worked again. After a bit (token still valid) it didn't anymore (got two messages). So I searched again and found a new solution that works and I think that gives me a token which is longer valid. This is the php code:
<?php
$limit = 3;
$profile_id = "32796xxxxx64243";
//App Info, needed for Auth
$app_id = "59351xxxxx41974";
$app_secret = "082d36fe4108ae51xxxxxxxxxxfb84b4";
//retrieve a new Auth token
$curl = 'https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id='.$app_id.'&client_secret='.$app_secret;
//$authToken = file_get_contents($curl);
$authToken = file_get_contents("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret}");
$data = file_get_contents("https://graph.facebook.com/{$profile_id}/feed?{$authToken}");
echo $data;
?>
This gives me following result:
Now I'm trying to get all the info out of it, but here is where I'm getting an error in my foreach loop. This is the code I use:
<div id="footer">
<h5>Laatste Facebook feeds</h5>
<div class="wrapperfb">
<?
$counter = 0;
foreach($data->data as $d){
if($counter==$limit)
break;
?>
<div class="singlefb">
<div class="imgfb">
<a class="afb" href="http://facebook.com/profile.php?id=<?=$d->from->id?>">
<img border="0" alt="<?=$d->from->name?>" src="https://graph.facebook.com/<?=$d->from->id?>/picture"/>
</a>
</div>
<div class="textfb">
<span style="font-weight:bold"><a class="afb" href="http://facebook.com/profile.php?id=<?=$d->from->id?>">
<?=$d->from->name?></a></span><br/>
<span style="color: #999999;">on <?=date('F j, Y H:i',strtotime($d->created_time))?></span>
<br/>
<?=$d->message?>
</div>
</div>
<?
$counter++;
}
?>
</div>
</div>
This is the error I'm getting:
Warning: Invalid argument supplied for foreach() in
/customers/9/2/3/beach-korfbal.be/httpd.www/facebookFeeds.php on line
19
line 19 is the foreach loop:
foreach($data->data as $d){
It's probably something stupid I'm looking over. Could someone please help me out. This is the last thing I need to complete to launch the site.
It works now thanks to Abhik Chakraborty's comment.
This is the full code:
<?php
$limit = 5;
$profile_id = "32796xxxxx64243";
//App Info, needed for Auth
$app_id = "59351xxxxx41974";
$app_secret = "082d36fe4108ae51xxxxxxxxxxfb84b4";
//retrieve a new Auth token
$authToken = file_get_contents("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret}");
$data = json_decode(file_get_contents("https://graph.facebook.com/{$profile_id}/feed?{$authToken}"));
?>
<div id="footer">
<h5>Laatste Facebook feeds</h5>
<div class="wrapperfb">
<?
$counter = 0;
foreach($data->data as $d){
if($counter==$limit)
break;
?>
<div class="singlefb">
<div class="imgfb">
<a class="afb" href="http://facebook.com/profile.php?id=<?=$d->from->id?>">
<img border="0" alt="<?=$d->from->name?>" src="https://graph.facebook.com/<?=$d->from->id?>/picture"/>
</a>
</div>
<div class="textfb">
<span style="font-weight:bold"><a class="afb" href="http://facebook.com/profile.php?id=<?=$d->from->id?>">
<?=$d->from->name?></a></span><br/>
<span style="color: #999999;">on <?=date('F j, Y H:i',strtotime($d->created_time))?></span>
<br/>
<?=$d->message?>
</div>
</div>
<?
$counter++;
}
?>
</div>
</div>
I am trying to integrate the Google+ API on my website so that when a user submits approval via Oauth their Google+ activity feed will be displayed.
I have all of it working pretty much and am working on defining the variables that it will display for their feed.
Currently, this is what I have that IS working:
$activityMarkup = '';
foreach($activities['items'] as $activity) {
$url = filter_var($activity['url'], FILTER_VALIDATE_URL);
$title = filter_var($activity['title'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$content = filter_var($activity['object']['content'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$published = $activity['published'];
$totalItems = $activity['object']['plusoners']['totalItems'];
$activityMarkup .= "<div class='activity'>
<a href='".$url."' target='_blank'>$title</a>
<div>$content</div>
<div><br/><span>Date Published: </span>$published</div>
<div><br/><span>Total Likes: </span>$totalItems</div>
</div><br/><br>";
What I am trying to get to work is adding in the image that is associated with the post, namely the thumbnail.
I have tried different variations based on using existing code that works, but I just can't figure it out.
According to the Google API, this is the information for that image:
object.attachments[].image object The preview image for photos or videos.
object.attachments[].image.url string URL of the link.
Here's a link to all of the activities available if the above information isn't enough:
https://developers.google.com/+/api/latest/activities#object.originalContent
If someone can help me figure out how to define the variable for the image, that'd be awesome.
I figured it out. If anyone needs it, here's the code.
$image='';
if(isset($activity['object']['attachments'])){
foreach($activity['object']['attachments'] as $att){
if(isset($att['image']['url'])) {
$src = $att['image']['url'];
To call it out:
<img src='$src'>
<?php
$image='';
if(isset($activity['object']['attachments']))
{
foreach($activity['object']['attachments'] as $att)
{
if(isset($att['image']['url']))
{
$src = $att['image']['url'] ;
}
}
}
?>
<img src='<?php echo $src ?>'>
Works a Treat - https://developers.google.com/+/api/latest/activities#object.originalContent
<?php foreach($activities['items'] as $activity): ?>
<div class="activity" >
<div class="title" ><a href="<?php echo($activity['object']['url']) ; ?>" ><?php echo($activity['object']['content']); ?></a></div>
<p>Published at <?php echo($activity['published']); ?></p>
<p>
<?php echo($activity['object']['replies']['totalItems']); ?> Replys .
<?php echo($activity['object']['plusoners']['totalItems']); ?> Plusoners .
<?php echo($activity['object']['resharers']['totalItems']); ?> Reshares
</p>
</div>
<?php endforeach ?>
Another Way to add to the Feed is with replies, plusoners and reshares
I am using the zend google api library to retrieve a list of event from a public calendar. I am successfully able to retrieve the title, when, and content, but where is an empty array. Any documentation that I have found in the past 3 hours from Google, Zend or otherwise is terrible and does not give a list of possible values. The only documentation I can find on the location of the event is how to set it if you are creating an event.
How do I access the location?
<?php
foreach ($eventFeed as $event) :
?>
<li>
<div class="event_title">
<?php echo $event->title->text;?>
</div>
<div class="event_time">
<?php echo date('h:ia', strtotime($event->when[0]->startTime));?>
</div>
<div class="event_location">
<?php echo $event->where?>
</div>
<div class="event_description">
<?php echo $event->content->text?>
</div>
</li>
<?php
endforeach;
?>
$where=$event->Where;
foreach($where as $eventplace)
{
echo $eventplace;
}
It will give you location.
The main reason is that it gives the location value in array so we have to find it out manually....
I'm looking for a way get the following from a users youtube name or url.
thumbnail
link to video on youtube.com
Title of the video
Need to do it with PHP. Is there some youtube api I can use or is something like simple pie my best bet?
Look at the code
<?php
error_reporting(E_ALL);
$feedURL = 'http://gdata.youtube.com/feeds/api/users/USER-ID/uploads?max-results=50';
$sxml = simplexml_load_file($feedURL);
$i=0;
foreach ($sxml->entry as $entry) {
$media = $entry->children('media', true);
$watch = (string)$media->group->player->attributes()->url;
$thumbnail = (string)$media->group->thumbnail[0]->attributes()->url;
?>
<div class="videoitem">
<div class="videothumb"><img src="<?php echo $thumbnail;?>" alt="<?php echo $media->group->title; ?>" /></div>
<div class="videotitle">
<h3><?php echo $media->group->title; ?></h3>
<p><?php echo $media->group->description; ?></p>
</div>
</div>
<?php $i++; if($i==3) { echo '<div class="clear small_v_margin"></div>'; $i=0; } } ?>
Code is copied from the post SimpleXML loop works but breaks half way through
It will get a users all youtube videos. Now it is very easy to develop your own.
Know more about YouTube API from https://code.google.com/apis/youtube/2.0/reference.html
YouTube has got an API available. You can see the reference here: https://code.google.com/apis/youtube/2.0/reference.html
http://code.google.com/apis/gdata/articles/php_client_lib.html
and
http://code.google.com/apis/youtube/2.0/developers_guide_php.html