Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One thing I noticed is the API is mega slow, good choice to cache.</p> <p>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.</p> <p>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.</p> <p><code>$item-&gt;data-&gt;location-&gt;latitude</code> and <code>$item-&gt;data-&gt;location-&gt;longtitude</code> is always null in the result so added a check for that...</p> <p>Hope it helps</p> <pre><code>&lt;?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) &amp;&amp; filemtime($cache) &gt; 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)-&gt;data as $item){ $title = (isset($item-&gt;caption))?mb_substr($item-&gt;caption-&gt;text,0,70,"utf8"):null; $src = $item-&gt;images-&gt;standard_resolution-&gt;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-&gt;data-&gt;location-&gt;latitude))?$item-&gt;data-&gt;location-&gt;latitude:null; // Caches latitude as $lat $lon = (isset($item-&gt;data-&gt;location-&gt;longtitude))?$item-&gt;data-&gt;location-&gt;longtitude:null; // Caches longitude as $lon $images[] = array( "title" =&gt; htmlspecialchars($title), "src" =&gt; htmlspecialchars($src), "lat" =&gt; htmlspecialchars($lat), "lon" =&gt; 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); } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload