【问题标题】:Get my instagram posts with the likes of a particular user mgp25/Instagram-API使用特定用户 mgp25/Instagram-API 获取我的 Instagram 帖子
【发布时间】:2018-03-14 10:44:35
【问题描述】:

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

【问题讨论】:

    标签: php instagram instagram-api


    【解决方案1】:

    工作过

    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】:

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

      // 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

      通过阅读源文件,我还没有测试过它。

      【讨论】:

      • 但是这个函数 getRecentActivityInbox() 是什么?
      • 它返回 ActivityNewsResponse 可以在这里找到:github.com/mgp25/Instagram-API/blob/…
      • 不是foreach($item->getMedia()->getLikers() as $h),但foreach($item->getLikers($item->getId()) as $h) 有效。但是$item->getComments($item->getId()) 不起作用。有什么想法吗?
      【解决方案3】:

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

      发布更新

      $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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-30
        • 1970-01-01
        相关资源
        最近更新 更多