How to display last photos from Instagram on your site
Problem with Instagram is that doesn’t support any simple interface for user posts like some simple XML of RSS feed which could be easily processed.
Yes, we can use Instagram API, but this solution is very complex and unnecessarily complicated.
After the authorization Followgram retrieves your photos and lets you use the RSS feed for these photos.
We can use the feed at: http://followgram.me/user/rss
Now you can process this feed and display photographs, like this:
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
/** * Class load last images from instagram */ class Instagram{ //default url private $rss_url = 'http://followgram.me/user/rss'; public function __construct( $url = NULL ) { if( $url ){ $this->setUrl( $url ); } } /** * Function load images from url * @return array */ public function loadImages() { try{ //load xml file $xml = simplexml_load_file( $this->getUrl(), 'SimpleXMLElement', LIBXML_NOCDATA ); //return output from function parseXML return $this->parseXML( $xml ); }catch(Exception $e) { return array(); } } /** * Function parse Xml file and return array with images * @param $xml string * @return array */ private function parseXML( $xml ) { $output = array(); $i = 0; if( ! $xml ) return $output; foreach($xml->channel->item as $item ){ $i++; //extract image from descriptions preg_match('`.*?(http://[w#$&+,/:;=?@.-]+)[^w#$&+,/:;=?@.-]*?`i', $item->description, $link); if( !$link ) continue; //create output $output[$i]['image'] = (string)$link[1]; //create small image url from image url $output[$i]['smallImage'] = $this->getSmallImageFromImage( $link[1] ); $output[$i]['pubDate'] = (string)$item->pubDate; } return $output; } /** * Function replace url * @param $link string * @return string */ private function getSmallImageFromImage( $link ) { return preg_replace( "|(_7.jpg)$|", '_5.jpg', $link ); } /** * Set anotner url * @param $url string * @return false */ private function setUrl( $url ) { $this->rss_url = $url; } /** * Function return actual RSS url * @return string */ private function getUrl() { return $this->rss_url; } } |
And simple example of usage:
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * ########################## * #Simple example of usage:# * ########################## */ $instagram = new Instagram('http://followgram.me/zaachi/rss'); $data = $instagram->loadImages(); foreach( $data as $value ){ echo '<a href="' . $value['image'] . '" title="' . $value['pubDate'] . '">' . '<img src="' . $value['smallImage'] . '" width="150">'. '</a> '; } |
Posted on 30 October 2012