PHP Instagram API usage

PHP English

Hello friends, in this article, I will explain the use of the API called instagram-user-feed, which a team uses unofficially.

What can be done with Instagram API?

  • You can pull the user's profile data.
  • You can capture media shared by the user.
  • You can capture user's stories.
  • You can pull user highlight stories.
  • You can pull the details of the post shared by the user.
  • You can pull the list of followers
  • You can pull the list of followed people
  • You can follow or unfollow users.
  • You can like or withdraw your like of the posts.
  • You can pull hashtag posts.
  • You can pull comments on a post.
  • You can capture live broadcast information.
  • You can shoot reel content.
  • You can capture IGTV content.
  • You can pull posts in which a user is tagged.
  • You can take HD profile picture.
  • You can comment on a post.

Don't you think I'm very excited? Let's move on to how to set up the API and how to use it.

Instagram API Setup

We will install the API with Composer. Open the Terminal program you are using, access the folder where you will run the project, and install it.

composer require pgrimaud/instagram-user-feed

Instagram API Usage

Now let's talk about how to use the API.

require "vendor/autoload.php";
use Instagram\Api;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
$cachePool = new FilesystemAdapter('Instagram', 0, __DIR__ . '/../cache');

$api = new Api($cachePool);
$api->login('username', 'password');

As you see above, we need to define an Instagram account in order to perform the actions. Now that we have included the library, let's start taking action.

We Capture User's Profile Information

$profile = $api->getProfile('eycreative'); // Çekilecek profilin kullanıcı adı.

echo "<pre>";
// print_r($profile) // Let's print it all on the screen to see what's in the array. Let's print it all on the screen to see what's in the array.
echo "</pre>";

echo $profile->getuserName();
echo $profile->getfullName();
echo $profile->getbiography();
echo $profile->getfollowers();
echo $profile->getfollowing();
echo $profile->getprofilePicture();
echo $profile->getexternalUrl();
echo $profile->getmediaCount();

print_r(); You can print all the profile detail information in the array we print with.

We Take Shipment Details

$medias = $profile->getMedias(); // Medyaların bulunduğu diziyi döngüye sokabilmek için ekleyiniz.

foreach ($medias as $media) {
 echo "<pre><hr>";
 echo 'ID        : ' . $media->getId() . "\n";
 echo 'Caption   : ' . $media->getCaption() . "\n";
 echo 'Link      : ' . $media->getLink() . "\n";
 echo 'Likes     : ' . $media->getLikes() . "\n";
 echo 'Date      : ' . $media->getDate()->format('Y-m-d h:i:s') . "\n\n";
 echo "<pre>";
}

With the loop above, we can pull some of the media shared by the user because the API does not allow us to pull all the shares at once. If we do not use pagination, we will see only 12 data coming. So how do we pull all the data?

do {
 foreach ($medias as $media) {
  echo "<pre><hr>";
  echo 'ID        : ' . $media->getId() . "\n";
  echo 'Caption   : ' . $media->getCaption() . "\n";
  echo 'Link      : ' . $media->getLink() . "\n";
  echo 'Likes     : ' . $media->getLikes() . "\n";
  echo 'Date      : ' . $media->getDate()->format('Y-m-d h:i:s') . "\n\n";
  echo "<pre>";
 }
  sleep(1);
} while ($profile->hasMoreMedias());

Now, as you scroll down our page, all data will start to change.

Actions to Follow and Unfollow Users

Let's take our favorite and simplest action.

// Follow Action
try{
    $userId = 3266224637; // ID of the user to follow
    $follow = $api->follow($userId);
    echo $follow . PHP_EOL;

    if($follow == TRUE){
        echo "Request Sent";
    }else{
        echo "NO";
    }

 } catch (InstagramException $e){
    print_r($e->getMessage());
 }catch (CacheException $e){
    print_r($e->getMessage());
 }

// Unfollow Action

try{
     $userId = 3266224637; // ID of the user to be unfollowed
     $unfollow = $api->unfollow($userId);
     echo $unfollow . PHP_EOL;
    
      if($unfollow == TRUE){
          echo "Unfollow Okey";
      }else{
          echo "NO";
      }
    
     } catch (InstagramException $e){
        print_r($e->getMessage());
     }catch (CacheException $e){
        print_r($e->getMessage());
     }

I received a 400 error "checkpoint_required", what can I do?

If you receive this error while making a query, log in to your defined Instagram account and select the "I did this action" option. I have been trying the API from a fake account that I have opened for a week, but I have not experienced any problems yet. I hope you don't have any problems.

Learn More About the API

As we mentioned at the beginning of our article, the usage area of ​​​​the API is very wide. In this article, I explained its simple installation and use by giving a few examples. You can check out the API's GitHub Documentation for more information.
You can access all other examples from the Examples folder in the API's GitHub Documentation.

Conclusion

As you can see, the API whose installation and use I have explained is a useful project, but as I mentioned at the beginning of our article, this API is a third-party unofficial API developed to access the Instagram platform through unofficial means, what I mean is that the API is currently smooth and stable. It works somehow, but this situation may change in the coming days, so when using this API in your projects, be prepared for situations we do not want to happen.

Thank you for listening to me, you can specify the questions you want to ask in the comments.


Yorumlar (0)

    Bu yazıya henüz bir yorum yapılmamış! İlk yorum yapan sen ol!