Skip to content

Instantly share code, notes, and snippets.

@Ibochkarev
Forked from petyagrill/insta_tag.php
Last active May 21, 2019 06:14
Show Gist options
  • Save Ibochkarev/43c29b68dd2b63831f9c6736b87141c6 to your computer and use it in GitHub Desktop.
Save Ibochkarev/43c29b68dd2b63831f9c6736b87141c6 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