@@ -221,38 +221,38 @@ private function generateHeaders($session)
221
221
*/
222
222
public function getMedias ($ username , $ count = 20 , $ maxId = '' )
223
223
{
224
- $ index = 0 ;
225
- $ medias = [] ;
226
- $ isMoreAvailable = true ;
227
- while ( $ index < $ count && $ isMoreAvailable) {
228
- $ response = Request:: get (Endpoints:: getAccountMediasJsonLink ( $ username , $ maxId ), $ this -> generateHeaders ( $ this -> userSession ));
229
- if ( static :: HTTP_OK !== $ response -> code ) {
230
- throw new InstagramException ( ' Response code is ' . $ response -> code . ' . Body: ' . static ::getErrorBody ( $ response ->body ) . ' Something went wrong. Please report issue. ' );
231
- }
232
-
233
- $ arr = json_decode ($ response ->raw_body , true , 512 , JSON_BIGINT_AS_STRING );
234
- if (!is_array ($ arr )) {
235
- throw new InstagramException ('Response code is ' . $ response ->code . '. Body: ' . static ::getErrorBody ($ response ->body ) . ' Something went wrong. Please report issue. ' );
236
- }
237
- $ nodes = $ arr ['user ' ]['media ' ]['nodes ' ];
238
- // fix - count takes longer/has more overhead
239
- if (!isset ($ nodes ) || empty ($ nodes )) {
240
- return [];
241
- }
242
- foreach ($ nodes as $ mediaArray ) {
243
- if ($ index === $ count ) {
244
- return $ medias ;
245
- }
246
- $ medias [] = Media::create ($ mediaArray );
247
- $ index ++;
248
- }
249
- if (empty ($ nodes ) || !isset ($ nodes )) {
250
- return $ medias ;
251
- }
252
- $ maxId = $ nodes [ count ( $ nodes ) - 1 ]['id ' ];
253
- $ isMoreAvailable = $ arr ['user ' ]['media ' ]['page_info ' ]['has_next_page ' ];
254
- }
255
- return $ medias ;
224
+ $ account = $ this -> getAccount ( $ username ) ;
225
+ $ index = 0 ;
226
+ $ medias = [] ;
227
+ $ isMoreAvailable = true ;
228
+ while ( $ index < $ count && $ isMoreAvailable ) {
229
+ $ response = Request:: get (Endpoints:: getAccountMediasJsonLink ( $ account -> getId (), $ maxId ), $ this -> generateHeaders ( $ this -> userSession ));
230
+ if ( static ::HTTP_OK !== $ response ->code ) {
231
+ throw new InstagramException ( ' Response code is ' . $ response -> code . ' . Body: ' . static :: getErrorBody ( $ response -> body ) . ' Something went wrong. Please report issue. ' );
232
+ }
233
+ $ arr = json_decode ($ response ->raw_body , true , 512 , JSON_BIGINT_AS_STRING );
234
+ if (!is_array ($ arr )) {
235
+ throw new InstagramException ('Response code is ' . $ response ->code . '. Body: ' . static ::getErrorBody ($ response ->body ) . ' Something went wrong. Please report issue. ' );
236
+ }
237
+ $ nodes = $ arr ['data ' ][ ' user ' ]['edge_owner_to_timeline_media ' ]['edges ' ];
238
+ // fix - count takes longer/has more overhead
239
+ if (!isset ($ nodes ) || empty ($ nodes )) {
240
+ return [];
241
+ }
242
+ foreach ($ nodes as $ mediaArray ) {
243
+ if ($ index === $ count ) {
244
+ return $ medias ;
245
+ }
246
+ $ medias [] = Media::create ($ mediaArray[ ' node ' ] );
247
+ $ index ++;
248
+ }
249
+ if (empty ($ nodes ) || !isset ($ nodes )) {
250
+ return $ medias ;
251
+ }
252
+ $ maxId = $ arr [ ' data ' ]['user ' ][ ' edge_owner_to_timeline_media ' ][ ' page_info ' ][ ' end_cursor ' ];
253
+ $ isMoreAvailable = $ arr ['data ' ][ ' user ' ]['edge_owner_to_timeline_media ' ]['page_info ' ]['has_next_page ' ];
254
+ }
255
+ return $ medias ;
256
256
}
257
257
258
258
/**
@@ -320,52 +320,53 @@ public function getMediaByCode($mediaCode)
320
320
*/
321
321
public function getPaginateMedias ($ username , $ maxId = '' )
322
322
{
323
- $ hasNextPage = true ;
324
- $ medias = [];
325
-
326
- $ toReturn = [
327
- 'medias ' => $ medias ,
328
- 'maxId ' => $ maxId ,
329
- 'hasNextPage ' => $ hasNextPage ,
330
- ];
331
-
332
- $ response = Request::get (Endpoints::getAccountMediasJsonLink ($ username , $ maxId ),
333
- $ this ->generateHeaders ($ this ->userSession ));
334
-
335
- // use a raw constant in the code is not a good idea!!
336
- //if ($response->code !== 200) {
337
- if (static ::HTTP_OK !== $ response ->code ) {
338
- throw new InstagramException ('Response code is ' . $ response ->code . '. Body: ' . static ::getErrorBody ($ response ->body ) . ' Something went wrong. Please report issue. ' );
339
- }
340
-
341
- $ arr = json_decode ($ response ->raw_body , true , 512 , JSON_BIGINT_AS_STRING );
342
-
343
- if (!is_array ($ arr )) {
344
- throw new InstagramException ('Response code is ' . $ response ->code . '. Body: ' . static ::getErrorBody ($ response ->body ) . ' Something went wrong. Please report issue. ' );
345
- }
346
- $ nodes = $ arr ['user ' ]['media ' ]['nodes ' ];
347
-
348
- //if (count($arr['items']) === 0) {
349
- // I generally use empty. Im not sure why people would use count really - If the array is large then count takes longer/has more overhead.
350
- // If you simply need to know whether or not the array is empty then use empty.
351
- if (empty ($ nodes )) {
352
- return $ toReturn ;
353
- }
354
-
355
- foreach ($ nodes as $ mediaArray ) {
356
- $ medias [] = Media::create ($ mediaArray );
357
- }
358
-
359
- $ maxId = $ arr ['user ' ]['media ' ]['page_info ' ]['end_cursor ' ];
360
- $ hasNextPage = $ arr ['user ' ]['media ' ]['page_info ' ]['has_next_page ' ];
361
-
362
- $ toReturn = [
363
- 'medias ' => $ medias ,
364
- 'maxId ' => $ maxId ,
365
- 'hasNextPage ' => $ hasNextPage ,
366
- ];
367
-
368
- return $ toReturn ;
323
+ $ account = $ this ->getAccount ($ username );
324
+ $ hasNextPage = true ;
325
+ $ medias = [];
326
+
327
+ $ toReturn = [
328
+ 'medias ' => $ medias ,
329
+ 'maxId ' => $ maxId ,
330
+ 'hasNextPage ' => $ hasNextPage ,
331
+ ];
332
+
333
+ $ response = Request::get (Endpoints::getAccountMediasJsonLink ($ account ->getId (), $ maxId ),
334
+ $ this ->generateHeaders ($ this ->userSession ));
335
+
336
+ // use a raw constant in the code is not a good idea!!
337
+ //if ($response->code !== 200) {
338
+ if (static ::HTTP_OK !== $ response ->code ) {
339
+ throw new InstagramException ('Response code is ' . $ response ->code . '. Body: ' . static ::getErrorBody ($ response ->body ) . ' Something went wrong. Please report issue. ' );
340
+ }
341
+
342
+ $ arr = json_decode ($ response ->raw_body , true , 512 , JSON_BIGINT_AS_STRING );
343
+
344
+ if (!is_array ($ arr )) {
345
+ throw new InstagramException ('Response code is ' . $ response ->code . '. Body: ' . static ::getErrorBody ($ response ->body ) . ' Something went wrong. Please report issue. ' );
346
+ }
347
+ $ nodes = $ arr ['data ' ]['user ' ]['edge_owner_to_timeline_media ' ]['edges ' ];
348
+
349
+ //if (count($arr['items']) === 0) {
350
+ // I generally use empty. Im not sure why people would use count really - If the array is large then count takes longer/has more overhead.
351
+ // If you simply need to know whether or not the array is empty then use empty.
352
+ if (empty ($ nodes )) {
353
+ return $ toReturn ;
354
+ }
355
+
356
+ foreach ($ nodes as $ mediaArray ) {
357
+ $ medias [] = Media::create ($ mediaArray ['node ' ]);
358
+ }
359
+
360
+ $ maxId = $ arr ['data ' ]['user ' ]['edge_owner_to_timeline_media ' ]['page_info ' ]['end_cursor ' ];
361
+ $ isMoreAvailable = $ arr ['data ' ]['user ' ]['edge_owner_to_timeline_media ' ]['page_info ' ]['has_next_page ' ];
362
+
363
+ $ toReturn = [
364
+ 'medias ' => $ medias ,
365
+ 'maxId ' => $ maxId ,
366
+ 'hasNextPage ' => $ hasNextPage ,
367
+ ];
368
+
369
+ return $ toReturn ;
369
370
}
370
371
371
372
/**
@@ -589,10 +590,10 @@ public function getAccount($username)
589
590
}
590
591
591
592
$ userArray = json_decode ($ response ->raw_body , true , 512 , JSON_BIGINT_AS_STRING );
592
- if (!isset ($ userArray ['user ' ])) {
593
+ if (!isset ($ userArray ['graphql ' ][ ' user ' ])) {
593
594
throw new InstagramException ('Account with this username does not exist ' );
594
595
}
595
- return Account::create ($ userArray ['user ' ]);
596
+ return Account::create ($ userArray ['graphql ' ][ ' user ' ]);
596
597
}
597
598
598
599
/**
0 commit comments