class-flickr.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * Flickr
  4. *
  5. * with help of the API this class delivers all kind of Images from flickr
  6. *
  7. * @package socialstreams
  8. * @subpackage socialstreams/flickr
  9. * @author ThemePunch <info@themepunch.com>
  10. */
  11. class TP_flickr {
  12. /**
  13. * API key
  14. *
  15. * @since 1.0.0
  16. * @access private
  17. * @var string $api_key flickr API key
  18. */
  19. private $api_key;
  20. /**
  21. * API params
  22. *
  23. * @since 1.0.0
  24. * @access private
  25. * @var array $api_param_defaults Basic params to call with API
  26. */
  27. private $api_param_defaults;
  28. /**
  29. * Basic URL
  30. *
  31. * @since 1.0.0
  32. * @access private
  33. * @var string $url Url to fetch user from
  34. */
  35. private $flickr_url;
  36. /**
  37. * Initialize the class and set its properties.
  38. *
  39. * @since 1.0.0
  40. * @param string $api_key flickr API key.
  41. */
  42. public function __construct($api_key) {
  43. $this->api_key = $api_key;
  44. $this->api_param_defaults = array(
  45. 'api_key' => $this->api_key,
  46. 'format' => 'json',
  47. 'nojsoncallback' => 1,
  48. );
  49. }
  50. /**
  51. * Calls Flicker API with set of params, returns json
  52. *
  53. * @since 1.0.0
  54. * @param array $params Parameter build for API request
  55. */
  56. private function call_flickr_api($params){
  57. //build url
  58. $encoded_params = array();
  59. foreach ($params as $k => $v){
  60. $encoded_params[] = urlencode($k).'='.urlencode($v);
  61. }
  62. //call the API and decode the response
  63. $url = "https://api.flickr.com/services/rest/?".implode('&', $encoded_params);
  64. $rsp = json_decode(file_get_contents($url));
  65. return $rsp;
  66. }
  67. /**
  68. * Get User ID from its URL
  69. *
  70. * @since 1.0.0
  71. * @param string $user_url URL of the Gallery
  72. */
  73. public function get_user_from_url($user_url){
  74. //gallery params
  75. $user_params = $this->api_param_defaults + array(
  76. 'method' => 'flickr.urls.lookupUser',
  77. 'url' => $user_url,
  78. );
  79. //set User Url
  80. $this->flickr_url = $user_url;
  81. //get gallery info
  82. $user_info = $this->call_flickr_api($user_params);
  83. return $user_info->user->id;
  84. }
  85. /**
  86. * Get Group ID from its URL
  87. *
  88. * @since 1.0.0
  89. * @param string $group_url URL of the Gallery
  90. */
  91. public function get_group_from_url($group_url){
  92. //gallery params
  93. $group_params = $this->api_param_defaults + array(
  94. 'method' => 'flickr.urls.lookupGroup',
  95. 'url' => $group_url,
  96. );
  97. //set User Url
  98. $this->flickr_url = $group_url;
  99. //get gallery info
  100. $group_info = $this->call_flickr_api($group_params);
  101. return $group_info->group->id;
  102. }
  103. /**
  104. * Get Public Photos
  105. *
  106. * @since 1.0.0
  107. * @param string $user_id flicker User id (not name)
  108. * @param int $item_count number of photos to pull
  109. */
  110. public function get_public_photos($user_id,$item_count=10){
  111. //public photos params
  112. $public_photo_params = $this->api_param_defaults + array(
  113. 'method' => 'flickr.people.getPublicPhotos',
  114. 'user_id' => $user_id,
  115. 'extras' => 'description, license, date_upload, date_taken, owner_name, icon_server, original_format, last_update, geo, tags, machine_tags, o_dims, views, media, path_alias, url_sq, url_t, url_s, url_q, url_m, url_n, url_z, url_c, url_l, url_o',
  116. 'per_page'=> $item_count,
  117. 'page' => 1
  118. );
  119. //get photo list
  120. $public_photos_list = $this->call_flickr_api($public_photo_params);
  121. return $public_photos_list->photos->photo;
  122. }
  123. /**
  124. * Get Photosets List from User
  125. *
  126. * @since 1.0.0
  127. * @param string $user_id flicker User id (not name)
  128. * @param int $item_count number of photos to pull
  129. */
  130. public function get_photo_sets($user_id,$item_count=10){
  131. //photoset params
  132. $photo_set_params = $this->api_param_defaults + array(
  133. 'method' => 'flickr.photosets.getList',
  134. 'user_id' => $user_id,
  135. 'per_page'=> $item_count,
  136. 'page' => 1
  137. );
  138. //get photoset list
  139. $photo_sets_list = $this->call_flickr_api($photo_set_params);
  140. return $photo_sets_list->photosets->photoset;
  141. }
  142. /**
  143. * Get Photoset Photos
  144. *
  145. * @since 1.0.0
  146. * @param string $photo_set_id Photoset ID
  147. * @param int $item_count number of photos to pull
  148. */
  149. public function get_photo_set_photos($photo_set_id,$item_count=10){
  150. //photoset photos params
  151. $photo_set_params = $this->api_param_defaults + array(
  152. 'method' => 'flickr.photosets.getPhotos',
  153. 'photoset_id' => $photo_set_id,
  154. 'per_page' => $item_count,
  155. 'page' => 1,
  156. 'extras' => 'license, date_upload, date_taken, owner_name, icon_server, original_format, last_update, geo, tags, machine_tags, o_dims, views, media, path_alias, url_sq, url_t, url_s, url_q, url_m, url_n, url_z, url_c, url_l, url_o'
  157. );
  158. //get photo list
  159. $photo_set_photos = $this->call_flickr_api($photo_set_params);
  160. return $photo_set_photos->photoset->photo;
  161. }
  162. /**
  163. * Get Groop Pool Photos
  164. *
  165. * @since 1.0.0
  166. * @param string $group_id Photoset ID
  167. * @param int $item_count number of photos to pull
  168. */
  169. public function get_group_photos($group_id,$item_count=10){
  170. //photoset photos params
  171. $group_pool_params = $this->api_param_defaults + array(
  172. 'method' => 'flickr.groups.pools.getPhotos',
  173. 'group_id' => $group_id,
  174. 'per_page' => $item_count,
  175. 'page' => 1,
  176. 'extras' => 'license, date_upload, date_taken, owner_name, icon_server, original_format, last_update, geo, tags, machine_tags, o_dims, views, media, path_alias, url_sq, url_t, url_s, url_q, url_m, url_n, url_z, url_c, url_l, url_o'
  177. );
  178. //get photo list
  179. $group_pool_photos = $this->call_flickr_api($group_pool_params);
  180. return $group_pool_photos->photos->photo;
  181. }
  182. /**
  183. * Get Gallery ID from its URL
  184. *
  185. * @since 1.0.0
  186. * @param string $gallery_url URL of the Gallery
  187. * @param int $item_count number of photos to pull
  188. */
  189. public function get_gallery_from_url($gallery_url){
  190. //gallery params
  191. $gallery_params = $this->api_param_defaults + array(
  192. 'method' => 'flickr.urls.lookupGallery',
  193. 'url' => $gallery_url,
  194. );
  195. //get gallery info
  196. $gallery_info = $this->call_flickr_api($gallery_params);
  197. return $gallery_info->gallery->id;
  198. }
  199. /**
  200. * Get Gallery Photos
  201. *
  202. * @since 1.0.0
  203. * @param string $gallery_id flicker Gallery id (not name)
  204. * @param int $item_count number of photos to pull
  205. */
  206. public function get_gallery_photos($gallery_id,$item_count=10){
  207. //gallery photos params
  208. $gallery_photo_params = $this->api_param_defaults + array(
  209. 'method' => 'flickr.galleries.getPhotos',
  210. 'gallery_id' => $gallery_id,
  211. 'extras' => 'description, license, date_upload, date_taken, owner_name, icon_server, original_format, last_update, geo, tags, machine_tags, o_dims, views, media, path_alias, url_sq, url_t, url_s, url_q, url_m, url_n, url_z, url_c, url_l, url_o',
  212. 'per_page'=> $item_count,
  213. 'page' => 1
  214. );
  215. //get photo list
  216. $gallery_photos_list = $this->call_flickr_api($gallery_photo_params);
  217. return $gallery_photos_list->photos->photo;
  218. }
  219. /**
  220. * Encode the flickr ID for URL (base58)
  221. *
  222. * @since 1.0.0
  223. * @param string $num flickr photo id
  224. */
  225. public static function base_encode($num, $alphabet='123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ') {
  226. $base_count = strlen($alphabet);
  227. $encoded = '';
  228. while ($num >= $base_count) {
  229. $div = $num/$base_count;
  230. $mod = ($num-($base_count*intval($div)));
  231. $encoded = $alphabet[$mod] . $encoded;
  232. $num = intval($div);
  233. }
  234. if ($num) $encoded = $alphabet[$num] . $encoded;
  235. return $encoded;
  236. }
  237. }
  238. ?>