Get number of followers on instagram with PHP

Asked

Viewed 389 times

-2

I have the following code:

$username = 'makeup.pro.br';
        $response = @file_get_contents( "https://www.instagram.com/$username/?__a=1" );

        if ( $response !== false ) {
            $data = json_decode( $response, true );
            if ( $data !== null ) {
                $full_name = $data['graphql']['user']['full_name'];
                $follower  = $data['graphql']['user']['edge_followed_by']['count'];
                echo "{$full_name} have {$follower} followers.";
            }
        } else {
            echo 'Username not found.';
        }

        die();

It works perfectly on localhost. But when I put it on the air, it brings nothing, it doesn’t work.

I can’t find the reason why it doesn’t work in the air.

  • Takes the @ of @file_get_contents. It makes no sense for you to omit any error message when you don’t know what is wrong.

  • I’ve done it. It didn’t work either.

  • 1

    Is there an error message in the server logs? What is the value of $response and $data?

  • When I give an echo $Answer; on localhost, it brings me a json with the page information, but on the air, it just brings me a blank page with the Instagram symbol and nothing else. Could it be some configuration of my cloud server?

  • Instead of echo use var_dump($response); exit;

  • With var_dump, I returned this: string(38297) " "

  • Ok, now look at the source code of the page. It is returning an HTML or XML that the browser is trying to process.

  • Trying to process an HTML: string(38304) "<! DOCTYPE html> <html lang="en" class="no-js not-logged-in client-root"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title> Login • Instagram </title> <meta name="robots" content="noimageindex, noarchive"> ...

Show 3 more comments

1 answer

1

I managed to solve.

I contacted the support of my hosting and told me to do the command this way:

$response = file_get_contents($value->instagram_link . "?__a=1", false, stream_context_create(['socket' => ['bindto' => '0: 0']]));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.