Skip to content

Instantly share code, notes, and snippets.

@petyagrill
Last active April 29, 2019 10:34
Show Gist options
  • Save petyagrill/6b8f40a291f1a87ad64ccc9317df4281 to your computer and use it in GitHub Desktop.
Save petyagrill/6b8f40a291f1a87ad64ccc9317df4281 to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: pg
* Date: 30.01.19
* Time: 18:10
*/
function getPublicInfo($tag) {
$url = sprintf("https://www.instagram.com/explore/tags/$tag");
$content = file_get_contents($url);
$content = explode("window._sharedData = ", $content)[1];
$content = explode(";</script>", $content)[0];
$data = json_decode($content, true);
return $data['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['edges'];
}
$images = [];
$cacheFile = MODX_CORE_PATH.'/cache/insta.json';
$cacheData = json_decode(file_get_contents($cacheFile),1);
if(!isset($cacheTime)){
$cacheTime = 1800;
}
if(!isset($limit)) {
$limit = 12;
}
if($cacheData['time']+$cacheTime < time() ) {
$profile = getPublicInfo($hashtag);
foreach ($profile as $item){
foreach ($item['node']['thumbnail_resources'] as $thumb){
if ($thumb['config_width'] == $thumbSize){
$images[] = [
'image'=>$item['node']['display_url'],
'thumb'=>$thumb['src'],
'shortcode'=>$item['node']['shortcode'],
];
}
}
}
$cacheData['time'] = time();
$cacheData['images'] = $images;
file_put_contents($cacheFile, json_encode($cacheData,256));
} else {
$images = $cacheData['images'];
}
if($randomize){
shuffle($images);
}
return array_slice($images, 0, $limit);
/*
{set $instaList = '@FILE snippets/instagram.php' | snippet : [
'hashtag'=>'aviatorwatch',
'cacheTime'=>'1800',
'thumbSize'=>'240',
'limit'=>'9',
'randomize'=>'1',
]}
{if count($instaList) > 0}
<section class="section-ig bg-light">
<div class="ig-logo"></div>
<div class="ig-hashtag">#AVIATORWATCH</div>
<div class="ig-layout">
{foreach $instaList as $item}
<a href="https://www.instagram.com/p/{$item.shortcode}" target="_blank">
<img src="{$item.thumb}">
</a>
{/foreach}
</div>
</section>
{/if}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment