Using cURL to Show Instagram Photos on your Website

While creating a “Social” page for a client, I came across an issue with displaying the users’ feed of new images from Instagram. The solution we came up with was to use Statigr.am’s awesome RSS feed, cURL, and SimpleXML within PHP to create a most recent images posting.

To get started, you’ll need to setup your account on Statigr.am.

Once you’ve got your account, your feed URL will be something like: http://statigr.am/feed/YOURACCOUNT.

Step 1:
Set up your cURL call:

<?php
          // Use cURL to fetch text
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'http://statigr.am/feed/YOURACCOUNT');
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);
	$rss = curl_exec($ch);
	curl_close($ch);
?>

Step 2:
Create your SimpleXML rss feed

<?php
// Manipulate string into object
$gramfeed = simplexml_load_string($rss);
?>

Step 3:
Output (we are only outputting the image + link from the RSS feed, there are many more options!)

<div class="hold">
	<?php
		//outputting just the image in this case.
		for($i=0; $i<10; $i++)
		{
			$desc = $gramfeed->channel->item[$i]->description;
			echo '<div class="igramimg">' .$desc . '</div>';
		}
	?>
</div><!--hold-->

Step 4:
Save it out! You should see a simple post of your Instagram feed on your website!

Full Code:

<?php
	// Use cURL to fetch text
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'http://statigr.am/feed/YOURACCOUNT);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);
	$rss = curl_exec($ch);
	curl_close($ch);

	// Manipulate string into object
	$gramfeed = simplexml_load_string($rss);
?>
	<div class="hold">
		<?php
			//outputting just the image in this case. remove links.
			for($i=0; $i<2; $i++)
			{
				$desc = $gramfeed->channel->item[$i]->description;
				echo '<div>' .$desc . '</div>';
			}
		?>
	</div><!--hold-->

Download Instagram-Feed here

Posted in Instagram, PHP, Statigram