使用PHP从Instagram获取基本信息

Naz*_*ker 7 php instagram

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

我正在使用此代码,我不明白这是什么问题.我是PHP的新手,所以请原谅新手的错误.我正试图让"follow_by"自行显示.我设法让Facebook的"喜欢"和推特的粉丝以这种方式展示.

Ben*_*Ben 24

如果您需要在不登录的情况下获取关注者数(或其他字段),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"
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.

2016年5月24日更新为更容忍JSON中的空格.

2018年4月19日更新为使用新的"edge_"前缀.

  • 经过**小时**尝试使用Instagram API实施此操作后,我最终使用了此解决方案。**谢谢**。我从未见过的最糟糕的API设计和文档。您甚至必须发送_screencast_解释要退出该沙箱模式要使用API​​进行的操作。我只想获取任何给定用户的关注者人数,无论如何这都是公共信息。疯了 (2认同)

Pas*_*Kit 2

根据Instagram API 文档followed_by是 的子级counts, 是 的子级data

https://api.instagram.com/v1/users/1574083/?access_token=ACCESS-TOKEN
Run Code Online (Sandbox Code Playgroud)

返回:

{
"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
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,以下内容应该有效。

<?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
Run Code Online (Sandbox Code Playgroud)