Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP调用公共Instagram JSON数据并将其分配给变量_Php_Arrays_Json - Fatal编程技术网

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

使用PHP调用公共Instagram JSON数据并将其分配给变量,php,arrays,json,Php,Arrays,Json,目标: $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 = numb

目标:

$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;
我的目标是获得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;
我可以调用的总喜欢和评论的最新职位,我不知道如何调用其他职位的信息。我已经试了好几个小时了

目标的逻辑解释:

$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;
当前代码:

$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\u get\u contents('https://www.instagram.com/instagram');
preg\u match(“/\”edge\u media\u preview\u like\”\:\s?{“count\”\:\s?([0-9]+)/”,$raw,$m1);
preg\u match('/\'edge\u media\u to\u comment\\:\s?\{\'count\'\:\s?([0-9]+)/',$raw,$m2);
$combine_all=$m2[1]+$m1[1];
返回$combine\u all;
数据链接:

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

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

在阅读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;
}

第二个答案是使用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);

$raw
是否包含JSON?如果是的话,$raw包含我附加的链接中的JSON数据。我已经遇到过这个问题,但我发现很难理解:(这似乎包括数组中的所有帖子,而不是最后3篇(这很好)。我已经实现了这段代码,但是它似乎会大大降低页面速度,你知道为什么吗?它甚至会导致网关超时。。是的,因为你从应用程序/网站上抓取数据会占用大量数据。当然,获取特定数据需要时间。我们可以使用它进行检查,这样可以缩短时间。@cactusboat现在使用curl it gi检查我的第二个答案我的结果很快就和上一个相比了。如果很好,别忘了把这个标记为回答谢谢@Rasa Mohamed!我已经用我自己的代码实现了curl部分,它使我的网站从GTMetrix上的16秒降到了2秒!难以置信,谢谢!