Reputation: 505
$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.
Upvotes: 8
Views: 30537
Reputation: 451
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.
Upvotes: 29
Reputation: 9890
function get_https_content($url=NULL,$method="GET"){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0');
curl_setopt($ch, CURLOPT_URL,$url);
return curl_exec($ch);
}
function ig_count($username) {
return json_decode(get_https_content("https://api.instagram.com/v1/users/1460891826/?client_id=ea69458ef6a34f13949b99e84d79ccf2"))->data->counts->followed_by;
}
Here is my code :)
Upvotes: 1
Reputation: 1283
Try this one...
$url = 'https://api.instagram.com/v1/users/USER_ID?access_token=YOUR_TOKEN';
$api_response = file_get_contents($url);
$record = json_decode($api_response);
echo $followed_by = $record->data->counts->followed_by;
Click to get all info of user
Upvotes: 0
Reputation: 41
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;
?>
Upvotes: 2
Reputation: 12591
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
Upvotes: 2