Instagram APIはAPI呼び出しごとに35枚の画像を制限しているため、選択したInstagramの写真をハッシュタグでアルバムに表示するプロジェクトに取り組んでいます.AJAX(私は非常に苦手です)を使用するか、PHPとAJAX。ギャラリーのコードでアクセス トークンとユーザー ID を使用できるようにしたくないため、後者を選択しました。
<?PHP
function jsongram($next=null){
$userid = "xxx";
$accessToken = "xxx";
$url = ("https://api.instagram.com/v1/users/{$userid}/media/recent/?access_token={$accessToken}");
if($url !== null) {
$url .= '&max_id=' . $next;
}
//Also Perhaps you should cache the results as the instagram API is slow
$cache = './'.sha1($url).'.json';
//unlink($cache); // Clear the cache file if needed
if(file_exists($cache) && filemtime($cache) > time() - 60*60){
// If a cache file exists, and it is newer than 1 hour, use it
$jsonData = json_decode(file_get_contents($cache));
}else{
$jsonData = json_decode((file_get_contents($url)));
file_put_contents($cache,json_encode($jsonData));
}
?>
<html>
<head>
</head>
<body>
<?php
$data_array = array();
foreach ($jsonData->data as $data){
if (stripos($data->caption->text,'egypt') === false) {
}
else{
$data_array[] = $data;
$data = (str_split($data->caption->text));
$data = (array_filter($data));
}
}
foreach ($data_array as $data):{
$igimglow = $data->images->low_resolution->url;
$igimgstd = $data->images->standard_resolution->url;
$igimgthumb = $data->images->thumbnail->url;
$igcaption = str_replace('#', '', (preg_replace('/(?:#[\w-]+\s*)+$/', '', $data->caption->text)));
$igtime = date("F j, Y", $data->caption->created_time);
$iglikes = $data->likes->count;
$igcomments = $data->comments->count;
$iglong = $data->location->longitude;
$iglat = $data->location->latitude ;
$igusername = $data->user->username;
$igfullname = $data->user->full_name;
$iglink = $data->link;
$igfilter = $data->filter;
$igtags = implode(',',$data->tags);
?>
<img src="<?php echo ($igimglow);}?>">
<?php endforeach ?>
<?php
if(isset($jsonData->pagination->next_max_id)) {
$result .= '<div><a href="?next=' . $jsonData->pagination->next_max_id . '">Next</a></div>';
}
return $result;
}
?>
<div id="container">
<?=jsongram(@$_GET['next']);?>
<div id="result"></div>
</div>
</body>
</html>
上記のコードの実際の例を次に示します。
http://johnricedesign.com/examples/pn.php
上図の2ページ目には「エジプト」のタグがつけられた写真が表示されています。同じページに自動的に読み込まれる「次へ」リンクを「さらに読み込む」ボタンに置き換えたいと思います-私の知る限り、AJAXを使用することが唯一の方法です。ただし、その方法も、どこから始めればよいかもわかりません。私が抱えている2番目の明らかな問題は、「エジプト」のキャプションを含まない写真を削除しているにもかかわらず、まだ多くの空白が残っていることです.AJAXが使用されると、修正はかなり簡単になるでしょう.
私はこれをしようとして、過去5日間髪を引っ張っています. あなたの助け、アドバイス、知恵、事前に大歓迎です。