Skip to content

Instantly share code, notes, and snippets.

@vocolboy
Last active January 27, 2016 08:51
Show Gist options
  • Save vocolboy/16e8b9c798356eb9f124 to your computer and use it in GitHub Desktop.
Save vocolboy/16e8b9c798356eb9f124 to your computer and use it in GitHub Desktop.
//viewCount,likeCount,dislikeCount,favoriteCount,commentCount
public function youtube_views($url)
{
$api_key = "Your_Api_for_youtube";
$count = viewCount //viewCount,likeCount,dislikeCount,favoriteCount,commentCount
parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );
$video_ID = $my_array_of_vars['v'];
$url_content = "https://www.googleapis.com/youtube/v3/videos?id={$video_ID}&key={$api_key}&part=statistics";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url_content);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($ch);
if (curl_errno($ch))
{
echo curl_error($ch);
echo "\n<br />";
$contents = '';
}
else
{
curl_close($ch);
}
if (!is_string($contents) || !strlen($contents))
{
echo "Failed to get contents.";
$contents = '';
}
$JSON_Data = json_decode($contents);
return $JSON_Data->{'items'}[0]->{'statistics'}->{$count};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment