Php 使用特定用户mgp25/instagram API获取我的instagram帖子
我正在使用mgp25/Instagram API 我怎样才能在instagram上找到像某个特定用户这样的帖子 我的代码:Php 使用特定用户mgp25/instagram API获取我的instagram帖子,php,instagram,instagram-api,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; $truncat
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";
}
尝试循环浏览你个人资料中的每个项目,然后找到喜欢的项目并找到用户名。然后,如果该用户将该项设置为like,则将其放入如下所示的项数组中:
// 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:
资料来源2:
资料来源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";
}
对于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()];
}
}
}
但是这个函数getRecentActivityInbox()返回的是什么呢?它返回的ActivityNewsResponse可以在这里找到:不是
foreach($item->getMedia()->>getLikers()作为$h)
,而是foreach($item->getLikers($item->getId())作为$h)
。但是$item->getComments($item->getId())
不起作用。有什么想法吗?