【问题标题】:show amount of Instagram followers on website在网站上显示 Instagram 关注者的数量
【发布时间】:2021-11-29 14:23:34
【问题描述】:

似乎 Instagram 改变了某些事情,因为我在我的 html 网站上尝试了几个代码来在按钮上显示 Instagram 关注者的数量,但没有任何效果。

我试过了:

<?php
$account='XXX';
$instagramsource=file_get_contents('https://instagram.com/' . $account);
preg_match_all('/"userInteractionCount":"(.*?)"/', $instagramsource, $count);
$followcount=$count[1][0];
echo "$account instagram account has $followcount followers";
?>

还有这个

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

还有这个……

<?php
$url = "https://www.instagram.com/XXX";
$json = file_get_contents($url);
$obj = json_decode($json, true);
$content = $obj['query']['results']['script']['content'];
$content = str_replace("window._sharedData =", "", $content);
$content = str_replace(";", "", $content);
$content = trim($content);
$json = json_decode($content);
$instagram_follower_count = $json->entry_data->ProfilePage{0}->user->followed_by->count;
?>

最后是这个:

<?php
$username = 'XXX';
$response = @file_get_contents( "https://www.instagram.com/$username/?__a=1" );
if ( $response !== false ) {
$data = json_decode( $response, true );
if ( $data !== null ) {
    $follows = $data['graphql']['user']['edge_follow']['count'];
    $followedBy  = $data['graphql']['user']['edge_followed_by']['count'];
    echo 'XXX follows:' . $follows . ' and is followed by: ' . $followedBy;
}
}
?>                                                      

没有任何作用。 谁能指出 2021 年会发生什么? 谢谢。

【问题讨论】:

    标签: javascript php count instagram


    【解决方案1】:

    这是因为 url https://www.instagram.com/$username/?__a=1 正在重定向到登录页面并给你一个 html 响应

    您可以通过echo $response查看

    这些帖子将对您有所帮助link1,link2

    【讨论】:

    • 请问这将如何工作?那么代码是什么样子的呢?
    • 查看已编辑的帖子
    • 谢谢,Dhruv,但是这些帖子对我来说太技术性了,我只是一个新手。 Instagram 似乎最近发生了一些变化,所以大多数脚本都不起作用,所以我正在寻找一个我可以轻松配置自己的脚本。谢谢。
    【解决方案2】:

    Instagram 自 2018 年 4 月 12 日起通过 __a=1 参数阻止访问。 __a=1 必须替换为 JS 和 Ajax 绕过。我一直在寻找替代解决方案。您可以在 php.ini 中使用 javascript 代码。例如:

    async function instagramFollowers () {
        
        const followers = []
        
        try {
            const userInfoSource = await Axios.get('https://www.instagram.com/123/')
    
            
            const jsonObject = userInfoSource.data.match(/<script type="text\/javascript">window\._sharedData = (.*)<\/script>/)[1].slice(0, -1)
    
            const userInfo = JSON.parse(jsonObject)
           
            const mediaArray = userInfo.entry_data.ProfilePage[0].graphql.user.edge_owner_to_timeline_media.edges.splice(0, 10)
            for (let media of mediaArray) {
                const node = media.node
                
                followers.push(node.thumbnail_src)
            }
        } catch (e) {
            console.error('Unable to retrieve Followers. Reason: ' + e.toString())
        }
        
        return followers
    }
    

    其他有用的链接:how to write javascript code inside php

    https://code.tutsplus.com/tutorials/how-to-use-ajax-in-php-and-jquery--cms-32494

    【讨论】:

    • 感谢您的代码。我把它放在 之间,我将 url 中的 123 更改为我的用户名,但它似乎不起作用。
    • 错误信息是什么?
    • 页面不再加载。在页面构建器中显示“意外的 T_function”
    • 嘿雾,请问有解决这个错误的方法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-21
    相关资源
    最近更新 更多