Skip to content

Instantly share code, notes, and snippets.

@stefanRepac
Last active May 7, 2019 09:32
Show Gist options
  • Select an option

  • Save stefanRepac/2971e059e2585b7bca14ac450a4c5d69 to your computer and use it in GitHub Desktop.

Select an option

Save stefanRepac/2971e059e2585b7bca14ac450a4c5d69 to your computer and use it in GitHub Desktop.
Get Instagram API Posts with Token in PHP
<?php
// Using curl because if you use file_get_contents() it need more then 10s page to be loaded...
function curl_get_contents($url) {
// Initiate the curl session
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
// Removes the headers from the output
curl_setopt($ch, CURLOPT_HEADER, 0);
// Return the output instead of displaying it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute the curl session
$output = curl_exec($ch);
// Close the curl session
curl_close($ch);
// Return the output as a variable
return $output;
}
$page_id = 'YOUR_PROFILE_ID'; // Find your ID from OEmbed post under author_id https://api.instagram.com/oembed/?callback=&url=YOUR_INSTA_POST_URL
$access_token = 'YOUR_ACCESS_TOKEN'; // Find your token here https://instagram.pixelunion.net/
// Get JSON
// &count=1 is for latest post, remove it if you want to get full list of posts in JSON
$json_object = curl_get_contents('https://api.instagram.com/v1/users/' . $page_id .
'/media/recent/?access_token=' . $access_token . '&count=1');
$indata = json_decode($json_object);
// Convert stdClass Objects to Multidimensional Arrays function https://gist.github.com/Soullighter/3362c72012e9cd9e22bd54bd2402b0cc
$array = objectToArray($indata);
// Get caption from latest Instagram post
echo $array[data][0][caption]['text'];
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment