Pull users Instagram Feed with PHP
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I need a PHP script that pulls an Instagram users last 10 photos (Bonus Tip: Would like to control the number of photos displayed). The HTML markup should put each photo in a < LI > tag. I'm aware I'll have to provide Access Tokens generated from my Instagram account.

To clarify, do you want to connect with the API or just pull the images off the website without the API?
alex almost 7 years ago
I think it would better to connect with the API. What are your thoughts?
robertblake almost 7 years ago
@robertblake, I think API would be safer (it wouldn't break) but it would require you to sign up for an application code (I can guide you through the process); if you want something very simple, grabbing the posts directly from their site would require less setup but if they changed their code, the script would most likely break. I think I'll just do it both ways and let you decide :)
alex almost 7 years ago
Lets stick with using the API method. Thanks!
robertblake almost 7 years ago
Alright, will do.
alex almost 7 years ago
What about my solution?
EvgeniS almost 7 years ago
awarded to weslly
Tags
PHP
instagram

Crowdsource coding tasks.

2 Solutions


This PHP class provide you a simple way to interact with Instagr.am API.
http://code.google.com/p/php-instagram/

Example is included. Here is simple version of it.

try {
$instagram = new CheInstagram($client_id, $client_secret, $access_token);
$result = $instagram->get('/users/self/feed', array('count'=>10));

$my_feed = $result->response['data'];

echo '< ul>';

foreach ($my_feed as $photo) {
echo '< li>';
echo '< img src="' . $photo['link'] . '" />';
echo $photo['link'];
echo '< /li>';
}
echo '< /ul>';
} catch (InstagramException $e) {
 echo $e->getMessage();
}


Here's my solution, without using any additional class/lib.

<?php
    function fetch_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;
    }

    $count = 10; // the number of photos you want to fetch
    $access_token = "YOUR_ACCESS_TOKEN";
    $display_size = "thumbnail"; // you can choose between "low_resolution", "thumbnail" and "standard_resolution"

    $result = fetch_data("https://api.instagram.com/v1/users/self/feed?count={$count}&access_token={$access_token}");
    $result = json_decode($result);

    echo "<ul>";
    foreach ($result->data as $photo) {
        $img = $photo->images->{$display_size};
        echo "<li><a href='{$photo->link}'><img src='{$img->url}' /></a></li>";
    }
    echo "</ul>";
?>

Getting the access_token

If you don't have a registered client already, create one at: http://instagram.com/developer/clients/manage/

Put your Client ID and the redirect url of your client at the following URL and open it:

https://instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=token

You'll see an authorization confirmation screen, confirm it and you'll be redirected to your redirect url along with your access token

Just tried to test out this solution and none of the photos showed up. Perhaps I am not getting the right access token. Can you tell me how I need to obtain it? Otherwise, your solution looks great! Thanks!
robertblake almost 7 years ago
I've updated the solution with instructions to get the access_token.
weslly almost 7 years ago
Great i was able to get it to work. The only issue which is very minor is that in order for it to show 10 photos I have to put 11 in the count variable. No big deal really but just thought I would let you know. Thanks so much!
robertblake almost 7 years ago
I'm glad it worked. :)
weslly almost 7 years ago
View Timeline