i'm having a difficult time getting youtube video data via json and php.
i've spent a full night and morning trying code snippets from around the web and stack overflow.
the fact that they are not working suggests to me that i am not using up to date syntax.
i think the clearest way to ask this question is to ask whether the following properties are correct as at november 2012.
so this is my initial variable declaration:
$json = file_get_contents("http://gdata.youtube.com/feeds/api/videos/{$random_text}?v=2&alt=json");
$json_data = json_decode($json);
can anyone please tell me if the following are correct:
1. $video_title = $json_data->{'entry'}->{'title'};
2. $video_date = $json_data->{'entry'}->{'published'};
3. $video_duration = $json_data->{'entry'}->{'media:group'}->{'yt$duration'};
4. $video_views = $json_data->{'entry'}->{'yt$statistics'}->{'viewCount'};
5. $video_description = $json_data->{'entry'}->{'content'};
i don't want to dilute the question by providing too much other code and information, but one of the errors i am getting is:
Catchable fatal error: Object of class stdClass could not be converted to string
so i know one of those properties is not correct.
thank you for your help, i'm gonna get a coffee and come back to this!
research
these resources are the direct api references to the properties i am trying to get and should work but they don't seem to be :(.
the feed and entry structure:
the contents of an entry:
https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_entry
title tag:
https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_title
published tag:
https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_published
yt:duration tag:
https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_yt:duration
yt:statistics > viewCount tag:
https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_yt:statistics
content tag (video description):
https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_content
code sample (as requested)
so what i am doing is:
- i have a form
- upon submit it is processed by a php file (insert.php)
- which makes some changes to the data and then submits to the database
- i get the error message at the line starting with $final_li_code (but the code there works if the json variables are not included, so the problem is with the json variables)
- (i have been told this form is vulnerable to sql injection but it is not a public facing form ie it is htaccess/htpasswd protected).
this is the relevant code from insert.php:
// basic form information
$field1 = $_POST["field1"];
$field2 = $_POST["field2"];
$original_link = $_POST["link"];
// add class and video display information
$random_text = array_pop(explode('/',$original_link));
$final_value = "<a class=\'youtube\' href=\"http://www.youtube.com/embed/".$random_text."?rel=0&autohide=1&showinfo=0&autoplay=0&iv_load_policy=3&wmode=transparent\">link</a>";
//start getting the youtube information
$thumb = "<img src=\"http://i.ytimg.com/vi/".$random_text."/mqdefault.jpg\">";
$json = file_get_contents("http://gdata.youtube.com/feeds/api/videos/{$random_text}?v=2&alt=json");
$json_data = json_decode($json);
$video_title = $json_data->entry->title;
$video_date = $json_data->entry->published;
$video_duration = $json_data->entry->media:group->yt:duration;
$video_views = $json_data->entry->ytstatistics->viewCount;
$video_description = $json_data->entry->content;
// put it all together to create an <li>
$final_li_code = "<li class=\".{$field1} .{$field2}\">{$thumb}<div id=\"video_information\"><h3>{$video_title}</h3><div id=\"video_information_left\"><span id=\"date\">{$video_date}</span><span id=\"duration\">{$video_duration}</span><span id=\"another_id\">{$field2}</span></div><div id=\"video_information_right\"><span id=\"video_views\">{$video_views}</span><span id=\"yet_another_id\">{$field1}</span></div><span id=\"description\">{$video_description}</span></div></li>";