Search code examples
phpinstagraminstagram-api

Get my instagram posts with the likes of a particular user mgp25/Instagram-API


I am use mgp25/Instagram-API

How can l get my instagram posts with the likes of a particular user?

My code:

set_time_limit(0);
date_default_timezone_set('UTC');
require __DIR__.'/vendor/autoload.php';

$username = 'myInstagramUsername';
$password = 'myInstagramPassword';
$debug = false;
$truncatedDebug = false;

$ig = new \InstagramAPI\Instagram($debug, $truncatedDebug);
try {
    $ig->login($username, $password);
} catch (\Exception $e) {
    echo 'Something went wrong: '.$e->getMessage()."\n";
    exit(0);
}
try {

    $userId = $ig->people->getUserIdForName($username);
    $act = json_encode($ig->people->getRecentActivityInbox(), true);
    ???????

} catch (\Exception $e) {
    echo 'Something went wrong: '.$e->getMessage()."\n";
}

Solution

  • Worked

    set_time_limit(0);
    date_default_timezone_set('UTC');
    require __DIR__.'/vendor/autoload.php';
    
    $username = 'username';
    $password = 'password';
    $debug = false;
    $truncatedDebug = false;
    
    
    $ig = new \InstagramAPI\Instagram($debug, $truncatedDebug);
    try {
        $ig->login($username, $password);
    } catch (\Exception $e) {
        echo 'Something went wrong: '.$e->getMessage()."\n";
        exit(0);
    }
    try {
    
        $posts = [];
        $comments = [];
    
        $userId = $ig->people->getUserIdForName($username);
        $maxId = null;
    
            $response = $ig->timeline->getUserFeed($userId, $maxId);
            foreach ($response->getItems() as $item) {
                foreach($item->getLikers($item->getId()) as $h){
                    $posts[] = ['id' => $item->getId(), 'username' => $h->username];
                }
    
                foreach($ig->media->getComments($item->getId()) as $v){             
                    if(count($v->comments) > 0){
                        foreach($v->comments as $c){
                            $comments[] = ['id' => $item->getId(), 'username' => $c->user->username, 'text' => $c->text];
                        }
                    }
                }
    
            }
    
            print_r($posts);
            print_r($comments);
    
    } catch (\Exception $e) {
        echo 'Something went wrong: '.$e->getMessage()."\n";
    }