├── .gitignore ├── src ├── InstagramFacade.php ├── InstagramConfig.php ├── InstagramServiceProvider.php └── Instagram.php ├── composer.json └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | /src/InstagramConfig.php 2 | -------------------------------------------------------------------------------- /src/InstagramFacade.php: -------------------------------------------------------------------------------- 1 | 'user-id-kamu', 9 | 'accessToken' => 'access-token-kamu', 10 | 11 | 'clientId' => 'client-id-kamu', 12 | 'clientSecret' => 'client-secrets-kamu', 13 | 'redirectUri' => 'redirect-uri-kamu', 14 | ]; 15 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "muhamadrezaar/instagram", 3 | "description": "Laravel 5 Instagram Package", 4 | "license": "MIT", 5 | "keywords" : ["laravel" , "instagram"], 6 | "authors": [ 7 | { 8 | "name": "Muhamad Reza Abdul Rohim", 9 | "email": "reza.wikrama3@gmail.com" 10 | } 11 | ], 12 | "minimum-stability": "dev", 13 | "require": {}, 14 | "autoload": { 15 | "psr-4": { 16 | "Oblagio\\Instagram\\": "src/" 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/InstagramServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([__DIR__.'/InstagramConfig.php' => config_path('InstagramConfig.php')] , 'config'); 13 | } 14 | 15 | public function register() 16 | 17 | { 18 | 19 | // merge config 20 | $configFile = config_path('InstagramConfig.php'); 21 | 22 | if(file_exists($configFile)) 23 | { 24 | $this->mergeConfigFrom($configFile , 'config'); 25 | }else{ 26 | $this->mergeConfigFrom(__DIR__.'/InstagramConfig.php', 'config'); 27 | } 28 | 29 | 30 | // 31 | 32 | $this->app->bind('register-instagram' , function(){ 33 | 34 | return new Instagram; 35 | 36 | }); 37 | 38 | } 39 | } -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Laravel 5 Instagram Packages 2 | 3 | [![Total Downloads](https://poser.pugx.org/muhamadrezaar/instagram/d/total.svg)](https://packagist.org/packages/muhamadrezaar/instagram) 4 | [![Latest Stable Version](https://poser.pugx.org/muhamadrezaar/instagram/v/stable.svg)](https://packagist.org/packages/muhamadrezaar/instagram/v/stable.svg) 5 | [![License](https://poser.pugx.org/muhamadrezaar/instagram/license.svg)](https://packagist.org/packages/muhamadrezaar/instagram) 6 | 7 | 8 | Package Instagram untuk Laravel 5 9 | 10 | ### Installasi 11 | 12 | Tambahkan Package pada composer.json 13 | ```sh 14 | composer require muhamadrezaar/instagram 15 | ``` 16 | setelah package terdownload , register provider dan facade nya 17 | 18 | Provider : 19 | ```sh 20 | Oblagio\Instagram\InstagramServiceProvider::class, 21 | ``` 22 | Facade : 23 | ```sh 24 | 'IG' => Oblagio\Instagram\InstagramFacade::class, 25 | ``` 26 | 27 | Publish config 28 | ```sh 29 | php artisan vendor:publish 30 | ``` 31 | 32 | ### Konfigurasi 33 | 34 | Buka file config/InstagramConfig.php 35 | masukan user id dan access token instagram anda 36 | contoh : 37 | ```sh 38 | 'user-id-kamu', 46 | 'accessToken' => 'access-token-kamu', 47 | 48 | 'clientId' => 'client-id-kamu', 49 | 'clientSecret' => 'client-secrets-kamu', 50 | 'redirectUri' => 'redirect-uri-kamu', 51 | ]; 52 | 53 | ``` 54 | 55 | ### Cara penggunaaan 56 | 57 | Menampilkan Gambar low resolusi 58 | 59 | ```sh 60 | 61 | "; 66 | } 67 | 68 | ?> 69 | ``` 70 | Contoh Output Menampilkan gambar low resolution di browser 71 | 72 | ![alt tag](https://s17.postimg.org/4velafbjz/instagram.png) 73 | 74 | 75 | 76 | 77 | Menampilkan Gambar standar resolusi 78 | 79 | ```sh 80 | 81 | "; 87 | } 88 | 89 | ?> 90 | 91 | ``` 92 | 93 | Menampilkan Informasi User 94 | 95 | ```sh 96 | 97 | 114 | 115 | 116 | ``` 117 | 118 | Menampilkan Data Followers 119 | 120 | ```sh 121 | 122 | 134 | 135 | ``` 136 | 137 | Menampilkan Data Following 138 | 139 | ```sh 140 | 141 | 153 | 154 | ``` 155 | 156 | ### Menggunakan Login Authenticate 157 | 158 | contoh membuat link authentikasi (tombol login ke instagram) 159 | 160 | ```sh 161 | Login"; 163 | ?> 164 | ``` 165 | tombol diatas akan meredirect ke halaman login instagram , setelah si user login maka instagram akan meridirect ke halaman yang anda declare di InstagramConfig.php -> ('redirectUri' => 'bla bla bla'). 166 | 167 | contoh redirectUri : localhost:8000/instagram 168 | 169 | selain meredirect ke halaman url anda tadi , instagram memberikan code di url anda , ini url nya localhost:8000/instagram?code=1234567. 170 | nah di url ini lah kita bisa mendapatkan informasi si user yang login tadi , dengan 171 | cara meggunakan script berikut : 172 | 173 | ```sh 174 | 181 | ``` 182 | 183 | semua informasi user yang login bisa di dapatkan :). 184 | 185 | 186 | 187 | ## Ada Pertanyaan ? 188 | 189 | email aja ke : reza.wikrama3@gmail.com 190 | 191 | ## License 192 | 193 | https://reza.mit-license.org/ 194 | 195 | **ENJOY !!!** -------------------------------------------------------------------------------- /src/Instagram.php: -------------------------------------------------------------------------------- 1 | 7 | */ 8 | 9 | class Instagram 10 | 11 | { 12 | 13 | protected $userId; 14 | 15 | protected $accessToken; 16 | 17 | protected $clientId; 18 | 19 | protected $clientSecret; 20 | 21 | protected $redirectUri; 22 | 23 | public function __construct() 24 | 25 | { 26 | $this->userId = config('config.userId'); 27 | 28 | $this->accessToken = config('config.accessToken'); 29 | 30 | $this->clientId = config('config.clientId'); 31 | 32 | $this->clientSecret = config('config.clientSecret'); 33 | 34 | $this->redirectUri = config('config.redirectUri'); 35 | } 36 | 37 | public function setContents($url) 38 | 39 | { 40 | 41 | try 42 | { 43 | $contents = file_get_contents($url); 44 | 45 | $result = json_decode($contents , true); 46 | 47 | return $result; 48 | }catch(\Exception $e){ 49 | 50 | throw new \Exception("Access Token Atau User ID Salah , silahkan cek lagi!"); 51 | 52 | } 53 | } 54 | 55 | public function getContents($url) 56 | { 57 | return $this->setContents($url)['data']; 58 | } 59 | 60 | public function getResultImage($count="", $max_id = "") 61 | 62 | { 63 | if(!empty($count)) 64 | { 65 | $count = "&count=".$count; 66 | }else{ 67 | $count = ""; 68 | } 69 | 70 | $url = "https://api.instagram.com/v1/users/".$this->userId."/media/recent/?access_token=".$this->accessToken.$count."&max_id=".$max_id; 71 | 72 | return $this->getContents($url); 73 | 74 | } 75 | 76 | 77 | /** 78 | * Helper untuk menampilkan selain image url (Custom) 79 | * Example : 80 | * 81 | * foreach(IG::images() as $row) 82 | * { 83 | * echo "
"; 84 | * echo $row['caption']['text'].'
'; 85 | * } 86 | */ 87 | 88 | public function images($count="") 89 | { 90 | $arr = []; 91 | $id = ""; 92 | do { 93 | $max_id = $id; 94 | $counter = 0; 95 | foreach($this->getResultImage($count, $max_id) as $row) 96 | { 97 | $counter++; 98 | $arr[] = $row; 99 | $id = $row['id']; 100 | } 101 | } while($count > 0 ? $count != $counter : $max_id !== $id); 102 | 103 | return $arr; 104 | } 105 | 106 | /** 107 | * 108 | * onlyImage khusus menangani image url saja. 109 | * 110 | */ 111 | 112 | 113 | public function onlyImage($params = "", $count="") 114 | 115 | { 116 | 117 | $arr = []; 118 | $id = ""; 119 | do { 120 | $max_id = $id; 121 | $counter = 0; 122 | foreach($this->getResultImage($count, $max_id) as $row) 123 | { 124 | $counter++; 125 | $arr[] = $row['images'][$params]['url']; 126 | $id = $row['id']; 127 | } 128 | } while($count > 0 ? $count != $counter : $max_id !== $id); 129 | 130 | return $arr; 131 | } 132 | 133 | public function lowResolution($count = "") 134 | 135 | { 136 | return $this->onlyImage('low_resolution',$count); 137 | } 138 | 139 | public function standardResolution($count="") 140 | 141 | { 142 | return $this->onlyImage('standard_resolution',$count); 143 | } 144 | 145 | public function thumbnail($count="") 146 | 147 | { 148 | return $this->onlyImage('thumbnail',$count); 149 | } 150 | 151 | public function getResultUser() 152 | 153 | { 154 | 155 | $url = "https://api.instagram.com/v1/users/".$this->userId."/?access_token=".$this->accessToken; 156 | 157 | return $this->getContents($url); 158 | 159 | } 160 | 161 | public function profile($param) 162 | 163 | { 164 | return $this->getResultUser()[$param]; 165 | } 166 | 167 | public function username() 168 | 169 | { 170 | return $this->profile('username'); 171 | } 172 | 173 | public function bio() 174 | 175 | { 176 | return $this->profile('bio'); 177 | } 178 | 179 | public function website() 180 | 181 | { 182 | return $this->profile('website'); 183 | } 184 | 185 | public function pic() 186 | 187 | { 188 | return $this->profile('profile_picture'); 189 | } 190 | 191 | public function fullName() 192 | 193 | { 194 | return $this->profile('full_name'); 195 | } 196 | 197 | public function counts($param) 198 | 199 | { 200 | return $this->getResultUser()['counts'][$param]; 201 | } 202 | 203 | public function countFollowers() 204 | 205 | { 206 | return $this->counts('followed_by'); 207 | } 208 | 209 | public function countFollowing() 210 | 211 | { 212 | return $this->counts('follows'); 213 | } 214 | 215 | 216 | 217 | public function displayFollowing() 218 | 219 | { 220 | $url = 'https://api.instagram.com/v1/users/self/follows?access_token='.$this->accessToken; 221 | return $this->getContents($url); 222 | } 223 | 224 | public function displayFollowers() 225 | 226 | { 227 | $url = 'https://api.instagram.com/v1/users/self/followed-by?access_token='.$this->accessToken; 228 | return $this->getContents($url); 229 | } 230 | 231 | public function getCodeAuth() 232 | 233 | { 234 | $url = "https://api.instagram.com/oauth/authorize/?client_id=".$this->clientId."&redirect_uri=".$this->redirectUri."&response_type=code"; 235 | return $url; 236 | } 237 | 238 | public function curlAuth($code) 239 | 240 | { 241 | 242 | $url = "https://api.instagram.com/oauth/access_token"; 243 | 244 | $ch = curl_init(); 245 | 246 | $info = [ 247 | 248 | 'client_id' => $this->clientId, 249 | 'client_secret' => $this->clientSecret, 250 | 'grant_type' => 'authorization_code', 251 | 'redirect_uri' => $this->redirectUri, 252 | 'grant_type' => 'authorization_code', 253 | 'code' => $code, 254 | ]; 255 | 256 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 257 | curl_setopt($ch, CURLOPT_POST, true); 258 | curl_setopt($ch, CURLOPT_POSTFIELDS, $info); 259 | curl_setopt($ch, CURLOPT_URL, $url); 260 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 261 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output 262 | $result = json_decode(curl_exec($ch) , true); 263 | curl_close ($ch); 264 | 265 | return $result; 266 | 267 | } 268 | 269 | public function auth($code) 270 | 271 | { 272 | $auth = $this->curlAuth($code); 273 | 274 | $jadikan_flatt_array = array_flatten($auth); 275 | $masukan_key = ['access_token' , 'username' , 'bio' , 'website' , 'pic' , 'full_name', 'id']; 276 | 277 | $hasil = array_combine($masukan_key, $jadikan_flatt_array); 278 | 279 | return $hasil; 280 | } 281 | 282 | } 283 | --------------------------------------------------------------------------------