I would like to get all my youtube watch later video list whith PHP API 3.0 but it seem's that we can not access to these with PHP and API 3. Does anyone have done it yet ?
My code for now
// Call the YouTube Data API's channelSections.list method to retrieve your channel sections.
$listResponse = $youtube->channelSections->listChannelSections('snippet,contentDetails', array('mine' => true));
$channelSections = $listResponse['items'];
$htmlBody .= "<h2>Sections Shuffled</h2><ul>";
foreach ($channelSections as $channelSection) {
// Each section in the list of shuffled sections is sequentially
// set to position 0, i.e. the top.
$channelSection['snippet']['position'] = 0;
// Call the YouTube Data API's channelSections.update method to update a channel section.
$updateResponse = $youtube->channelSections->update('snippet,contentDetails', $channelSection);
$htmlBody .= sprintf('<li>%s "%s"</li>',
$updateResponse['id'], $updateResponse['snippet']['title']);
}
$htmlBody .= '</ul>';
Use $youtube->channels->listChannels instead of $youtube->channelSections->listChannelSections
// Get all channels of current user
$channels = $youtube->channels->listChannels('contentDetails', ['mine' => true]);
// Get watch later playlists ID from the first channel
$watchLaterListId = $channels[0]->getContentDetails()->getRelatedPlaylists()->getWatchLater();
// Get videos from the playlist
$videos = $youtube->playlistItems->listPlaylistItems('contentDetails', ['playlistId' => $watchLaterListId]);
// Loop videos
foreach($videos as $video)
{
// This is the global video ID.
$globalVideoId = $video->getContentDetails()->getVideoId();
// This is unique ID of this video on the playlist. The one you need to interact with the playlist
$playlistVideoId = $video->id;
//i.e: To remove this video from the playlist
$youtube->playlistItems->delete($playlistVideoId);
}
Related
I try update a YouTube Video. I use the function at the site https://developers.google.com/youtube/v3/code_samples/php#update_a_video and I try to copy this in my code but if I call listVideos(), I get Google_Service_YouTube_VideoListResponse but I need a Google_Service_YouTube_Video. How can I get it?
My PHP Code:
$youtube = new Google_Service_YouTube($client);
$videoid = "******Video ID********";
$new = "******* TEXT *********";
$videoinfo = $youtube->videos->listVideos('snippet', array('id' => $videoid));
$videoSnippet = $videoinfo[0]['snippet'];
$description = $videoSnippet['description'] . $new;
$videoSnippet['description'] = $description;
$youtube->videos->update("snippet", $videoSnippet);
The Error:
: Uncaught TypeError: Argument 2 passed to
Google_Service_YouTube_Resource_Videos::update() must be an instance of
Google_Service_YouTube_Video, instance of
Google_Service_YouTube_VideoSnippet given, called in
P:\Apache24\htdocs\*********** on line 232 and defined in
P:\Apache24\htdocs\*******\Google Api System\vendor\google\apiclient-
services\src\Google\Service\YouTube\Resource\Videos.php:309
Stack trace:
0 P:\Apache24\htdocs\*********: Google_Service_YouTube_Resource_Videos-
>update('snippet', Object(Google_Service_YouTube_VideoSnippet ))
1 P:\Apache24\htdocs\********\*********\index.php(438):
require('P:\\Apache24\\htd...')
you have to use
$video = $listResponse[0];
$updateResponse = $youtube->videos->update("snippet", $video);
since in the documentation
// Since the request specified a video ID, the response only
// contains one video resource.
$video = $listResponse[0];
$videoSnippet = $video['snippet'];
$tags = $videoSnippet['tags'];
// Preserve any tags already associated with the video. If the video does
// not have any tags, create a new list. Replace the values "tag1" and
// "tag2" with the new tags you want to associate with the video.
if (is_null($tags)) {
$tags = array("tag1", "tag2");
} else {
array_push($tags, "tag1", "tag2");
}
// Set the tags array for the video snippet
$videoSnippet['tags'] = $tags;
// Update the video resource by calling the videos.update() method.
$updateResponse = $youtube->videos->update("snippet", $video);
I am trying to use the YouTube v3 API to show all the videos from a particular YouTube playlist. Here is my code so far:
<?php
$playlistId = 'PLHGPqm6HHcedxhoMXo9m9yKqsOmOB_S0F';
$maxResults = 20;
$apiKey = 'APIKEYHERE';
$string = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId='.$playlistId.'&maxResults='.$maxResults.'&key='.$apiKey.''));
foreach($string['items'] as $item) {
echo $item['title'];
}
?>
I have used Postman to test the url and it is definitely getting data but when i try to echo any data it isn't showing.
I have learnt that Json decode will return stdclass by default json_decode php.net so you would have to replace foreach with this:
foreach($string->items as $item) {
echo $item->snippet->title;
}
Im using the youtube api to search for videos. My issue is that when a search is completed, the output from the search is
video_name video_id
I just want it to show
video_name
heres my code to display search results
$client = new Google_Client();
$client->setDeveloperKey($DEVELOPER_KEY);
$youtube = new Google_Service_YouTube($client);
try {
$searchResponse = $youtube->search->listSearch('id,snippet', array(
'q' => $_GET['q'],
'maxResults' => $_GET['maxResults'],
));
$videos = '';
$channels = '';
$playlists = '';
// Add each result to the appropriate list, and then display the lists of
// matching videos, channels, and playlists.
foreach ($searchResponse['items'] as $searchResult) {
$imgsrc="<img src=http://img.youtube.com/vi/".$searchResult['id']
['videoId']."/hqdefault.jpg height=125 width=125>";
switch ($searchResult['id']['kind']) {
case 'youtube#video':
$videos .= sprintf('<li>'.$imgsrc.' %s (%s)</li>', $searchResult['snippet']['title'],
$searchResult['id']['videoId']."<a href=/video.php?".$searchResult['id']['videoId']." target=_blank> Watch This Video</a>");
break;
I'm not too familiar with sprintf but you want to remove
$searchResult['id']['videoId']
without breaking your code.
I'd suggest wrapping it in between a particular string e.g.
vidid[".$searchResult['id']['videoId']."]
and then remove all content between vidid[ and ].
I am able to access my user image from FB with graph api by accessing the user id like so: https://graph.facebook.com/<USER_ID>/picture
However for my code to work, i need the real path to the image like http://profile.ak.fbcdn.net/hprofile-ak-snc6/******_**************_********_q.jpg
FBs doc shows that by adding ?callback=foo i can get an output, but in practice it doesnt seem to work.
any suggestions for getting the full path to my image with that .jpg extension from graph api or with the user id, thank you.
Callback is for javascript requests,
For php,try appending a redirect=false in url.
Do a curl request to,
https://graph.facebook.com/shaverm/picture?redirect=false
If you want to use callback in js,
$.getJSON('https://graph.facebook.com/zuck/picture?callback=?',function (resp) {
$('body').html(resp.data.url);
});
Demo
Reference
*USE FOLLOWING YOU NEVER GET WRONG RESULT *
$URL='FB GRAPH API URL';
$headers = get_headers($URL, 1); // make link request and wait for redirection
if(isset($headers['Location'])) {
$URL = $headers['Location']; // this gets the new url
}
$url_arr = explode ('/',$URL);
$ct = count($url_arr);
$name = $url_arr[$ct-1];
$name_div = explode('.', $name);
$ct_dot = count($name_div);
$img_type = $name_div[$ct_dot -1];
$pos = strrpos($img_type, "&");//many time you got in url
if($pos)
{
$pieces = explode("&", $img_type);
$img_type = $pieces[0];
}
$imagename = imgnameyouwant'.'.$img_type;
$content = file_get_contents($URL);
file_put_contents("fbscrapedimages/$imagename", $content);
I'm trying to get the video data from this youtube playlist feed and add the interesting data to an array and use that later, but as you can see from the feed some videolinks are "dead" and that results in problems for my code.
The error I get is "Node no longer exists" when I try to access $attrs['url']. I've tried for hours to find a way to check if the node exists before I access it but I have no luck.
If anyone could help me to either parse the feed some other way with the same result or create a if-node-exists check that works I would be most happy. Thank you in advance
$url = 'http://gdata.youtube.com/feeds/api/playlists/18A7E36C33EF4B5D?v=2';
$sxml = simplexml_load_file($url);
$i = 0;
$videoobj;
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get video player URL
$attrs = $media->group->player->attributes();
$videoobj[$i]['url'] = $attrs['url'];
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$videoobj[$i]['thumb'] = $attrs['url'];
$videoobj[$i]['title'] = $media->group->title;
$i++;
}
if ($media->group->thumbnail && $media->group->thumbnail[0]->attributes()) {
$attrs = $media->group->thumbnail[0]->attributes();
$videoobj[$i]['thumb'] = strval($attrs['url']);
$videoobj[$i]['title'] = strval($media->group->title);
}
SimpleXML's methods always return objects, which are themselves linked to the original document (some internal thingy related to libxml.) If you want to store that data for later use, cast it as a string, like this:
$videoobj[$i]['url'] = (string) $attrs['url'];
$videoobj[$i]['thumb'] = (string) $attrs['url'];
$videoobj[$i]['title'] = (string) $media->group->title;