如何从一个 Instagram 帐户获取关注者列表?

2024-03-28

我正在建立一个网站,我需要的只是一个 Instagram 帐户的关注者列表。我已经完成了使用 auth 2.0 验证我的网络应用程序的步骤。我刚刚意识到,通过此身份验证,我只能访问每个访问令牌所属帐户的关注者。

有没有其他方法可以从我想要的帐户访问关注者?

https://api.instagram.com/v1/users/4082347837/followed-by?access_token=AccessToken https://api.instagram.com/v1/users/4082347837/followed-by?access_token=AccessToken

API 请求的输出:-

{ ["meta"]=> object(stdClass)#2 (3) { ["error_type"]=> string(18) "APINotAllowedError" ["code"]=> int(400) ["error_message"]=> string(29) "you cannot view this resource" } }

可能您的 client_id 处于沙盒模式。您无法从白名单之外的帐户获取信息。如果您将应用程序发送给审核,您可以离开沙盒模式。

但有一个更简单的解决方案。只需一通电话即可从网页版(无API)获取公共信息:

$otherPage = 'nasa';
$response = file_get_contents("https://www.instagram.com/$otherPage/?__a=1");
if ($response !== false) {
    $data = json_decode($response, true);
    if ($data !== null) {
        $follows = $data['user']['follows']['count'];
        $followedBy = $data['user']['followed_by']['count'];
        echo $follows . ' and ' . $followedBy;
    }
}

Update.抱歉,我误解了你的问题。无需API即可获取列表。您需要 cookie 中的 csrf 令牌和用户 id,然后调用查询:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://www.instagram.com/query/");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

//You need the csrftoken, ds_user_id
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: ..."));

curl_setopt($ch, CURLOPT_POST, 1);
$userId = 528817151;
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "q=ig_user($userId) {
  followed_by.first(10) {
    count,
    page_info {
      end_cursor,
      has_next_page
    },
    nodes {
      id,
      is_verified,
      followed_by_viewer,
      requested_by_viewer,
      full_name,
      profile_pic_url,
      username
    }
  }
}");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
var_dump($server_output);

您可以在 Instagram 网站上执行登录操作来获取正确的 cookie。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从一个 Instagram 帐户获取关注者列表? 的相关文章

随机推荐