【问题标题】:Getting basic information from Instagram using PHP使用 PHP 从 Instagram 获取基本信息
【发布时间】:2013-01-03 02:08:18
【问题描述】:
$url = 'https://api.instagram.com/v1/users/XXXX?access_token=XXXX';
echo json_decode(file_get_contents($url))->{'followed_by'};

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

【问题讨论】:

  • 抱歉,此页面不可用。

标签: php instagram


【解决方案1】:

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

希望对您有所帮助。

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

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

【讨论】:

  • 经过 小时 尝试使用 Instagram API 实现此功能后,我最终使用了此解决方案。 谢谢。我从未见过的最糟糕的 API 设计和文档。您甚至必须发送一个截屏视频,说明您想用 API 做什么才能摆脱沙盒模式。我只想获取任何给定用户的关注者数量,这无论如何都是公共信息。太疯狂了。
  • 很好的解决方案!我目前在令牌过期或 api 放弃时将其用作后备:p
  • @Ben 这个解决方案在几周前对我来说非常有效,我猜 Instagram 可能已经改变了页面上的某些内容?您介意检查和更新解决方案吗?我现在收到一个未定义的偏移错误(对于 $m[1])。谢谢!
  • @user6122500 感谢您的提醒。看起来他们已经为变量名添加了前缀,所以我更新了上面的示例代码。很高兴它对您有所帮助!
  • 任何个人资料的关注人数如何?他们关注了多少人。
【解决方案2】:

根据Instagram API Docsfollowed_bycounts 的子代data 的子代。

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
    }
}

因此,以下应该有效。

<?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

【讨论】:

  • 我刚刚尝试了您提供的代码,但它不起作用(我自己尝试了两个 echo)。我什至确定我的 api.instagram.com/xxx 是有效的。
  • @NazarAbubaker - 你确定你的 access_token 是正确的 - 你是如何生成它的?试试上面编辑过的代码,让我们知道 API 实际返回了什么。
  • 我用它来生成 access_token [link]jelled.com/instagram/access-token[/link]。取自 [link]stackoverflow.com/questions/12677551/… 我得到的只是“警告:file_get_contents() [function.file-get-contents]:无法找到包装器“https”——在配置 PHP 时是否忘记启用它?在第 36 行的 XXX 中"
  • @NazarAbubaker - 那么 $api_response 显示了什么?如果您手动将生成的 URL 粘贴到网络浏览器中会发生什么?
  • @NazarAbubaker - 对于 PHP 错误,请参阅此问题中带有 31 个赞成票的评论stackoverflow.com/questions/5444249/…
【解决方案3】:

试试这个..

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

【讨论】:

    【解决方案4】:
    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;
    }
    

    这是我的代码:)

    【讨论】:

      【解决方案5】:

      试试这个...

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

      【讨论】:

        猜你喜欢
        • 2018-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-30
        • 2012-08-09
        • 1970-01-01
        相关资源
        最近更新 更多