What is the correct syntax for retrieving youtube data via json and php as of November 2012?

I find it difficult to upload videos from YouTube via json and php.

I spent all night and morning trying to find code snippets from all over the Internet and.

the fact that they do not work tells me that I am not using modern syntax.

I think the clearest way to ask this question is to ask if the following properties are correct in 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 someone tell me if the following is 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 get is:

 Catchable fatal error: Object of class stdClass could not be converted to string 

therefore, I know that one of these properties is incorrect.

Thank you for your help, I will drink coffee and get back to that!

research

these resources are direct api links to the properties I'm trying to get and should work, but they don't seem to be :(.

feed and input structure:

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

record content:

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 label:

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

sample code (on request)

so what i do:

  • I have a form
  • after sending it is processed by the php file (insert.php)
  • which makes some changes to the data and then sent to the database
  • I get an error in a line starting with $ final_li_code (but the code works there if json variables are not included, so the problem is with json variables)
  • (I was told that this form is vulnerable to SQL injection, but it is not a public form, i.e. it is protected by htaccess / htpasswd).

this is the corresponding 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>"; 
+4
source share
2 answers

Received 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 a literal $ followed by t , not a variable named $t .

Example output for gzDS-Kfd5XQ video disc:

string (66) "Sesame Street: Ray Charles sings" I have a song "with Burt and Ernie"

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

line (3) "129"

line (6) "828277"

string (342) "For additional videos and games, visit our new website at http://www.sesamestreet.org

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

Sesame Street is the production of the Sesame Workshop, a non-profit educational organization that also publishes 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 the second parameter to json_decode :

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

PPS: it is much easier var_dump data, find all the bits you need and then write the code.

+5
source

The error you receive means that you are trying to print the object. You can use var_dump ($ json_data) to better understand the object you are working with. Then you can find out which lines can be printed.

For best help, please provide the shortest code example showing your problem. The line specified to retrieve data from youtube ends with an error 400. In addition, the error you specified indicates that you are trying to print a value. None of the code examples you gave try to print the value. It is possible that printing $ video_title is valid when printing a different value.

0
source

Source: https://habr.com/ru/post/1444021/


All Articles