Is there a way to pull an instagram feed with PHP without logging in?

↘锁芯ラ 提交于 2019-12-20 15:30:21

问题


It seems that this solution no longer works -

How to get a user's Instagram feed

The new API requires an access token which is dynamically assigned after passing through a login page. Is there a way to still pull a feed programmatically through PHP without jumping through the new oauth hoops? This is useful for setting a crontab to automatically save new posts to a database.


回答1:


Yes you can. You don't need to login or access_token to get the latest 20 posts. You just need to parse the json content from https://www.instagram.com/[USERNAME]/media/. Replace the [username] with the instagram user_name.

eg.

$instaResult = file_get_contents('https://www.instagram.com/'.$username.'/media/');
$insta = json_decode($instaResult);

UPDATE: Instagram has changed the user media rss url. In order to get the rss feed you now have to use https://www.instagram.com/[USERNAME]/?__a=1

UPDATE: The above solutions are depreciate, a access token is mandatory to fetch the instagram feed. So dont waste your time to look for solutions that do not require access token.




回答2:


Try this php library: https://github.com/postaddictme/instagram-php-scraper

And here is example (https://github.com/postaddictme/instagram-php-scraper/blob/master/examples/getAccountMediasByUsername.php):

<?php
require __DIR__ . '/../vendor/autoload.php';
// If account is public you can query Instagram without auth
$instagram = new \InstagramScraper\Instagram();
$medias = $instagram->getMedias('kevin', 25);
// Let's look at $media
$media = $medias[0];
echo "Media info:\n";
echo "Id: {$media->getId()}\n";
echo "Shotrcode: {$media->getShortCode()}\n";
echo "Created at: {$media->getCreatedTime()}\n";
echo "Caption: {$media->getCaption()}\n";
echo "Number of comments: {$media->getCommentsCount()}";
echo "Number of likes: {$media->getLikesCount()}";
echo "Get link: {$media->getLink()}";
echo "High resolution image: {$media->getImageHighResolutionUrl()}";
echo "Media type (video or image): {$media->getType()}";
$account = $media->getOwner();
echo "Account info:\n";
echo "Id: {$account->getId()}\n";
echo "Username: {$account->getUsername()}\n";
echo "Full name: {$account->getFullName()}\n";
echo "Profile pic url: {$account->getProfilePicUrl()}\n";
// If account private you should be subscribed and after auth it will be available
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', 'path/to/cache/folder');
$instagram->login();
$medias = $instagram->getMedias('private_account', 100);



回答3:


We can still access the photos. But just last 12 photo. If want more photos access_token require. This my way to get photos.

$instaResult = file_get_contents('https://www.instagram.com/'.$username.'/?__a=1');
$insta = json_decode($instaResult);
$instagram_photos = $insta->graphql->user->edge_owner_to_timeline_media->edges;

Now we get the last photos array.

Than i used in the view with foreach loop.

<ul>
@foreach($instagram_photos as $instagram_photo)
   <li>
       <img src="{{$instagram_photo->node->display_url}}">
   </li>
@endforeach
</ul>



回答4:


This does not work anymore:

https://www.instagram.com/zahidrasheed/?__a=1

This works:

https://www.instagram.com/zahidrasheed/?__a=1


来源:https://stackoverflow.com/questions/37580783/is-there-a-way-to-pull-an-instagram-feed-with-php-without-logging-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!