Skip to content

Instantly share code, notes, and snippets.

@carolinerusso
Created December 7, 2015 15:38
Show Gist options
  • Save carolinerusso/5f8f383e2d55e1198091 to your computer and use it in GitHub Desktop.
Save carolinerusso/5f8f383e2d55e1198091 to your computer and use it in GitHub Desktop.
Connect to and return Instagram uploads via Instagram API/PHP/cURL
<?php
function get_instagram_data( $url ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_TIMEOUT, 20 );
$result = curl_exec( $ch );
curl_close( $ch );
return $result;
}
$access_token = '2017272886.3703b06.b2ceb80bb12e495cba3c699812082a34';
$user_id = '2017272886';
$instagram_count = 6;
$result = get_instagram_data('https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token='.$access_token.'');
$result = json_decode( $result );
$i = 0;
if ( $result ) :
echo '<ul>'
foreach ($result->data as $post ) :
$image_url = $post->images->low_resolution->url;
$caption = $post->caption->text;
$likes = $post->likes->count;
$link = $post->link;
echo '<li class="instagram">
<a href="' . $link . '" target="_blank">
<img src="' . $image_url . '" alt="' . $caption . '" title="' . $caption . '"/>
<div>
<span>
<i>' . $likes . '</i>
</span>
</div>
</a>
</li>';
$i++;
if ($i == $instagram_count )
break;
endforeach;
echo '</ul>';
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment