Skip to content

Instantly share code, notes, and snippets.

@phranck phranck/instagram_json.php
Last active Feb 21, 2020

Embed
What would you like to do?
Produces JSON of the last 12 posts of a users Instagram timeline
<?php
//
// Instagram URL that produces a JSON response:
// https://www.instagram.com/YOUR_INSTAGRAM_USERNAME/?__a=1"
//
$instaUrl = "https://www.instagram.com";
$instaUser = "YOUR_INSTAGRAM_USERNAME";
$instaUserTimeline = "$instaUrl/$instaUser/?__a=1";
$instaPostBaseUrl = $instaUrl."/p/";
$json = json_decode(file_get_contents($instaUserTimeline), true);
$jsonUser = $json["graphql"]["user"];
$channel = $jsonUser["edge_owner_to_timeline_media"];
$posts = [];
foreach ($channel["edges"] as $item) {
$node = $item["node"];
$post = [
"url" => $instaPostBaseUrl.$node["shortcode"],
"comment_count" => $node["edge_media_to_comment"]["count"],
"like_count" => $node["edge_liked_by"]["count"],
"caption" => $node["edge_media_to_caption"]["edges"][0]["node"]["text"],
"accessibility_caption" => $node["accessibility_caption"],
"created_timestamp" => $node["taken_at_timestamp"],
"thumbnail_url" => $node["thumbnail_src"],
"media_resources" => [
"150" => [
"url" => $node["thumbnail_resources"][0]["src"],
"width" => $node["thumbnail_resources"][0]["config_width"],
"height" => $node["thumbnail_resources"][0]["config_height"],
],
"240" => [
"url" => $node["thumbnail_resources"][1]["src"],
"width" => $node["thumbnail_resources"][1]["config_width"],
"height" => $node["thumbnail_resources"][1]["config_height"],
],
"320" => [
"url" => $node["thumbnail_resources"][2]["src"],
"width" => $node["thumbnail_resources"][2]["config_width"],
"height" => $node["thumbnail_resources"][2]["config_height"],
],
"640" => [
"url" => $node["thumbnail_resources"][4]["src"],
"width" => $node["thumbnail_resources"][4]["config_width"],
"height" => $node["thumbnail_resources"][4]["config_height"],
],
"1080" => [
"url" => $node["display_url"],
"width" => $node["dimensions"]["width"],
"height" => $node["dimensions"]["height"],
],
]
];
array_push($posts, $post);
}
$response = [
"user" => [
"name" => $jsonUser["username"],
"title" => $jsonUser["full_name"],
"biography" => $jsonUser["biography"],
"url" => $jsonUser["external_url"],
"follower_count" => $jsonUser["edge_followed_by"]["count"],
"following_count" => $jsonUser["edge_follow"]["count"],
"is_private" => $jsonUser["is_private"],
"is_verified" => $jsonUser["is_verified"],
"profile" => [
"standard" => $jsonUser["profile_pic_url"],
"hd" => $jsonUser["profile_pic_url_hd"]
]
],
"channel" => [
"posts_count" => $channel["count"],
"posts" => $posts
]
];
echo(json_encode($response));
// I put the script from instagram_json.php at its own subdomain (YOUR_SUBDOMAIN)
<?php
// use the URL of the feed you want to fetch
$request = Remote::get("https://YOUR_SUBDOMAIN.YOUR_DOMAIN.TLD");
// if the request was sucessfully, parse JSON
$json = "";
if ($request->code() === 200) {
$json = json_decode($request->content(), true);
} else {
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.