On June 1, 2016, Instagram
will disconnect from its API all applications that have not been moderated. What if you are among them?
Prehistory
We make a service for posting to Instagram on a schedule and use the API to get information about accounts. The posting itself is handled by phones in automatic mode. We were denied access to the API after June 1 (we tried moderation twice), so it was decided to find a replacement.
First, I’ll tell you how we used the official API:
- When adding an account we take away account information from Instagram: name, profile photo, number of posts, subscribers, subscriptions.
- Before publishing a photo / video, we request the number of posts, and the same after publication, if the number of posts has increased, we consider the publication successful.
- If the publication was successful, we take away the link to the latest profile photo.
- If the user deletes a photo from our service, then before completing the task, you need to check whether such a post exists in Instagram (or it was deleted).
Implementation
Instagram has a
web version . Using it
in private accounts you can get information about the number of posts, subscriptions and subscribers, and
in public even the posts themselves, comments and likes. Therefore, due to the simplicity of the receipt, I thought that such libraries had already been written. Went to google and found only for
NodeJS . And there was some code for
PHP , but it didn’t match all four points. As a result, it was decided to write their own library.
I will not go into details, as you can see the
code on GitHub . I'll tell you only the key points.
')
Getting account information
If you go to the profile (for example,
instagram.com/kevin ) and look at the source code of the page, then scroll down to see the object that is wired directly into the JSON page with information about the account.
It's pretty easy to pull it out (for convenience, I used
mashape / unirest-php ), parse and write to the array:
$response = Request::get('https://instagram.com/kevin'); $pageString = $response->body(); $arr = explode('window._sharedData = ', $pageString); $json = explode(';</script>', $arr[1]); $userArray = json_decode($json[0], true); $userData = $userArray['entry_data']['ProfilePage'][0]['user']; echo $userData['username'];
Getting all the posts in your account
As it turned out, you can get ready JSON of the last 20 posts by adding to the account URL
/media
:
https://instagram.com/kevin/media
But what to do if we need all the posts? It is enough to add the
max_id
parameter to the URL with the
id
last post from the 20s in the loop, until all the posts run out:
https://instagram.com/kevin/media?max_id=id
. For convenience, there is even a
more_available
field that is
true
or
false
.
Information about a separate post
What if you have a link to an Instagram post (for example,
www.instagram.com/p/9BDXa_L7bm ) and want to get information about it? The same as with the profile page, JSON with the data about the post is embedded there.
The same as in the first paragraph: we take out, parsim and, boom, we have information about the post.
Bonus How to get photos from Instagram in the best quality?
The best quality photos in Instagram at the moment
1080
pixels. But our solution gives only
640
.
At random, we realized that if, for example, we replace the photo in the URL
https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/12950481_1753078061593396_874826488_n.jpg
the part from
640x640
to
1080x1080
:
https://scontent.cdninstagram.com/t51.2885-15/s1080x1080/sh0.08/e35/12950481_1753078061593396_874826488_n.jpg
Then we get a photo in the highest possible quality.
Conclusion
In our case, with the help of the library, we managed to completely cover the API requirements from Instagram.
Repository:
github.com/raiym/instagram-php-scraperAlmost the same thing in Java:
github.com/raiym/instagram-java-scraperProject website:
postaddict.meUpdate: Thank you very much
simpel and
toly for the info. Based on your comments, Instagram requests were written and new ones were added.
Update 2 (December 2, 2016): The development of the library went quite far after the publication of this article (added: getting user information by id, getting comments, pagination by tags, getting photos in the best available quality, searching, etc.), therefore We recommend to watch the repository.
Update 3 (March 16, 2017): A couple of days ago Instagram was updated. Now, for example, authorization is required to get photos by tag. Today added this to the library.