J
J
Justique2016-01-09 01:14:39
PHP
Justique, 2016-01-09 01:14:39

Strange Instagram API?

There was a need to work with the Instagram API, the first thing I did was to look for classes for work and periodically I come across such links that are accessed via the API
//i.instagram.com/api/v1/
and in the documentation Moreover, the authorization in the first example is completely different
//api.instagram.com/v1/
from that described in the doc.
docs.

private function Auth() {
        $this->inLog("Auth");
        $login = $this->makeApiCall('accounts/login/', $this->user, true, false);
    print_r($login);
        if(isset($login['status']) && $login['status'] == 'ok') {
            $this->inLog("Login success..\nLogged in as ".$login['logged_in_user']['username']); //todo msgs in array
            $this->is_logged = true;
            $this->user = $login['logged_in_user'];
        } else die($this->inLog('Wrong username/password'));
    }

Whole class
set_time_limit(0);

class Instagram {
    
    public $user;
    private $signatureKey;
    public $is_logged = false;
    public static $apiURL    = '//i.instagram.com/api/v1/';
    public static $userAgent = 'Instagram 6.9.1 Android (15/4.0.4; 160dpi; 320x480; Sony; MiniPro; mango; semc; en_Us)';
    public static $key_ver   = 4;
    
    
    public function __construct($login, $password, $key) {
        $this->user = array('username'=>$login, 'password'=>$password);
        $this->signatureKey = $key;
        $this->GUID = $this->GenerateGuid();
        $this->curl = new cUrl($this->user['username'], self::$userAgent);
        $this->Auth();
    
        return true;
    }
    
    private function Auth() {
        $this->inLog("Auth");
        $login = $this->makeApiCall('accounts/login/', $this->user, true, false);
    print_r($login);
        if(isset($login['status']) && $login['status'] == 'ok') {
            $this->inLog("Login success..\nLogged in as ".$login['logged_in_user']['username']); //todo msgs in array
            $this->is_logged = true;
            $this->user = $login['logged_in_user'];
        } else die($this->inLog('Wrong username/password'));
    }

    public function getUsers($from, $data, $count) {
        $this->inLog("getUsers");
        $fromlist = array('followers', 'following', 'likes');
        $list = array();
        if(!isset($from) || !in_array($from, $fromlist)) die("from get users?");

        if($from == 'following' || $from == 'followers') {
            $users = $this->getUsersByRelationships($from, $data['id'], $count);
        } elseif ($from == 'likes') {
            $users = $this->getUsersFromLikes($data['media'], $count);
        }


        return new UserActions($this, array_slice($users, 0, $count));
    }

    public function getNotMutualRelationships() {

        $followers = $this->getUsersByRelationships('followers', $this->user['pk'], 9999999); 
        $following = $this->getUsersByRelationships('following', $this->user['pk'], 9999999); 


        $list = $following;

        foreach ($following as $key => $user) {
            foreach ($followers as $follower) {
                if($user['pk'] === $follower['pk']) {
                    unset($list[$key]);
                    break;
                }
            }
        }

        $this->inLog("Not mutual relationships: ".count($list));
        return new UserActions($this, $list);

    }

    public function getUsersFromLikes($media, $count) {
        $this->inLog("getUsersFromLikes");
        $list = array();
        $req = $this->makeApiCall('media/'.$media.'/likers/');
        return $req;
    }

    public function getUsersByRelationships($from, $id, $count) {
        $this->inLog("getUsersByRelationships ".$from);
        $fromlist = array("followers", "following");
        if(!isset($from) || !in_array($from, $fromlist)) die("from get users?");

        $list = array();

        while(count($list) < $count) {
            $req = $this->makeApiCall('friendships/'.$id.'/'.$from.'/?max_id='.$max_id);
            $list = array_merge($list, $req['users']);
            if(isset($req['next_max_id'])) {
                $max_id = $req['next_max_id'];
            } else break;
        }
        return array_slice($list, 0, $count);
    }


    public function searchUserByUsername($username) {
        $this->inLog("searchUserByUsername ".$username);
        $req = $this->makeApiCall('users/search/?query='.$username);
        return $req;
    }

    public function getMedia($id, $count) {
        /*$this->inLog("getMedia ".$id);
        
        $list = array();

        while(count($list) < $count) {
            $req = $this->makeApiCall('feed/user/'.$id.'/?max_id='.$max_id);
            
            if(end($req['items'])['id'] != $max_id) {
                $max_id = end($req['items'])['id'];
            } else break;
            
            $list = array_merge($list, $req['items']); 

        }
        return array_slice($list, 0, $count);*/
    }

    public function Follow($uid, $destroy = false) {
       /* $request = $this->makeApiCall('friendships/'.($destroy ? 'destroy/' :'create/').$uid.'/', ['user_id'=>$uid]);
        return $request;*/
    }


    public function inLog($str) {
    if(is_array($str)){
        echo "#######Start ARRAY#######\n";
      foreach($str as $key =>$values){
        echo @date("[H:i:s]: ").'['.$key.']'.$values.PHP_EOL . "\n";
      }
        echo "########End ARRAY########\n";
    }else{
        echo @date("[H:i:s]: ").$str.PHP_EOL;
    }
    }

    public function makeApiCall($method, $params = array(), $ssl = false, $use_cookie = true, $server = true) {
        $defaultRequestBody = array(
            "device_id"=>'android-'.$this->GUID,
            "guid"=>$this->GUID,
            "Content-Type"=>"application/x-www-form-urlencoded; charset=UTF-8"
        );
    $signedBody = '';
        if(!empty($params)) {
            $params = json_encode(array_merge($defaultRequestBody, $params));
            $signedBody = 'signed_body='.$this->generateSig($params).".".urlencode($params).'&ig_sig_key_version='.self::$key_ver;
        }
    
  $server = self::$apiURL;
        $result = json_decode($this->curl->call(($ssl ? 'https:' : 'http:').$server.$method, $signedBody, $use_cookie), true);
        return $result;
    }
    
    private function generateSig($str) {
        return hash_hmac('sha256', $str, $this->signatureKey);
    }

    private function GenerateGuid() {
        return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', 
            mt_rand(0, 65535), 
            mt_rand(0, 65535), 
            mt_rand(0, 65535), 
            mt_rand(16384, 20479), 
            mt_rand(32768, 49151), 
            mt_rand(0, 65535), 
            mt_rand(0, 65535), 
            mt_rand(0, 65535)
        );
    }

    
}

Actually, the methods described in the documentation do not work with this class (

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Oleg Karnaukhov, 2016-01-09
@Justique

This is not an instagram API, it's an unofficial open API through which instagram applications work.

A
Anton Seredny, 2016-01-16
@smidl

this class does not work with applications that were created after November 17, 2015.
All new applications must now use an access_token with the correct scope. Previously, client_id was enough.
Moreover, new applications work in sandbox mode, which greatly reduces the limits and capabilities of the application. And in order to become fully functional, the application needs to go through a dreary approve on Instagram.

A
Alexey Timofeev, 2016-01-09
@phtimofeeff

So what do you want from Instagram?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question