title: what is the correct syntax to use for getting youtube data via json and php as at november 2012?
body: <p>i'm having a difficult time getting youtube video data via json and php.  </p>
  
  <p>i've spent a full night and morning trying code snippets from around the web and stack overflow. </p>
  
  <p>the fact that they are not working suggests to me that i am not using up to date syntax.  </p>
  
  <p>i think the clearest way to ask this question is to ask whether the following properties are correct as at november 2012.  </p>
  
  <p>so this is my initial variable declaration:</p>
  
  <pre><code>$json = file_get_contents("http://gdata.youtube.com/feeds/api/videos/{$random_text}?v=2&amp;alt=json");
  $json_data = json_decode($json);
  </code></pre>
  
  <p>can anyone please tell me if the following are correct:</p>
  
  <pre><code>1.  $video_title = $json_data-&gt;{'entry'}-&gt;{'title'};
  2.  $video_date = $json_data-&gt;{'entry'}-&gt;{'published'};
  3.  $video_duration = $json_data-&gt;{'entry'}-&gt;{'media:group'}-&gt;{'yt$duration'};
  4.  $video_views = $json_data-&gt;{'entry'}-&gt;{'yt$statistics'}-&gt;{'viewCount'};
  5.  $video_description = $json_data-&gt;{'entry'}-&gt;{'content'};
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>Catchable fatal error: Object of class stdClass could not be converted to string
  </code></pre>
  
  <p>so i know one of those properties is not correct.  </p>
  
  <p>thank you for your help, i'm gonna get a coffee and come back to this!</p>
  
  <p><strong>research</strong></p>
  
  <p>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 :(.  </p>
  
  <p>the feed and entry structure:</p>
  
  <p><a href="https://developers.google.com/youtube/2.0/developers_guide_protocol_understanding_video_feeds#Understanding_Video_Entries" rel="nofollow">https://developers.google.com/youtube/2.0/developers_guide_protocol_understanding_video_feeds#Understanding_Video_Entries</a></p>
  
  <p>the contents of an entry:</p>
  
  <p><a href="https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_entry" rel="nofollow">https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_entry</a></p>
  
  <p>title tag:</p>
  
  <p><a href="https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_title" rel="nofollow">https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_title</a></p>
  
  <p>published tag:</p>
  
  <p><a href="https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_published" rel="nofollow">https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_published</a></p>
  
  <p>yt:duration tag:</p>
  
  <p><a href="https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_yt:duration" rel="nofollow">https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_yt:duration</a></p>
  
  <p>yt:statistics > viewCount tag:</p>
  
  <p><a href="https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_yt:statistics" rel="nofollow">https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_yt:statistics</a></p>
  
  <p>content tag (video description):</p>
  
  <p><a href="https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_content" rel="nofollow">https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_content</a></p>
  
  <p><strong>code sample</strong> (as requested)</p>
  
  <p>so what i am doing is:</p>
  
  <ul>
  <li>i have a form</li>
  <li>upon submit it is processed by a php file (insert.php)</li>
  <li>which makes some changes to the data and then submits to the database</li>
  <li>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)  </li>
  <li>(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).  </li>
  </ul>
  
  <p>this is the relevant code from insert.php:</p>
  
  <pre><code>// 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 = "&lt;a class=\'youtube\' href=\"http://www.youtube.com/embed/".$random_text."?rel=0&amp;autohide=1&amp;showinfo=0&amp;autoplay=0&amp;iv_load_policy=3&amp;amp;wmode=transparent\"&gt;link&lt;/a&gt;";
  //start getting the youtube information
  $thumb = "&lt;img src=\"http://i.ytimg.com/vi/".$random_text."/mqdefault.jpg\"&gt;";
  $json = file_get_contents("http://gdata.youtube.com/feeds/api/videos/{$random_text}?v=2&amp;alt=json");
  $json_data = json_decode($json);
  $video_title = $json_data-&gt;entry-&gt;title;
  $video_date = $json_data-&gt;entry-&gt;published;
  $video_duration = $json_data-&gt;entry-&gt;media:group-&gt;yt:duration;
  $video_views = $json_data-&gt;entry-&gt;ytstatistics-&gt;viewCount;
  $video_description = $json_data-&gt;entry-&gt;content;
  // put it all together to create an &lt;li&gt;
  $final_li_code = "&lt;li class=\".{$field1} .{$field2}\"&gt;{$thumb}&lt;div id=\"video_information\"&gt;&lt;h3&gt;{$video_title}&lt;/h3&gt;&lt;div id=\"video_information_left\"&gt;&lt;span id=\"date\"&gt;{$video_date}&lt;/span&gt;&lt;span id=\"duration\"&gt;{$video_duration}&lt;/span&gt;&lt;span id=\"another_id\"&gt;{$field2}&lt;/span&gt;&lt;/div&gt;&lt;div id=\"video_information_right\"&gt;&lt;span id=\"video_views\"&gt;{$video_views}&lt;/span&gt;&lt;span id=\"yet_another_id\"&gt;{$field1}&lt;/span&gt;&lt;/div&gt;&lt;span id=\"description\"&gt;{$video_description}&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;";
  </code></pre>