使用PHP从Instagram获取基本信息

使用PHP从Instagram获取基本信息,php,instagram,Php,Instagram,我正在使用此代码,我不明白问题是什么。我是PHP新手,请原谅我的新手错误。我正试图让“following_by”自行显示。我已经设法让Facebook的“like”和twitter的关注者以这种方式显示出来。根据,后跟是计数的子项,它是数据的子项 $url = 'https://api.instagram.com/v1/users/XXXX?access_token=XXXX'; echo json_decode(file_get_contents($url))->{'followed_b

我正在使用此代码,我不明白问题是什么。我是PHP新手,请原谅我的新手错误。我正试图让“following_by”自行显示。我已经设法让Facebook的“like”和twitter的关注者以这种方式显示出来。

根据,
后跟
计数的子项,它是
数据的子项

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

https://api.instagram.com/v1/users/1574083/?access_token=ACCESS-TOKEN
因此,以下几点应该是可行的

{
"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
    }
}
试试这个

$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;

试试这个

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;
}
获取用户的所有信息

$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"

这是我的代码:)

如果您需要在不登录的情况下获取关注者计数(或其他字段),Instagram很好,可以将它们放在页面源代码中的JSON中:

希望有帮助

2016年5月24日更新后,对JSON中的空格更加宽容


2018年4月19日更新为使用新的“edge”前缀。

我刚刚尝试了您给出的代码,但它不起作用(我自己尝试了两种echo)。我甚至已经确定了我的api.instagram.com/xxx是有效的,并且是有效的。@NazarAbubaker-你确定你的访问令牌是正确的吗?你是如何生成它的?尝试上面编辑的代码,让我们知道API实际返回的是什么。我用它来生成access_令牌[link]。从[link]获取的信息是“警告:文件\u get\u contents()[]:找不到包装器“https”-您在配置PHP时忘记启用它了吗?在XXX的第36行“@NazarAbubaker”-那么$api\u响应显示了什么?如果您手动将生成的URL粘贴到web浏览器中会发生什么情况?@NazarAbubaker-有关PHP错误,请参阅此问题中的注释,在几个小时后尝试使用Instagram API实现此功能,我最终使用了此解决方案。非常感谢。我从未见过最差的API设计和文档。您甚至需要发送一个屏幕广播,解释您希望如何使用API摆脱沙箱模式。我只想得到任何给定用户的追随者数量,这是公开信息。这太疯狂了,很好的解决方案!我目前在令牌过期或api放弃时将其用作备用:p@Ben直到几周前,这个解决方案还对我非常有效,我猜Instagram可能改变了页面上的某些内容?您介意检查并更新解决方案吗?我现在得到一个未定义的偏移错误(对于$m[1])。谢谢@用户6122500谢谢你的提醒。看起来他们在变量名中添加了前缀,所以我更新了上面的示例代码。很高兴这有帮助!任何个人资料的以下计数是多少?他们关注多少人。抱歉,此页面不可用。
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;
}
$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"