InstagramBridge.php 2.1 KB

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