使用PHP调用Public Instagram JSON数据并将其分配给变量

时间:2019-05-25 06:31:43

标签: php arrays json

目标:

我的目标是获取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;

链接到数据:

link to json data

我是PHP新手,非常感谢您的帮助! 谢谢

2 个答案:

答案 0 :(得分:2)

使用CURL进行时间优化的第二个答案,这只会添加前三个值

$website_url = 'https://www.instagram.com/instagram/?__a=1';
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_URL, $website_url);
curl_setopt($curl, CURLOPT_REFERER, $website_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0(Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/66.0');
$str = curl_exec($curl);
curl_close($curl);
$json = json_decode($str, true);

$combine_all=0; $loop = 0; 
foreach($json['graphql']['user']['edge_felix_video_timeline'] ['edges'] as $key=>$node){
        if(($node['node']['edge_media_to_comment']['count']>0 || $node['node']['edge_media_preview_like']['count']>0) && ($loop<4)){ // if you need add first only three then $loop<4
            $combine_all += $node['node']['edge_media_preview_like']['count'] + $node['node']['edge_media_to_comment']['count'];
            $loop++;
        }
}
echo "combine_all : ";
print_r($combine_all);
  

样品输出   Combine_all:4415112

只需解码为Array,然后循环该数组即可得到计数。 测试过的作品

$data = file_get_contents('https://www.instagram.com/instagram/?__a=1');
$data = json_decode($data,true);
echo'<PRE>';
$combine_all=0;
foreach($data['graphql']['user']['edge_felix_video_timeline'] ['edges'] as $key=>$node){
        if($node['node']['edge_media_to_comment']['count']>0 || $node['node']['edge_media_preview_like']['count']>0)
            $combine_all += $node['node']['edge_media_preview_like']['count'] + $node['node']['edge_media_to_comment']['count'];
}
echo "combine_all : ";
print_r($combine_all);
  

//输出

combine_all : 12693471

答案 1 :(得分:1)

在获取后续信息之前,您所阅读的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;
}