This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgetCookies.php
121 lines (106 loc) · 3.61 KB
/
getCookies.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
<?php
namespace juanmicl\igcreator;
function randstring($lenght){
return substr(str_shuffle("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $lenght);
}
function curl_request($request, $url, $payload, $header){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 1,
CURLOPT_CUSTOMREQUEST => $request,
CURLOPT_POSTFIELDS => http_build_query($payload),
CURLOPT_HTTPHEADER => $header,
));
return curl_exec($curl);
curl_close($curl);
}
function curl_request_ToFile($request, $url, $payload, $header, $username, $dir){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 1,
CURLOPT_CUSTOMREQUEST => $request,
CURLOPT_POSTFIELDS => http_build_query($payload),
CURLOPT_HTTPHEADER => $header,
CURLOPT_COOKIEJAR => $dir.'/'.$username.'.txt',
));
return curl_exec($curl);
curl_close($curl);
}
class getCookies {
public function login($username, $password){
$url = 'https://www.instagram.com/accounts/login/ajax/';
$payload = array(
'username' => $username,
'password' => $password,
);
$header = array(
'Cache-Control: no-cache',
'Content-Type: application/x-www-form-urlencoded',
'Referer: https://www.instagram.com/',
'User-Agent: Mozilla/5.0 (Linux; Android 6.0.1; SM-G935T Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/51.0.2704.81 Mobile Safari/537.36',
'X-CSRFToken: '.randstring('32'),
);
$curl_output = curl_request('POST', $url, $payload, $header);
//extract json
preg_match('~\{(?:[^{}]|(?R))*\}~', $curl_output, $ig_response);
$ig_response = json_decode($ig_response[0], true);
//extract cookies
preg_match_all('/^Set-Cookie:\s*([^\r\n]*)/mi', $curl_output, $ms);
$ig_cookies = array();
foreach ($ms[1] as $m) {
list($name, $value) = explode('=', $m, 2);
$ig_cookies[$name] = $value;
}
$array = array(
'igcreator' => array(
'authenticated' => $ig_response['authenticated'],
'userId' => $ig_response['userId'],
'username' => $username,
'password' => $password,
),
'cookies' => $ig_cookies,
);
echo json_encode($array);
}
public function loginToFile($username, $password, $dir){
$url = 'https://www.instagram.com/accounts/login/ajax/';
$payload = array(
'username' => $username,
'password' => $password,
);
$header = array(
'Cache-Control: no-cache',
'Content-Type: application/x-www-form-urlencoded',
'Referer: https://www.instagram.com/',
'User-Agent: Mozilla/5.0 (Linux; Android 6.0.1; SM-G935T Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/51.0.2704.81 Mobile Safari/537.36',
'X-CSRFToken: '.randstring('32'),
);
$curl_output = curl_request_ToFile('POST', $url, $payload, $header, $username, $dir);
//extract json
preg_match('~\{(?:[^{}]|(?R))*\}~', $curl_output, $ig_response);
$ig_response = json_decode($ig_response[0], true);
if ($ig_response['authenticated'] == true){
}
//extract cookies
preg_match_all('/^Set-Cookie:\s*([^\r\n]*)/mi', $curl_output, $ms);
$ig_cookies = array();
foreach ($ms[1] as $m) {
list($name, $value) = explode('=', $m, 2);
$ig_cookies[$name] = $value;
}
$array = array(
'igcreator' => array(
'authenticated' => $ig_response['authenticated'],
'userId' => $ig_response['userId'],
'username' => $username,
'password' => $password,
),
'cookies' => $ig_cookies,
);
echo json_encode($array);
}
}