Skip to content

Commit c513b2f

Browse files
authored
Allow getMediasByTag to GraphQL or data access (#1020)
Upgrading a project to php 8 I lost the functionality of `getMediasByTag()` only ever getting a empty array. It looks like a PR changed the array key from ['graphql'] to ['data'] yet 'data' doesn't exist on the responses I get, while 'graphql' does. On the potential that the response might A) differ from time to time or B) somehow be different based on the request origin or something (i.e. I can't prove ['data'] doesn't work for others, while my ['graphql'] doesn't) I propose the following fallback 'reversion' - check for the existence of ['graphql'] then try ['data'] if it's not found.
1 parent 5d8a2af commit c513b2f

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/InstagramScraper/Instagram.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,11 +1204,13 @@ public function getMediasByTag($tag, $count = 12, $maxId = '', $minTimestamp = n
12041204
if (!is_array($arr)) {
12051205
throw new InstagramException('Response decoding failed. Returned data corrupted or this library outdated. Please report issue');
12061206
}
1207-
if (empty($arr['data']['hashtag']['edge_hashtag_to_media']['count'])) {
1207+
$rootKey = array_key_exists('graphql', $arr) ? 'graphql' : 'data';
1208+
1209+
if (empty($arr[$rootKey]['hashtag']['edge_hashtag_to_media']['count'])) {
12081210
return [];
12091211
}
12101212

1211-
$nodes = $arr['data']['hashtag']['edge_hashtag_to_media']['edges'];
1213+
$nodes = $arr[$rootKey]['hashtag']['edge_hashtag_to_media']['edges'];
12121214
foreach ($nodes as $mediaArray) {
12131215
if ($index === $count) {
12141216
return $medias;
@@ -1227,8 +1229,8 @@ public function getMediasByTag($tag, $count = 12, $maxId = '', $minTimestamp = n
12271229
if (empty($nodes)) {
12281230
return $medias;
12291231
}
1230-
$maxId = $arr['data']['hashtag']['edge_hashtag_to_media']['page_info']['end_cursor'];
1231-
$hasNextPage = $arr['data']['hashtag']['edge_hashtag_to_media']['page_info']['has_next_page'];
1232+
$maxId = $arr[$rootKey]['hashtag']['edge_hashtag_to_media']['page_info']['end_cursor'];
1233+
$hasNextPage = $arr[$rootKey]['hashtag']['edge_hashtag_to_media']['page_info']['has_next_page'];
12321234
}
12331235
return $medias;
12341236
}

0 commit comments

Comments
 (0)