class-youtube.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Youtube
  4. *
  5. * with help of the API this class delivers all kind of Images/Videos from youtube
  6. *
  7. * @package socialstreams
  8. * @subpackage socialstreams/youtube
  9. * @author ThemePunch <info@themepunch.com>
  10. */
  11. class TP_youtube {
  12. /**
  13. * API key
  14. *
  15. * @since 1.0.0
  16. * @access private
  17. * @var string $api_key Youtube API key
  18. */
  19. private $api_key;
  20. /**
  21. * Channel ID
  22. *
  23. * @since 1.0.0
  24. * @access private
  25. * @var string $channel_id Youtube Channel ID
  26. */
  27. private $channel_id;
  28. /**
  29. * Initialize the class and set its properties.
  30. *
  31. * @since 1.0.0
  32. * @param string $api_key Youtube API key.
  33. */
  34. public function __construct($api_key,$channel_id) {
  35. $this->api_key = $api_key;
  36. $this->channel_id = $channel_id;
  37. }
  38. /**
  39. * Get Youtube Playlists
  40. *
  41. * @since 1.0.0
  42. */
  43. public function get_playlists(){
  44. //call the API and decode the response
  45. $url = "https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=".$this->channel_id."&key=".$this->api_key;
  46. $rsp = json_decode(file_get_contents($url));
  47. return $rsp->items;
  48. }
  49. /**
  50. * Get Youtube Playlist Items
  51. *
  52. * @since 1.0.0
  53. * @param string $playlist_id Youtube Playlist ID
  54. * @param integer $count Max videos count
  55. */
  56. public function show_playlist_videos($playlist_id,$count=50){
  57. //call the API and decode the response
  58. $url = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=".$playlist_id."&maxResults=".$count."&fields=items%2Fsnippet&key=".$this->api_key;
  59. $rsp = json_decode(file_get_contents($url));
  60. return $rsp->items;
  61. }
  62. /**
  63. * Get Youtube Channel Items
  64. *
  65. * @since 1.0.0
  66. * @param integer $count Max videos count
  67. */
  68. public function show_channel_videos($count=50){
  69. //call the API and decode the response
  70. $url = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=".$this->channel_id."&maxResults=".$count."&key=".$this->api_key."&order=date";
  71. echo $url;
  72. $rsp = json_decode(file_get_contents($url));
  73. return $rsp->items;
  74. }
  75. }
  76. ?>