获取我的Instagram帖子与特定用户mgp25 / Instagram-API

时间:2018-03-14 10:44:35

标签: php instagram instagram-api

我使用mgp25/Instagram-API

如何通过特定用户获得我的Instagram帖子?

我的代码:

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";
}

3 个答案:

答案 0 :(得分:3)

尝试循环浏览个人资料中的每个项目,然后获取相似内容并找到用户名。然后,如果该项目具有该用户的喜欢,则将其放入项目数组中,如下所示:

// Get the UserPK ID for "natgeo" (National Geographic).
$userId = $ig->people->getUserIdForName('natgeo');
// Starting at "null" means starting at the first page.
$maxId = null;
do {

    $response = $ig->timeline->getUserFeed($userId, $maxId);
    // In this example we're simply printing the IDs of this page's items.
    foreach ($response->getItems() as $item) {
       //loop through likes as u can see in [source 1][1] there is some method called 'getLikers()' which u can call on a media object.
        foreach($item->getMedia()->getLikers() as $h){
            // here do some if with if response user == username
        }
    }

来源1:https://github.com/mgp25/Instagram-API/blob/master/src/Request/Media.php 来源2:https://github.com/mgp25/Instagram-API/tree/master/examples 来源3:https://github.com/mgp25/Instagram-API/blob/e66186f14b9124cc82fe309c98f5acf2eba6104d/src/Response/MediaLikersResponse.php

通过阅读源文件,这可行,我还没有测试过。

答案 1 :(得分:3)

<强>曾为

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";
}

答案 2 :(得分:2)

对于新版本的mgp25,此代码工作正常

POST UPDATED

$likes = [];
$comments = [];
$userId = $ig->people->getUserIdForName($username);
$maxId = null;
$response = $ig->timeline->getUserFeed($userId, $maxId);
$posts = $response->jsonSerialize();
foreach ($response->getItems() as $item) {
    $likers = $ig->media->getLikers($item->getId());
    if ($likers != null) {
        foreach ($likers->getUsers() as $h) {
            $likes[] = ['id' => $item->getId(), 'username' => $h->getUsername()];
        }
    }
    $commentsList = $ig->media->getComments($item->getId());
    if ($commentsList != null) {
        foreach ($commentsList->getComments() as $c) {
            $comments[] = ['id' => $item->getId(), 'username' => $c->getUser()->getUsername(), 'text' => $c->getText()];
        }
    }
}

updated reference link