haridarshan/instagram-php

此包已被废弃,不再维护。未建议替代包。

实现了由http://instagram.com/developer/指定的Instagram API

v2.2.3 2019-04-08 12:11 UTC

This package is auto-updated.

Last update: 2023-03-17 14:09:35 UTC


README

Instagram API的易于使用的PHP库(Beta版)

License Scrutinizer Code Quality Build Status Latest Stable Version Total Downloads Issues Count Code Climate StyleCI

注意:2015年11月17日之前创建的应用将继续工作到2016年6月。2016年6月之后,如果应用未通过审查流程批准,将自动移动到沙盒模式。Instagram API要求每个端点从认证用户获取access_token。我们不再支持仅使用client_id进行请求。

最低要求

PHP 5.5+ and above
Below 5.5.0, this library won't work

##安装

要安装,请使用composer

composer require haridarshan/instagram-php

用法

<?php
require 'vendor/autoload.php';

use Haridarshan\Instagram\Instagram;
use Haridarshan\Instagram\InstagramRequest;
use Haridarshan\Instagram\Exceptions\InstagramOAuthException;
use Haridarshan\Instagram\Exceptions\InstagramResponseException;
use Haridarshan\Instagram\Exceptions\InstagramServerException;

授权码流

<?php 

$instagram = new Instagram(array(
  "ClientId" => <InstagramAppClientId>,
  "ClientSecret" => <InstagramAppClientSecret>,
  "Callback" => <callback_url>,
  "State" => <state_param_value> // Optional parameter: if not defined it will generate a random string
));

$scope = [
  "basic",
  "likes",
  "public_content",
  "follower_list", 
  "comments", 
  "relationships"
];

// To get the Instagram Login Url
$insta_url = $instagram->getLoginUrl(["scope" => $scope]);
echo "<a href='{$insta_url}'>Login with Instagram</a>";

获取访问令牌

<?php
try {
  $oauth = $instagram->oauth($_GET['code']);
  // To get User's Access Token
  $insta_access_token = $oauth->getAccessToken();
  // To get User's Info Token
  $user_info = $oauth->getUserInfo();
} catch (InstagramOAuthException $e) {
  echo $e->getMessage();
}

请求Instagram API

<?php
try {
  // To get User Profile Details or to make any api call to instagram
  $user = new InstagramRequest($instagram, "/users/self", [ "access_token" => $insta_access_token ]);
  // To get Response
  $user_response = $user->getResponse();
} catch(InstagramResponseException $e) {
  echo $e->getMessage();
} catch(InstagramServerException $e) {
  echo $e->getMessage();
}

$media_comment = new InstagramRequest(
  $instagram,
  "/media/{media-id}/comments", 
  [ "access_token" => $insta_access_token, "text" => "{comment}" ], 
  "POST"
);
$media_response = $media_comment->getResponse();

$delete_comment = new InstagramRequest(
  $instagram,
  "/media/{media-id}/comments/{comment-id}", 
  [ "access_token" => $insta_access_token], 
  "DELETE"
);
$delete_response = $delete_comment->getResponse();

常用方法

<?php
  // To get User Profile Details or to make any api call to instagram
  $user = new InstagramRequest($instagram, "/users/self", [ "access_token" => $insta_access_token ]);
  // To get Response
  $user_response = $user->getResponse();
  // To get Body part
  $user_body = $user_response->getBody();
  // To get Data part only
  $user_data = $user_response->getData();
  // To get MetaData part
  $user_meta_data = $user_response->getMetaData();