InstagramBridge.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * RssBridgeInstagram
  4. * Returns the newest photos
  5. * 2014-05-25
  6. *
  7. * @name Instagram Bridge
  8. * @homepage http://instagram.com/
  9. * @description Returns the newest images
  10. * @maintainer pauder
  11. * @use1(u="username")
  12. */
  13. class InstagramBridge extends BridgeAbstract{
  14. private $request;
  15. public function collectData(array $param){
  16. $html = '';
  17. if (isset($param['u'])) { /* user timeline mode */
  18. $this->request = $param['u'];
  19. $html = file_get_html('http://instagram.com/'.urlencode($this->request)) or $this->returnError('Could not request Instagram.', 404);
  20. }
  21. else {
  22. $this->returnError('You must specify a Instagram username (?u=...).', 400);
  23. }
  24. $innertext = null;
  25. foreach($html->find('script') as $script)
  26. {
  27. if ('' === $script->innertext) {
  28. continue;
  29. }
  30. $pos = strpos(trim($script->innertext), 'window._sharedData');
  31. if (0 !== $pos)
  32. {
  33. continue;
  34. }
  35. $innertext = $script->innertext;
  36. break;
  37. }
  38. $json = trim(substr($innertext, $pos+18), ' =;');
  39. $data = json_decode($json);
  40. $userMedia = $data->entry_data->UserProfile[0]->userMedia;
  41. foreach($userMedia as $media)
  42. {
  43. $image = $media->images->standard_resolution;
  44. $item = new \Item();
  45. $item->uri = $media->link;
  46. $item->content = '<img src="' . htmlentities($image->url) . '" width="'.htmlentities($image->width).'" height="'.htmlentities($image->height).'" />';
  47. if (isset($media->caption))
  48. {
  49. $item->title = $media->caption->text;
  50. } else {
  51. $item->title = basename($image->url);
  52. }
  53. $item->timestamp = $media->created_time;
  54. $this->items[] = $item;
  55. }
  56. }
  57. public function getName(){
  58. return (!empty($this->request) ? $this->request .' - ' : '') .'Instagram Bridge';
  59. }
  60. public function getURI(){
  61. return 'http://instagram.com/';
  62. }
  63. public function getCacheDuration(){
  64. return 3600;
  65. }
  66. }