I’m planning on redesigning my website and part of this will involve retrieving my own Instagram posts to show the most recent ones on the homepage. It’s been a while since I did any coding and so I wanted to learn how to do it myself rather than using a plugin. The main recent change is that Instagram now requires an access token in order to use their API; thankfully, it is easy to acquire this using the Pixel Union website. Simply login using your Instagram credentials and a token will be generated.

I then used the following PHP function to access the Instagram API and retrieve my posts as JSON – I can then format these as necessary to display on the homepage. Just replace “INSERT_TOKEN_HERE” with the token generated at Pixel Union. There are various other endpoints available – just check the Instagram Developer website for full details.

function instagram_api_curl_connect( $api_url ){
   $connection_c = curl_init();
   curl_setopt( $connection_c, CURLOPT_URL, $api_url );
   curl_setopt( $connection_c, CURLOPT_RETURNTRANSFER, 1 );
   curl_setopt( $connection_c, CURLOPT_TIMEOUT, 20 );
   $json_return = curl_exec( $connection_c );
   curl_close( $connection_c );
   return json_decode( $json_return, true );
}

$result = instagram_api_curl_connect("https://api.instagram.com/v1/users/self/media/recent/?access_token=INSERT_TOKEN_HERE");

Leave a Reply

Your email address will not be published.