Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for subscriptions to the real-time API #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions example/callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* We need a token to verify the subscriptions as explained in the docs.
* http://instagram.com/developer/realtime/
* [Quote] In order to keep someone else from creating unwanted subscriptions, we must verify that the endpoint would like this new subscription
* This $verifytoken must match the $_verifytoken in the Instagram class set by the setVerifyToken method.
*/

$verifytoken = '123451234';


if(isset($_GET['hub_mode']) && $_GET['hub_mode'] == 'subscribe'){
if($_GET['hub_verify_token']==$verifytoken)
die($_GET['hub_challenge']);
}
echo 'Invalid valid token';
90 changes: 90 additions & 0 deletions example/router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/**
* This router file is to facilitate tests with the subscription portion of the Instagram API Class
* You can test it like so :
* http://localhost/instagram/router.php
* http://localhost/instagram/router.php?fct=addUsers
* http://localhost/instagram/router.php?fct=addTag&tag=nofilter
* http://localhost/instagram/router.php?fct=addGeo
* http://localhost/instagram/router.php?fct=addLocation
* http://localhost/instagram/router.php?fct=del&object=tag
* http://localhost/instagram/router.php?fct=del&all=1
*
*
*/


require_once 'instagram.class.php';

$config = array('apiKey'=>'CLIENT_API_KEY',
'apiSecret'=>'CLIENT_API_SECRET',
'apiCallback'=>'http://localhost/link/to/callback.php'
);


$instagram = new Instagram($config);


/*
* We need a token that will also be used in the callback script to verify the subscriptions as explained in the docs.
* http://instagram.com/developer/realtime/
* Quote : In order to keep someone else from creating unwanted subscriptions, we must verify that the endpoint would like this new subscription
* See callback.php script.
*/
$verifytoken = '123451234';
$instagram->setVerifyToken($verifytoken);


if(isset($_GET['fct'])){

/*
* Will create a subscription for every authorized user through app.
*/
if($_GET['fct']=='addUsers')
echo json_encode( $instagram->createSubscription('user'));

/*
* Will create a subscription for a certain tag.
*/
elseif($_GET['fct']=='addTag')
echo json_encode( $instagram->createSubscription('tag',$_GET['tag']));

/*
* Will create a subscription for a certain instagram location id.
*/
elseif($_GET['fct']=='addLocation')
echo json_encode( $instagram->createSubscription('location','1257285'));

/*
* Will create a subscription for a certain geographic position using latitude, longitude and a given radius.
*/
elseif($_GET['fct']=='addGeo')
echo json_encode( $instagram->createSubscription('geography','',array('lat'=>35.657872,'lng'=>139.70232,'radius'=>'1000')));

/*
* Will delete a certain subscription identified by its unique identifer
*/
elseif($_GET['fct']=='del' && isset($_GET['id']))
echo json_encode( $instagram->deleteSubscriptionById($_GET['id']));

/*
* Will delete all subscriptions by object type (tag, user, location, geography).
*/
elseif($_GET['fct']=='del' && isset($_GET['object']))
echo json_encode( $instagram->deleteSubscriptions($_GET['object']));


/*
* Will delete all subscriptions.
*/
elseif($_GET['fct']=='del' && isset($_GET['all']))
echo json_encode( $instagram->deleteSubscriptions('all'));
}

/*
* Always list active subsriptions.
*/
echo json_encode( $instagram->listSubscriptions());
?>

89 changes: 85 additions & 4 deletions instagram.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Instagram {
*/
const API_OAUTH_TOKEN_URL = 'https://api.instagram.com/oauth/access_token';

/**
/**
* The Instagram API Key
*
* @var string
Expand All @@ -55,6 +55,13 @@ class Instagram {
* @var string
*/
private $_accesstoken;

/**
* Verify token for subscriptions
*
* @var string
*/
private $_verifytoken;

/**
* Available scopes
Expand All @@ -69,7 +76,7 @@ class Instagram {
* @var array
*/
private $_actions = array('follow', 'unfollow', 'block', 'unblock', 'approve', 'deny');

/**
* Default constructor
*
Expand Down Expand Up @@ -361,7 +368,62 @@ public function getLocationMedia($id) {
public function searchLocation($lat, $lng, $distance = 1000) {
return $this->_makeCall('locations/search', false, array('lat' => $lat, 'lng' => $lng, 'distance' => $distance));
}


/**
* List all your subscriptions
*
* @return mixed
*/
public function listSubscriptions(){
return $this->_makeCall('subscriptions',false,array('client_secret'=>$this->getApiSecret()));
}

/**
* Create a subscription
*
* @param string $object Object type (user, tag, geography, location)
* @param string [optional]$object_id Name of the object you want to subscribe : can be a location, a tag,
* @param array [optional] $additionalParams Distance in meter (max. distance: 5km = 5000)
* @param string [optional] $aspect Aspect of the object, only media is supported for this time.
* @return mixed
*/
public function createSubscription($object, $object_id='', $additionalParams=array(), $aspect='media'){
return $this->_makeCall('subscriptions',false,
array_merge(array('object'=>$object,
'aspect'=>$aspect,
'object_id'=>$object_id,
'verify_token'=>$this->getVerifyToken(),
'client_secret' =>$this->getApiSecret(),
'callback_url'=>$this->getApiCallback()
),$additionalParams)
,'POST');
}
/**
* Delete a subscription
*
* @param string $id Id of the subscription you want to delete
* @return mixed
*/
public function deleteSubscriptionById($id){
return $this->_makeCall('subscriptions',false,array('id'=>$id,
'verify_token'=>$this->getVerifyToken(),
'client_secret' =>$this->getApiSecret()
),'DELETE');
}

/**
* Delete subscriptions
*
* @param string $object Object type that you want to delete (Ex.: trag, user, geography, location)
* @return mixed
*/
public function deleteSubscriptions($object){
return $this->_makeCall('subscriptions',false,array('object'=>$object,
'verify_token'=>$this->getVerifyToken(),
'client_secret' =>$this->getApiSecret()
),'DELETE');
}

/**
* Pagination feature
*
Expand Down Expand Up @@ -488,7 +550,7 @@ private function _makeOAuthCall($apiData) {

return json_decode($jsonData);
}

/**
* Access Token Setter
*
Expand Down Expand Up @@ -565,5 +627,24 @@ public function setApiCallback($apiCallback) {
public function getApiCallback() {
return $this->_callbackurl;
}

/**
* Verify Token Setter
*
* @param string $data
* @return void
*/
public function setVerifyToken($token) {
$this->_verifytoken = $token;
}

/**
* Access Token Getter
*
* @return string
*/
public function getVerifyToken() {
return $this->_verifytoken;
}

}