1

GOAL:

My goal is to get the total number of likes and comments of the last 3 posts of an Instagram account, and assign them to separate variables.

ISSUE:

I am able to call the total likes and comments of the latest post only, I am not sure how to call for the other posts information. I have been trying for hours.

LOGICAL EXPLANATION OF GOAL:

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

CURRENT CODE:

$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 DATA:

link to json data

I am a PHP newbie and would appreciate your help! Thank you

cactusboat
  • 778
  • 2
  • 7
  • 15
  • Does `$raw` contain JSON - if so - https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php – Nigel Ren May 25 '19 at 06:34
  • $raw contains the JSON data from the link I've attached. I've already come across this but I'm finding it hard to understand :( – cactusboat May 25 '19 at 06:53

2 Answers2

1

The JSON your reading has quite a few layers of data before you get to the information your after. The following code should give you a start on what you need to do...

// 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;
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
1

SECOND ANSWER FOR TIME OPTIMIZATION USING CURL and this will add only first three values

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

SAMPLE OUTPUT combine_all : 4415112

Just decode to Array , then loop that array you will get the count. TESTED ITS WORKED

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

// Output

combine_all : 12693471
Rasa Mohamed
  • 882
  • 1
  • 6
  • 14
  • It seems that this counts ALL of the posts in the array and not the last 3 (which is fine). I have implemented this code, however it seems to slow down the page A LOT, would you have any idea why? It even causes gateway timeouts.. – cactusboat May 25 '19 at 11:51
  • Yes, because your scraping data from app/website tats large data. Sure it took time to fetch specific data. We can check with [CURL()](https://www.php.net/manual/en/curl.examples-basic.php) It could reduce the time. – Rasa Mohamed May 25 '19 at 15:18
  • @cactusboat Now check my second answer, using curl it give result soon compare to previous one. If its fine dont forget mark this as answer – Rasa Mohamed May 26 '19 at 04:59
  • Thank you @Rasa Mohamed! I have implemented the curl section to my own code and it has brought my website back down to 2 seconds from 16 seconds on GTMetrix! Incredible, thank you! – cactusboat May 28 '19 at 04:24