|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* User: jakim <[email protected]> |
|
4
|
|
|
* Date: 15.10.2017 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace jakim\ig; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class Endpoint |
|
11
|
|
|
{ |
|
12
|
|
|
public static $baseUrl = 'https://www.instagram.com'; |
|
13
|
|
|
|
|
14
|
|
|
public static function accountInfo($accountId, array $params = []): string |
|
15
|
|
|
{ |
|
16
|
|
|
$params['id'] = $accountId; |
|
17
|
|
|
|
|
18
|
|
|
return static::createUrl('https://i.instagram.com/api/v1/users/{id}/info/', $params); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public static function mediaDetails($code, array $params = []): string |
|
22
|
|
|
{ |
|
23
|
|
|
$params['code'] = $code; |
|
24
|
|
|
$params['__a'] = 1; |
|
25
|
|
|
|
|
26
|
|
|
return static::createUrl('/p/{code}/', $params); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public static function exploreLocations($id, array $params = []): string |
|
30
|
|
|
{ |
|
31
|
|
|
$params['id'] = $id; |
|
32
|
|
|
$params['__a'] = 1; |
|
33
|
|
|
|
|
34
|
|
|
return static::createUrl('/explore/locations/{id}/', $params); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function exploreTags($tag, array $params = []): string |
|
38
|
|
|
{ |
|
39
|
|
|
$params['tag'] = $tag; |
|
40
|
|
|
$params['__a'] = 1; |
|
41
|
|
|
|
|
42
|
|
|
return static::createUrl('/explore/tags/{tag}/', $params); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function tagGraphqlQuery($queryHash, $tagName, $after, $first = null) |
|
46
|
|
|
{ |
|
47
|
|
|
return static::createUrl('/graphql/query/', [ |
|
48
|
|
|
'query_hash' => $queryHash, |
|
49
|
|
|
'variables' => [ |
|
50
|
|
|
'tag_name' => $tagName, |
|
51
|
|
|
'first' => $first ?: rand(2, 11), |
|
52
|
|
|
'after' => $after, |
|
53
|
|
|
], |
|
54
|
|
|
]); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public static function followersGraphqlQuery($queryHash, $accountId, $after = null, $first = null) |
|
58
|
|
|
{ |
|
59
|
|
|
$variables = [ |
|
60
|
|
|
'id' => $accountId, |
|
61
|
|
|
'include_reel' => true, |
|
62
|
|
|
'fetch_mutual' => false, |
|
63
|
|
|
'first' => $first ?: rand(12, 24), |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
|
|
if ($after) { |
|
67
|
|
|
$variables['after'] = $after; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return static::createUrl('/graphql/query/', [ |
|
71
|
|
|
'query_hash' => $queryHash, |
|
72
|
|
|
'variables' => $variables, |
|
73
|
|
|
]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public static function createUrl($endpoint, array $params = []): string |
|
77
|
|
|
{ |
|
78
|
|
|
preg_match_all('/{(.+?)}/', $endpoint, $matches); |
|
79
|
|
|
$placeholders = array_combine($matches['0'], $matches['1']); |
|
80
|
|
|
|
|
81
|
|
|
array_walk($params, function ($value, $key) use (&$params) { |
|
82
|
|
|
$params[$key] = is_array($value) ? json_encode($value) : $value; |
|
83
|
|
|
}); |
|
84
|
|
|
|
|
85
|
|
|
foreach ($placeholders as $placeholder => $key) { |
|
86
|
|
|
if (array_key_exists($key, $params)) { |
|
87
|
|
|
$value = $params[$key]; |
|
88
|
|
|
unset($params[$key]); |
|
89
|
|
|
$endpoint = str_replace($placeholder, $value, $endpoint); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$query = http_build_query($params); |
|
94
|
|
|
$query = $query ? "?{$query}" : ''; |
|
95
|
|
|
$baseUrl = substr($endpoint, 0, 4) !== 'http' ? static::$baseUrl : ''; |
|
96
|
|
|
|
|
97
|
|
|
return $baseUrl . $endpoint . $query; |
|
98
|
|
|
} |
|
99
|
|
|
} |