4

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:

https://developers.google.com/youtube/2.0/developers_guide_protocol_understanding_video_feeds#Understanding_Video_Entries

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&amp;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>";
user1063287
  • 10,265
  • 25
  • 122
  • 218
  • Take a look at this, can be usefull: http://stackoverflow.com/questions/13219020/how-to-get-youtube-video-data-using-php – gabrielem Nov 05 '12 at 01:32
  • 2
    Get rid of the curlies and single quotes. `$json_data->entry->title` Alternatively, pass a second argument of `true` to `json_decode` and access it as an array, `$json_data['entry']['title']`. The arrows and curlies look like something out of Perl, where it would be the syntax to access nested arrayrefs. In PHP, curlies and quotes like that do some funny stringification and generally should not be used without understanding the actual thing going on. – Charles Nov 05 '12 at 02:33
  • thanks for your reply, trying to test first solution, code editor does not like `$video_duration = $json_data->entry->media:group->yt$duration;` and `$video_views = $json_data->entry->yt$statistics->viewCount;` - can you please tell me what the syntax should be for these? thank you. – user1063287 Nov 05 '12 at 03:19
  • @charles: the properties returned in feed use the `$` character frequently. Curly brackets and single quotes can be dropped except when `$` appears in property name. – Salman A Nov 05 '12 at 15:35
  • @SalmanA, sounds like a recipe for insanity, especially considering that `:` seems to be used frequently as well. I'd personally *highly* recommend decoding the JSON data into an array, something that naturally uses string keys. – Charles Nov 05 '12 at 17:18

2 Answers2

5

Got your SOS message. Here are the changes you need to make:

$video_title = $json_data->{'entry'}->{'title'}->{'$t'};
$video_date = $json_data->{'entry'}->{'published'}->{'$t'};
$video_duration = $json_data->{'entry'}->{'media$group'}->{'yt$duration'}->{'seconds'};
$video_views = $json_data->{'entry'}->{'yt$statistics'}->{'viewCount'};
$video_description = $json_data->{'entry'}->{'media$group'}->{'media$description'}->{'$t'};

Note that $t is literal $ followed by t, not a variable named $t.

Sample output for video id gzDS-Kfd5XQ:

string(66) "Sesame Street: Ray Charles Sings "I Got A Song" With Bert & Ernie"

string(24) "2008-08-06T18:56:56.000Z"

string(3) "129"

string(6) "828277"

string(342) "For more videos and games check out our new website at http://www.sesamestreet.org

In this video, Bert and Ernie perform with Ray Charles.

Sesame Street is a production of Sesame Workshop, a nonprofit educational organization which also produces Pinky Dinky Doo, The Electric Company, and other programs for children around the world."

PS: If you are comfortable with associative arrays, pass true as second parameter to json_decode:

mixed json_decode(string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0]]])

PPS: It is much easier to var_dump the data, find all the bits you need and write the code afterwards.

Salman A
  • 262,204
  • 82
  • 430
  • 521
  • thank you VERY much, i am currently troubleshooting a few issues and will post back here when i have tested everything, THANK YOU again! – user1063287 Nov 06 '12 at 08:08
0

The error you're receiving means that you are trying to print out an object. You can use var_dump($json_data) to get a better idea of the object that you're working with. Then you can find out what strings can be printed.

For better help, please include the shortest code sample possible showing your issue. The line given for retrieving data from youtube ends up giving a 400 error. Also, the error you give indicates you are trying to print a value. None of the sample code you gave attempts to print a value. It's possible that printing $video_title is valid when printing another value is not.

Nate
  • 539
  • 3
  • 14
  • hello, thank you for your reply, the file_get_contents url above includes a variable `{$random_text}` so if you were trying to access that url, that is why there was a 400 error i think. i have amended above post with code sample. thank you very much. – user1063287 Nov 05 '12 at 07:59