title: Updating YouTube PHP Function for V3 API
body: <p>I had been using a function I created after piecing together different bits of code for my Wordpress blog. It allowed me to pass in a shortcode with a value defining the URL of the YouTube video, then it would print the embedded video, the view count and the duration immediately underneath it. </p>
  
  <p>Unfortunately, it appears that the API I was using was depreciated by Google and I can't seem to get the current API (v3) to work with this code, so I'd appreciate some help in trying to make this right. I grabbed a developer key from Google and am able to get the JSON response when I open it directly in my browser but it seems like the calls in the code below doesn't want to match up for some reason. It would help if I still could reference the old API response to compare so I'm sort of flying blind here. </p>
  
  <pre><code>add_shortcode('yt', 'getYoutubeFrame');
  
  // Convert Seconds to Minutes
    function sec2hms ($sec, $padHours = false) 
    {
      $hms = "";
      $hours = intval(intval($sec) / 3600); 
      $hms .= ($padHours) 
            ? str_pad($hours, 2, "0", STR_PAD_LEFT). ":"
            : $hours. ":";
      $minutes = intval(($sec / 60) % 60); 
      $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ":";
      $seconds = intval($sec % 60); 
      $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
      return $hms;
  }
  
  function getYoutubeFrame($atts) {
      extract(shortcode_atts(array(
      'video' =&gt; ''
      ), $atts));
  
      // Get YouTube data via the API
      $JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos?q=$video&amp;alt=json");
      $JSON_Data = json_decode($JSON);
      $views = $JSON_Data-&gt;{'feed'}-&gt;{'entry'}[0]-&gt;{'yt$statistics'}-&gt;{'viewCount'};
      $views = number_format($views);
      $duration = $JSON_Data-&gt;{'feed'}-&gt;{'entry'}[0]-&gt;{'media$group'}-&gt;{'yt$duration'}-&gt;{'seconds'};
  
  
      $s = "&lt;iframe width=\"1280\" height=\"720\" src=\"//www.youtube.com/embed/$video?showinfo=0&amp;rel=0&amp;modestbranding=1&amp;theme=light&amp;iv_load_policy=3&amp;autohide=1&amp;enablejsapi=1\" frameborder=\"0\" allowfullscreen&gt;&lt;/iframe&gt;";
      $s .= "&lt;strong&gt;Views:&lt;/strong&gt; $views&lt;br&gt;&lt;strong&gt;Duration:&lt;/strong&gt; ";
      $s .= sec2hms($duration);
      $s .= "&lt;br /&gt;&lt;br /&gt;";
      return $s;
      }
  </code></pre>
  
  <p>It appears that the API has changed the way the duration is displayed (using ISO 8601 instead of raw number of seconds). My experience and knowledge of PHP is rather limited otherwise I probably could have solved this in my sleep! Any suggestions for how to resolve this?</p>