php [instagram php] instagram api #social
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [instagram php] instagram api #social相关的知识,希望对你有一定的参考价值。
<?php
/*
https://github.com/mgp25/Instagram-API/wiki
*/
/**
auth
*/
if(!isset($_GET['code'])) {
$url="https://api.instagram.com/oauth/authorize/?client_id=$client_id&redirect_uri=".urlencode($redirect_uri)."&response_type=code";
header('Location: '.$url);
}
else {
//Request the access_token
$r=curl_post('https://api.instagram.com/oauth/access_token', [], [
'client_id'=> $client_id,
'client_secret'=> $client_secret,
'grant_type'=>'authorization_code',
'redirect_uri'=> $redirect_uri,
'code'=> $_GET['code']
]);
//Array ( [access_token] => 7128395857.1074c04.e7575b2760304ad2baf7fd38bd2d44b1 [user] => Array ( [id] => 7128395857 [username] => quach6049 [profile_picture] => https://scontent.cdninstagram.com/vp/01042c7bb620293cfa067444b6c3808d/5CB95E51/t51.2885-19/s150x150/42939233_256824515176103_4858955205577801728_n.jpg?_nc_ht=scontent.cdninstagram.com [full_name] => hoang quach [bio] => [website] => [is_business] => ) )
print_r($r['access_token']);
}
//login
$ig = new \InstagramAPI\Instagram(true, true, [
'storage' => 'mysql',
'dbhost' => 'localhost',
'dbname' => 'mydatabase',
'dbusername' => 'root',
'dbpassword' => '',
]);
$ig->login('hoangweb', 'T1vkF5lS5bsfdgTjyIRe8EiZ');
/**
people
*/
//get user id by username
$userId = $ig->people->getUserIdForName('hoangweb');
$response = $ig->people->getInfoById($userId);
echo $response->getUser()->getUsername();
/**
post
*/
/*
uploading photo
*/
$metadata = [
'caption' => 'My awesome caption',
'location' => $location, // $location must be an instance of /Model/Location class
];
// if you want only a caption, you can simply do this:
$metadata = ['caption' => 'My awesome caption'];
$r = $ig->timeline->uploadPhoto($photoFilename, $metadata);
#or
$photo = new \InstagramAPI\Media\Photo\InstagramPhoto($photoFilename);
$ig->timeline->uploadPhoto($photo->getFile(), ['caption' => $captionText]);
$item=$r->getMedia();
var_dump( $item->getId());
echo $item->getItemUrl();
$url = $item->getImageVersions2()->getCandidates()[0]->getUrl();
/*
Uploading an album
https://github.com/mgp25/Instagram-API/blob/master/examples/uploadAlbum.php
*/
$media = [ // Albums can contain between 2 and 10 photos/videos.
[
'type' => 'photo',
'file' => '', // Path to the photo file.
],
[
'type' => 'photo',
'file' => '', // Path to the photo file.
/* TAGS COMMENTED OUT UNTIL YOU READ THE BELOW:
'usertags' => [ // Optional, lets you tag one or more users in a PHOTO.
[
'position' => [0.5, 0.5],
// WARNING: THE USER ID MUST BE VALID. INSTAGRAM WILL VERIFY IT
// AND IF IT'S WRONG THEY WILL SAY "media configure error".
'user_id' => '123456789', // Must be a numerical UserPK ID.
],
],
*/
],
[
'type' => 'video',
'file' => '', // Path to the video file.
],
];
$captionText = '';
$ig->timeline->uploadAlbum($media, ['caption' => $captionText]);
/**
delete media
https://github.com/mgp25/Instagram-API/blob/fa6057c16d7ab646abf2a943254edcce4ef3c04d/examples/exceptionDetailsExample.php
*/
try {
$ig->media->delete('123456'); // Invalid media ID, to trigger a server error.
} catch (\InstagramAPI\Exception\InstagramException $e) {
echo 'Something went wrong (InstagramException): '.$e->getMessage()."\n";
if ($e->hasResponse()) { // <-- VERY IMPORTANT TO CHECK FIRST!
echo "The current exception contains a full server response:\n";
$e->getResponse()->printJson();
echo "Showing the 'did_delete' and 'message' values of the response:\n";
var_dump($e->getResponse()->getDidDelete());
var_dump($e->getResponse()->getMessage());
}
}
//enable comment: disable: disableComments()
$ig->media->enableComments($mediaId);
/**
comment
*/
//add comment
$ig->media->comment($mediaId, 'this is comment');
//get comments for media
$options=[];
$r = $ig->media->getComments($mediaId, $options);
//delete comment
$ig->media->deleteComment($mediaId, $commentId);
以上是关于php [instagram php] instagram api #social的主要内容,如果未能解决你的问题,请参考以下文章
带有 Laravel 5.4 的 Instagram API