使用 PHP 从 Instagram 获取基本信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14414606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting basic information from Instagram using PHP
提问by Nazar Abubaker
$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.
我正在使用此代码,但我不明白问题是什么。我是 PHP 新手,所以请原谅新手的错误。我试图让“followed_by”自行显示。我已经设法让 Facebook 的“喜欢”和 Twitter 的关注者以这种方式显示。
回答by Ben
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:
如果您需要在不登录的情况下获取关注者数量(或其他字段),Instagram 可以很好地将它们放在页面源中的 JSON 中:
$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 2016Updated to be more tolerant of spaces in JSON.
2016 年 5 月 24 日更新为更能容忍 JSON 中的空格。
19 Apr 2018Updated to use new "edge_" prefix.
2018 年 4 月 19 日更新为使用新的“edge_”前缀。
回答by PassKit
According to the Instagram API Docs, followed_byis a child of countswhich is a child of data.
根据Instagram API Docs,followed_by是 的子项counts是 的子项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
回答by Cousin Richie
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;
?>
回答by Wasim A.
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 :)
这是我的代码:)

