-1

I am trying to do a Instagram Follower count in PHP. (as JavaScript isn't working)

I got this PHP script but it's not working.

<?php
$url = 'https://www.instagram.com/cristiano/';
$str = file_get_contents($url);
$count = 0;
if(preg_match('#followed_by": {"count": (.*?)}#', $str, $match)) {
     $count = $match[1];
} echo $count;
?>
j08691
  • 204,283
  • 31
  • 260
  • 272
SayBored
  • 3
  • 1
  • 2
    I assume because your preg_match statement returns false and so count it never updated. – DevWithZachary Jul 06 '21 at 14:51
  • 1
    You should check the content of `$str`, as https://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen might be disabled – JamesHalsall Jul 06 '21 at 14:52
  • Also, that string probably doesn't exist in the HTML that you are downloading. Instead, it exists in the DOM which is generated by JavaScript. – Chris Haas Jul 06 '21 at 14:56
  • I think that `$str` is an HTML, but I don't know lot about of PHP. – SayBored Jul 06 '21 at 15:02
  • **Off-topic: Cannot Reproduce** / **Needs Debugging Details** https://3v4l.org/Jtu8K If this is not about the `preg_match()` call, but about scraping the url, then remove everything after the `file_get_contents()` line in your question. Anything helpful to you in https://stackoverflow.com/q/35234096/2943403 ? – mickmackusa Jul 06 '21 at 15:05
  • i don't understand...? i want it to get the followers of an Instagram account, getting the HTML of the ig account || or using the url/?__a=1 will print a JSON – SayBored Jul 06 '21 at 15:11
  • Why are you doing this using a regexp instead of parsing the JSON? – Barmar Jul 06 '21 at 15:22

1 Answers1

1

For Your Kind Information ->>

1. Instagram Has Changed #followed_by To #edge_followed_by (source: view-source:https://www.instagram.com/cristiano), This Is One Of The Reasons Why It Is Returning 0.

2. Add "?__a=1" To Insta Profile Url (https://www.instagram.com/cristiano/?__a=1) And You Will Get Json, Just Decode Json And Get The Value.

3. These Are Not Legal Ways To Get Information From Instagram. I Prefer To Use Instagram API, Which Is Easy To Use And Free.

UPDATE

4. Or You Can Get Followers From <meta> Tags Instead Of <script> Tags (Example - <meta content="310m Followers, 476 Following, 3,095 Posts - See Instagram photos and videos from Cristiano Ronaldo (@cristiano)" name="description" /> )