-
Notifications
You must be signed in to change notification settings - Fork 73
/
InstagramDownload.class.php
177 lines (147 loc) · 4.2 KB
/
InstagramDownload.class.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
class InstagramDownload {
public $input_url;
public $id;
public $type = 'image';
public $download_url;
public $meta_values = array();
public $error_code = 0;
const INSTAGRAM_DOMAIN = 'instagram.com';
function __construct($url = '') {
if (!empty($url)) {
$this->setUrl($url);
}
}
public function setUrl($url) {
$id = $this->validateUrl($url);
if ($id && !is_numeric($id)) {
$this->id = $id;
$this->input_url = $url;
}
else {
$this->id = FALSE;
}
}
public function type() {
return $this->type;
}
public function downloadUrl($force_dl = TRUE) {
if ($this->getError()) {
return FALSE;
}
$status = $this->process($this->input_url);
if ($status) {
if ($force_dl) {
return $this->download_url . '?dl=1';
}
return $this->download_url;
}
return FALSE;
}
protected function process(){
$this->fetch($this->input_url);
if (!is_array($this->meta_values)) {
$this->meta_values = array();
return FALSE;
}
if (!empty($this->meta_values['og:video'])) {
$this->type = 'video';
$this->download_url = $this->meta_values['og:video'];
}
elseif (!empty($this->meta_values['og:image'])) {
$this->type = 'image';
$this->download_url = $this->meta_values['og:image'];
}
else {
return FALSE;
}
return $this->download_url;
}
public function validateUrl($url = NULL) {
if (is_null($url) && isset($this->input_url)) {
$url = $this->input_url;
}
$url = parse_url($url);
if (empty($url['host'])) {
$this->error_code = -1;
return FALSE;
}
$url['host'] = strtolower($url['host']);
if ($url['host'] != self::INSTAGRAM_DOMAIN && $url['host'] != 'www.' . self::INSTAGRAM_DOMAIN) {
$this->error_code = -2;
return FALSE;
}
if (empty($url['path'])) {
$this->error_code = -3;
return FALSE;
}
$args = explode('/', $url['path']);
if (!empty($args[1]) && $args[1] == 'p' && isset($args[2], $args[2][4]) && !isset($args[2][255])) {
$this->error_code = 0;
return $args[2];
}
$this->error_code = -4;
return FALSE;
}
protected function fetch($URI) {
$curl = curl_init($URI);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
if (!empty($_SERVER['HTTP_USER_AGENT'])) {
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
}
$response = curl_exec($curl);
curl_close($curl);
if(!empty($response)) {
return $this->_parse($response);
}
else {
return false;
}
}
protected function _parse($HTML) {
$rawTags = array();
preg_match_all("|<meta[^>]+=\"([^\"]*)\"[^>]" . "+content=\"([^\"]*)\"[^>]+>|i", $HTML, $rawTags, PREG_PATTERN_ORDER);
if(!empty($rawTags)) {
$multiValueTags = array_unique(array_diff_assoc($rawTags[1], array_unique($rawTags[1])));
for($i=0; $i < sizeof($rawTags[1]); $i++) {
$hasMultiValues = false;
$tag = $rawTags[1][$i];
foreach($multiValueTags as $mTag) {
if($tag == $mTag)
$hasMultiValues = true;
}
if($hasMultiValues) {
$this->meta_values[$tag][] = $rawTags[2][$i];
}
else {
$this->meta_values[$tag] = $rawTags[2][$i];
}
}
}
if (empty($this->meta_values)) { return false; }
return $this->meta_values;
}
public function getError() {
if ($this->error_code !== TRUE && $this->error_code !== 0) {
return self::error($this->error_code);
}
return NULL;
}
static function error($id) {
$errors = array(
-1 => 'Invalid URL',
-2 => 'Entered URL is not an ' . self::INSTAGRAM_DOMAIN . ' URL.',
-3 => 'No image or video found in this URL',
-4 => 'No image or video found in this URL',
);
if (isset($errors[$id])) {
return $errors[$id];
}
return 'Unknown error';
}
}