簡體   English   中英

使用PHP通過Instagram API存儲json

[英]Using PHP to store json via Instagram API

我覺得我想得太多了。 我想做的是從instagram API中提取最新照片,並將得到的json信息保存為緩存文件。 然后,我將使用jQuery讀取該文件-我已經弄清楚了那部分。 我現在使用的是將其保存在緩存文件中,但不是我可以識別的格式。 我想這太復雜了。

這是我根據發現的一個教程使用的代碼:

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

我注意到的一件事是API非常慢,是緩存的不錯選擇。

您正在嘗試另存為序列化數組(這沒什么大不了的),但是如果您要再次將其讀取為json,則也可以將其另存為json,這樣可以節省再次反序列化的1個步驟。

這是我所做的一些更改:添加了curl以嘗試加快響應速度,或者如果您未安裝FGC則退回到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);
    }

}

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM