定制 bubalubs/craft-instagram-api 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bubalubs/craft-instagram-api

Composer 安装命令:

composer require bubalubs/craft-instagram-api

包简介

Instagram API Integration for Craft CMS, allowing you to fetch media from Instagram and display it on your website.

README 文档

README

Instagram Integration for Craft CMS, allowing you to fetch media and profile from Instagram and display it on your website via the Instagram API.

Features

  • Craft 5 Support!
  • No dependencies on third-party libraries
  • Authenticate via OAuth 2
  • Fetch Instagram Media and Profile Data
  • Twig, Javascript and PHP API
  • Full caching support with preheating/clearing controls
  • Cron job support for refreshing access tokens
  • Manual token refreshing from the control panel or CLI

Requirements

This plugin requires Craft CMS 5.0.0 or later, and PHP 8.2 or later.

Installation

You can install this plugin from the Plugin Store or with Composer.

From the Plugin Store

Go to the Plugin Store in your project’s Control Panel and search for "Instagram API". Then press "Install".

With Composer

Open your terminal and run the following commands:

# go to the project directory
cd /path/to/my-project.test

# tell Composer to load the plugin
composer require bubalubs/craft-instagram-api

# tell Craft to install the plugin
./craft plugin/install craft-instagram-api

Setup

Step 1

First you need to create an Instagram App. You can do this by visiting the Facebook Developers page. When creating the app select use case Other, then Comsumer. and then you will be able to Set Up the Instagram Basic Display.

Add your instagram account to App Roles -> Roles on your Instagram App settings.

Note: You can find your App ID, App Secret and Valid OAuth Redirect URIs at: Facebook Developers -> App Settings -> Products -> Instagram Basic Display -> Basic Display

Step 2

Add the URI from the from Plugin's Settings to the Valid OAuth Redirect URIs field on your Instagram App settings.

Note: If you are in a development environment, remember to additionally add your live domain to the Valid OAuth Redirect URIs field.

Step 3

Fill in the Plugin Settings your Instagram App ID and Instagram App Secret.

Step 4

After saving your Instagram App ID and Secret, you need to authenticate with Instagram to get an access token. Click the button in the Plugin Settings start the authorization flow with Instagram.

Usage

Javascript API Examples

Get Instagram Media

Endpoint: /actions/instagram-api/api/media

fetch('/actions/instagram-api/api/media')
  .then(response => response.json())
  .then(data => {
      console.log(data);
  });

Get Instagram Profile

Endpoint: /actions/instagram-api/api/profile

fetch('/actions/instagram-api/api/profile')
  .then(response => response.json())
  .then(data => {
      console.log(data);
  });

Twig Examples

Loop through Instagram Media

{% set instagramMedia = craft.instagram.getMedia() %}

{% if instagramMedia|length %}
    <div class="grid grid-cols-4 gap-4">
        {% for media in instagramMedia %}
            <div>
                <a href="{{ media.permalink }}" title="View on Instagram" target="_blank">
                    <img src="{{ media.media_url }}" alt="{{ media.caption ?? 'Instagram image' }}"/>
                </a>
            </div>
        {% endfor %}
    </div>
{% endif %}

Get Profile Data

{% set instagramProfile = craft.instagram.getProfile() %}

{% if instagramProfile %}
  <h2>Instagram Profile</h2>

  <p>
      <strong>ID:</strong> {{ instagramProfile.id }}<br>
      <strong>Username:</strong> {{ instagramProfile.username }}<br>
      <strong>Account Type:</strong> {{ instagramProfile.account_type }}<br>
      <strong>Media Count:</strong> {{ instagramProfile.media_count }}<br>
  </p>
{% endif %}

PHP Examples

Loop through Instagram Media and save locally

use bubalubs\instagramapi\InstagramAPI;

// ...

$instagramMedia = InstagramAPI::getInstance()->instagram->getMedia();

foreach ($instagramMedia as $media) {
    $mediaUrl = $media['media_url'];
    $file = file_get_contents($mediaUrl);

    // Strip query string from filename
    $newFilename = pathinfo(explode('?', $mediaUrl)[0], PATHINFO_BASENAME);
    
    // Check if directory exists
    if (!file_exists(Craft::$app->path->getStoragePath() . '/instagram')) {
        mkdir(Craft::$app->path->getStoragePath() . '/instagram', 0775, true);
    }

    // Save file locally
    file_put_contents(Craft::$app->path->getStoragePath() . '/instagram/' . $newFilename, $file);
}

Get Profile Data

use bubalubs\instagramapi\InstagramAPI;

// ...

$instagramProfile = InstagramAPI::getInstance()->instagram->getProfile();

CLI Commands

Refresh Access Token

./craft instagram-api/refresh-token

bubalubs/craft-instagram-api 适用场景与选型建议

bubalubs/craft-instagram-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32 次下载、GitHub Stars 达 5, 最近一次更新时间为 2024 年 05 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 bubalubs/craft-instagram-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bubalubs/craft-instagram-api 我们能提供哪些服务?
定制开发 / 二次开发

基于 bubalubs/craft-instagram-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 32
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2024-05-07