-
Notifications
You must be signed in to change notification settings - Fork 798
/
Instagram.php
95 lines (86 loc) · 3.6 KB
/
Instagram.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace InstagramScraper;
use InvalidArgumentException;
use Unirest\Request;
class Instagram implements InstagramDataProvider
{
const INSTAGRAM_URL = 'https://www.instagram.com/';
public function getAccount($username)
{
$response = Request::get(self::INSTAGRAM_URL . $username);
if ($response->code === 404) {
throw new InstagramNotFoundException('Account with given username does not exist.');
}
if ($response->code !== 200) {
throw new InstagramException('Response code is not equal 200. Something went wrong. Please report issue.');
}
$json = self::getJsonPayload($response->body);
$userArray = json_decode($json, true);
if (!is_array($userArray)) {
throw new InstagramException('Response decoding failed. Returned data corrupted or this library outdated. Please report issue');
}
if (!isset($userArray['entry_data']['ProfilePage'])) {
throw new InstagramException('Account with this username does not exist');
}
return Account::fromAccountPage($userArray['entry_data']['ProfilePage'][0]['user']);
}
private static function getJsonPayload($pageString)
{
$arr = explode('window._sharedData = ', $pageString);
$json = explode(';</script>', $arr[1]);
return $json[0];
}
public function getMedias($username, $count = 20)
{
$index = 0;
$medias = [];
$maxId = '';
$isMoreAvailable = true;
while ($index < $count && $isMoreAvailable) {
$response = Request::get(self::INSTAGRAM_URL . $username . '/media/?max_id=' . $maxId);
if ($response->code !== 200) {
throw new InstagramException('Response code is not equal 200. Something went wrong. Please report issue.');
}
$arr = json_decode($response->raw_body, true);
if (!is_array($arr)) {
throw new InstagramException('Response decoding failed. Returned data corrupted or this library outdated. Please report issue');
}
if (count($arr['items']) === 0) {
return [];
}
foreach ($arr['items'] as $mediaArray) {
if ($index === $count) {
return $medias;
}
$medias[] = Media::fromApi($mediaArray);
$index++;
}
$maxId = $arr['items'][count($arr['items']) - 1]['id'];
$isMoreAvailable = $arr['more_available'];
}
return $medias;
}
public function getMediaByCode($mediaCode)
{
return self::getMediaByUrl(self::INSTAGRAM_URL . 'p/' . $mediaCode);
}
public function getMediaByUrl($mediaUrl)
{
if (filter_var($mediaUrl, FILTER_VALIDATE_URL) === false) {
throw new InvalidArgumentException('Malformed media url');
}
$response = Request::get($mediaUrl);
if ($response->code === 404) {
throw new InstagramNotFoundException('Media with given code does not exist or account is private.');
}
if ($response->code !== 200) {
throw new InstagramException('Response code is not equal 200. Something went wrong. Please report issue.');
}
$json = self::getJsonPayload($response->body);
$mediaArray = json_decode($json, true);
if (!isset($mediaArray['entry_data']['PostPage'])) {
throw new InstagramException('Media with this code does not exist');
}
return Media::fromMediaPage($mediaArray['entry_data']['PostPage'][0]['media']);
}
}