@
@
@atambalasi2016-04-25 17:02:16
PHP
@atambalasi, 2016-04-25 17:02:16

How to cache request or images from Instagram API?

I make a request by tags

$hashtags = array( "тег1", "тег2", "тег3",  "тег4");
for($i=0; $i<count($hashtags); $i++)
{
                        // make request
                        $ch = curl_init();
                        
                        if($hashtags[$i] == 'тег1'){
                            
                        curl_setopt($ch, CURLOPT_URL, "https://api.instagram.com/v1/tags/{$hashtags[$i]}/media/recent?access_token=MyTOKEN&count=2"); 
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
                        $output = curl_exec($ch);   

                      
                        }elseif($hashtags[$i] == 'тег2'){
                           curl_setopt($ch, CURLOPT_URL, "https://api.instagram.com/v1/tags/{$hashtags[$i]}/media/recent?access_token=MyTOKEN&count=3"); 
                            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
                            $output = curl_exec($ch); 
                        }else{
                            curl_setopt($ch, CURLOPT_URL, "https://api.instagram.com/v1/tags/{$hashtags[$i]}/media/recent?access_token=MyTOKEN&count=1"); 
                            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
                            $output = curl_exec($ch);
                        }
                        
                          // convert response
                        $jdata = json_decode($output);

//Вывод 

    for($j=0; $j<count($jdata->data); $j++){
        if(!empty($jobj->data[$j]->images->standard_resolution->url)){
            echo ' <img src="'. $jdata->data[$j]->images->standard_resolution->url .'"> '
        }
    }
}

There are 10 tags on the working code. It turns out I do 10 requests each time. Exhausted the limit. If I save the image url in my database and update it every 5 hours, will it reduce API requests?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jacksparrow, 2016-04-25
@jacksparrow

An extremely strange question, if you cache the api responses, during the cache time you will not access the instagram. According to your version, you will request those 10 requests every 5 hours.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question