为了账号安全,请及时绑定邮箱和手机立即绑定

使用 PHP 调用公共 Instagram JSON 数据并将它们分配给变量

使用 PHP 调用公共 Instagram JSON 数据并将它们分配给变量

PHP
素胚勾勒不出你 2021-06-30 13:04:05
目标:我的目标是获取 Instagram 帐户最后 3 个帖子的喜欢和评论总数,并将它们分配给单独的变量。问题:我只能调用最新帖子的总赞和评论,我不确定如何调用其他帖子信息。我已经尝试了几个小时。目标的逻辑解释:$post1_likes = number of likes from post;$post1_comments = number of comments from post;$post2_likes = number of likes from post;$post2_comments = number of comments from post;$post3_likes = number of likes from post;$post3_comments = number of comments from post;$combine_all = $post1_likes + $post1_comments + $post2_likes + $post2_comments $post3_likes + $post3_comments;return combine_all;当前代码:$raw = file_get_contents('https://www.instagram.com/instagram');preg_match('/\"edge_media_preview_like\"\:\s?\{\"count\"\:\s?([0-9]+)/',$raw,$m1);preg_match('/\"edge_media_to_comment\"\:\s?\{\"count\"\:\s?([0-9]+)/',$raw,$m2);$combine_all = $m2[1] + $m1[1];return $combine_all;
查看完整描述

2 回答

?
紫衣仙女

TA贡献1839条经验 获得超15个赞

在您获取信息之前,您阅读的 JSON 具有相当多的数据层。下面的代码应该让你开始了解你需要做什么......


// Decode the data to a nested associative array

$json = json_decode($raw, true);


// Loop through each of the edges

foreach ( $json['graphql']['user']['edge_felix_video_timeline']['edges'] as $edge ){

    echo $edge['node']['edge_media_to_comment']['count'].'/'

        .$edge['node']['edge_media_preview_like']['count'].PHP_EOL;

}


查看完整回答
反对 回复 2021-07-09
  • 2 回答
  • 0 关注
  • 177 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信