2012-04-14 132 views
2

我覺得我正在過度揣測這個。我想要做的是從instagram api中拉取最近的照片,並將生成的json信息保存爲緩存文件。然後,我將使用jQuery從該文件中讀取 - 我已經弄清楚了這一部分。我現在使用的是將其保存在緩存文件中,但不是以我認識的格式保存。我覺得我太過於複雜了。使用PHP通過Instagram存儲json API

這是代碼,我一直在努力與基於教程中,我發現:

// 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)); 
} 
+0

如果你想將文件保存爲JSON,用'json_encode'代替'serialize'。 – 2012-04-14 20:46:46

+0

@MichaelMior謝謝。 – 2012-04-14 22:09:32

回答

3

一件事我注意到的是API是超級慢,好選擇緩存。

你試圖保存爲一個序列化的數組(這並不是什麼大問題),但是你可以將它另存爲json,如果你要再次讀取它作爲json,它將再次反序列化它將節省1個步驟。

以下是我所做的一些更改: 新增了curl以嘗試加快響應速度,或者如果您沒有安裝,請回退到FGC。 響應被保存爲json,當從緩存中檢索時,它被解碼爲一個數組而不是一個對象,這意味着你可以保持相同的數組結構。

$item->data->location->latitude$item->data->location->longtitude總是在結果空,因此增加了一個檢查...

希望它可以幫助

<?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

太棒了!現在玩這個,非常感謝。 – 2012-04-14 21:56:22