从Instagram API获取帖子数

时间:2019-07-22 10:45:12

标签: instagram-api

我正在尝试获取instagram帐户的帖子数,我设法做到了追随者和关注,但无法正确获取帖子数。

<?php
$username = 'instagram';

$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'];
        $follows = $data['graphql']['user']['edge_follow']['count'];
        echo "<p>{$full_name}</p> <p>{$follower} followers {$follows} following.</p>";
    }
} else {
echo 'Username not found.';
}
?>

1 个答案:

答案 0 :(得分:0)

如果有人需要答案,我就设法解决了...

<?php
$username = 'instagram';

$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'];
      $follows = $data['graphql']['user']['edge_follow']['count'];
      $posts = $data['graphql']['user']['edge_owner_to_timeline_media']['count'];
      echo "<h2><a href='https://www.instagram.com/{$username}'>{$full_name}</a></h2> 
      <p><span>{$posts} posts</span> <span>{$follower} followers</span>  <span>{$follows} following</span></p>";
     }
} else {
echo 'Username not found.';
}
?>

只需转到 https://www.instagram.com/ $ username /?__ a = 1 并将$ username更改为您需要查看的帐户