Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting basic information from Instagram using PHP

Tags:

php

instagram

$url = 'https://api.instagram.com/v1/users/XXXX?access_token=XXXX';
echo json_decode(file_get_contents($url))->{'followed_by'};

I am using this code and I do not understand what the issue is. I'm new to PHP so excuse the newbie mistake. I'm trying to get the "followed_by" to display on its own. I've managed to get Facebook's "like" and twitter's followers to display this way.

like image 832
Nazar Abubaker Avatar asked Jan 19 '13 12:01

Nazar Abubaker


People also ask

How do I retrieve data from Instagram?

Tap in the top right, then tap Your activity. Tap Download your information. Enter the email address where you'd like to receive a link to your data, then tap Request Download. Enter your Instagram account password and tap Next in the top right, then tap Done.

What information can you get from Instagram API?

The API can be used to get and publish their media, manage and reply to comments on their media, identify media where they have been @mentioned by other Instagram users, find hashtagged media, and get basic metadata and metrics about other Instagram Businesses and Creators.


3 Answers

In case you need to grab follower count (or other fields) without logging in, Instagram is nice enough to put them in JSON inside the page source:

$raw = file_get_contents('https://www.instagram.com/USERNAME'); //replace with user
preg_match('/\"edge_followed_by\"\:\s?\{\"count\"\:\s?([0-9]+)/',$raw,$m);
print intval($m[1]);

//returns "123"

Hope that helps.

24 May 2016 Updated to be more tolerant of spaces in JSON.

19 Apr 2018 Updated to use new "edge_" prefix.

like image 157
Ben Avatar answered Sep 30 '22 21:09

Ben


According to the Instagram API Docs, followed_by is a child of counts which is a child of data.

https://api.instagram.com/v1/users/1574083/?access_token=ACCESS-TOKEN

Returns:

{
"data": {
    "id": "1574083",
    "username": "snoopdogg",
    "full_name": "Snoop Dogg",
    "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg",
    "bio": "This is my bio",
    "website": "http://snoopdogg.com",
    "counts": {
        "media": 1320,
        "follows": 420,
        "followed_by": 3410
    }
}

The following should therefore work.

<?php 
$url = 'https://api.instagram.com/v1/users/XXXX?access_token=XXXX';
$api_response = file_get_contents($url);
$record = json_decode($api_response);
echo $record->data->counts->followed_by;

// if nothing is echoed try
echo '<pre>' . print_r($api_response, true) . '</pre>';
echo '<pre>' . print_r($record, true) . '</pre>';
// to see what is in the $api_response and $record object
like image 39
PassKit Avatar answered Sep 30 '22 20:09

PassKit


Try this..

<?php 
$instagram = "https://api.instagram.com/v1/users/xxxxx/?access_token=xxxxx";
$instagram_follows = json_decode(file_get_contents($instagram))->data->counts->followed_by;
echo $instagram_follows;
?>
like image 26
Cousin Richie Avatar answered Sep 30 '22 21:09

Cousin Richie