0

I want to get an Instagram user account info (follower, following and account name).

I used this endpoint:

https://www.instagram.com/{username}/?__a=1

When I add the username in the endpoint, it displays a JSON page that includes a lot of data such as follower, following and account name,

I use this code in WordPress for parsing the JSON code in functions.php and used a shortcode to a page.:

function insta_shortcode_func() {
  $request = wp_remote_get('https://www.instagram.com/barkobco/?__a=1');

  if (is_wp_error($request)) {
    return false; // Bail early
  }

  $body = wp_remote_retrieve_body($request);

  $data = json_decode($body);

  return $data -> { 'count'};
}
add_shortcode('count_of_followers', 'insta_shortcode_func');

But nothing is displayed, I want to display follower, following and account name data.

1
  • You will probably have to debug this further yourself before stating a more specific question. Can you check if just executing the function 'echo insta_shortcode_func();' returns any data? The url seems to work fine at least. EDIT: Looking at the JSON it seems like there is no root property named count, you need to navigate the object properly Commented Jun 16, 2020 at 10:54

1 Answer 1

2

You need to return this to get the followers

return $data->graphql->user->edge_followed_by->count;

Here is the full code

function insta_shortcode_func()
{
    $request = wp_remote_get('https://www.instagram.com/barkobco/?__a=1');

    if (is_wp_error($request)) {
        return false; // Bail early
    }

    $body = wp_remote_retrieve_body($request);

    $data = json_decode($body);

    return $data->graphql->user->edge_followed_by->count;
}

add_shortcode('count_of_followers', 'insta_shortcode_func');

I think it will help you

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.