Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7817969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:24:48+00:00 2026-06-02T06:24:48+00:00

I feel like I’m overthinking this. What I want to do is pull the

  • 0

I feel like I’m overthinking this. What I want to do is pull the most recent photos from the instagram api and save the resulting json information as a cache file. I’ll then use jQuery to read from that file — I’ve got that part figured out. What I’m using now is saving it in a cache file, but not in a format that I recognize. I think I’m overcomplicating this.

This is code I’ve been working with based on a tutorial I found:

// Client ID for Instagram API
$instagramClientID = '9110e8c268384cb79901a96e3a16f588';

$api = 'https://api.instagram.com/v1/media/popular?client_id='.$instagramClientID; //api       request (edit this to reflect tags)
$cache = 'cache.txt';

if(file_exists($cache) && filemtime($cache) > time() - 60*60){
// If a cache file exists, and it is newer than 1 hour, use it
$images = unserialize(file_get_contents($cache));
}
else{
// Make an API request and create the cache file

// For example, gets the 32 most popular images on Instagram

$response = file_get_contents($api); //change request path to pull different photos

$images = array();

// Decode the response and build an array
foreach(json_decode($response)->data as $item){ // Decodes json (javascript) into an array

    $title = '';

    if($item->caption){
        $title = mb_substr($item->caption->text,0,70,"utf8");
    }

    $src = $item->images->standard_resolution->url; //Caches standard res img path to variable $src

    $lat = $item->data->location->latitude; // Caches latitude as $lat
    $lon = $item->data->location->longtitude; // Caches longitude as $lon       

    $images[] = array(
        "title" => htmlspecialchars($title),
        "src" => htmlspecialchars($src),
        "lat" => htmlspecialchars($lat),
        "lon" => htmlspecialchars($lon) // Consolidates variables to an array
    );
}

// Remove the last item, so we still have
// 32 items when when the cover is added
//array_pop($images);

// Push the cover in the beginning of the array
//array_unshift($images,array("title"=>"Cover", "src"=>"assets/img/cover.jpg"));

// Update the cache file
file_put_contents($cache,serialize($images));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:24:49+00:00Added an answer on June 2, 2026 at 6:24 am

    One thing I noticed is the API is mega slow, good choice to cache.

    You are attempting to save as a serialized array (which is no biggie) but you may as well save it as json if your going to read it as json again, it saves 1 step in unserializing it again.

    Here are some changes I made:
    Added curl to try and speed up the response or fall back to FGC if u dont have it installed.
    The response gets saved as json, and when retrieved from the cache its decoded as an array instead of an object, this means you can keep your same array structure.

    $item->data->location->latitude and $item->data->location->longtitude is always null in the result so added a check for that…

    Hope it helps

    <?php
    // Client ID for Instagram API
    $instagramClientID = '9110e8c268384cb79901a96e3a16f588';
    
    $api = 'https://api.instagram.com/v1/media/popular?client_id='.$instagramClientID; //api request (edit this to reflect tags)
    $cache = './cache.json';
    
    if(file_exists($cache) && filemtime($cache) > time() - 60*60){
        // If a cache file exists, and it is newer than 1 hour, use it
        $images = json_decode(file_get_contents($cache),true); //Decode as an json array
    }
    else{
        // Make an API request and create the cache file
        // For example, gets the 32 most popular images on Instagram
        $response = get_curl($api); //change request path to pull different photos
    
        $images = array();
    
        if($response){
            // Decode the response and build an array
            foreach(json_decode($response)->data as $item){
    
                $title = (isset($item->caption))?mb_substr($item->caption->text,0,70,"utf8"):null;
    
                $src = $item->images->standard_resolution->url; //Caches standard res img path to variable $src
    
                //Location coords seemed empty in the results but you would need to check them as mostly be undefined
                $lat = (isset($item->data->location->latitude))?$item->data->location->latitude:null; // Caches latitude as $lat
                $lon = (isset($item->data->location->longtitude))?$item->data->location->longtitude:null; // Caches longitude as $lon
    
                $images[] = array(
                "title" => htmlspecialchars($title),
                "src" => htmlspecialchars($src),
                "lat" => htmlspecialchars($lat),
                "lon" => htmlspecialchars($lon) // Consolidates variables to an array
                );
            }
            file_put_contents($cache,json_encode($images)); //Save as json
        }
    }
    
    //Debug out
    print_r($images);
    
    //Added curl for faster response
    function get_curl($url){
        if(function_exists('curl_init')){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
            $output = curl_exec($ch);
            echo curl_error($ch);
            curl_close($ch);
            return $output;
        }else{
            return file_get_contents($url);
        }
    
    }
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like this is something I should already know, but I'm just not
I feel like this is easy but I am missing something... Using jQuery, I
I feel like this should be a no brainer, but clearly I'm missing something...
I feel like this is a stupid question, but I can't think of a
I feel like I am missing something - from what it seems, JSP comes
I feel like there's a simple solution to this, but I'm not really sure
I feel like this may be a dumb question, but it's late and my
I feel like there is a better way to do this. I'm mostly just
I feel like the answer to this question is really simple, but I really
I feel like I should know this but I've been stumped for hours now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.