模拟新浪微博自动登陆
PHP code
class="clipboardWindowBtn">?
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
monospace !important;" class="php plain"><?php
?
?
?
?
?
?
?
????
????????
????????
????????
????????
????????
????????
????????
????????
????
????
????????
????????
????????
????????
????????
????????
????????
????
?
?
????
????
????
?
?
?
?
?
?
?
?
define('REQUEST_METHOD_GET',??????????????? 'GET');
define('REQUEST_METHOD_POST',??????????????? 'POST');
define('REQUEST_METHOD_HEAD',??????????????? 'HEAD');
?
define('COOKIE_FILE',??????????????????????? '/tmp/sina.login.cookie');
?
function?curl_switch_method($curl,?$method)?{
????switch?(?$method)?{
????????case?REQUEST_METHOD_POST:
????????????curl_setopt($curl,?CURLOPT_POST,?TRUE);
????????????break;
????????case?REQUEST_METHOD_HEAD:
????????????curl_setopt($curl,?CURLOPT_NOBODY,?TRUE);
????????????break;
????????case?REQUEST_METHOD_GET:
????????default:
????????????curl_setopt($curl,?CURLOPT_HTTPGET,?TRUE);
????????????break;
????}
}
function?curl_set_headers($curl,?$headers)?{
????if?(?empty($headers)?)?return?;
????if?(?is_string($headers)?)?
????????$headers??? =?explode("\r\n",?$headers);
????#类型修复
????foreach?(?$headers?as?&$header?)?
????????if?(?is_array($header)?)?
????????????$header??? =?sprintf('%s:?%s',?$header[0],?$header[1]);
????curl_setopt($curl,?CURLOPT_HTTPHEADER,?$headers);
}
function?curl_set_datas($curl,?$datas)?{
????if?(?empty($datas)?)?return?;
????curl_setopt($curl,?CURLOPT_POSTFIELDS,?$datas);
}
function?curl_request($url,?$method?=?REQUEST_METHOD_GET,?$datas?=?NULL,?$headers?=?NULL)?{
????static??? $curl;
????if?(?!$curl?)
????????$curl??? =?curl_init();
????curl_switch_method($curl,?$method);
????curl_setopt($curl,?CURLOPT_URL,??????????????????????? $url);
????curl_setopt($curl,?CURLOPT_RETURNTRANSFER,??????????? TRUE);
????curl_setopt($curl,?CURLOPT_FOLLOWLOCATION,??????????? TRUE);
????curl_setopt($curl,?CURLOPT_AUTOREFERER,??????????????? TRUE);
????curl_setopt($curl,?CURLOPT_COOKIEJAR,??????????????? COOKIE_FILE);
????curl_setopt($curl,?CURLOPT_COOKIESESSION,??????????? TRUE);
????if?(?$datas?)?
????????curl_set_datas($curl,?$datas);
????if?(?$headers)?
????????curl_set_headers($curl,?$headers);
????$response??? =?curl_exec($curl);
????if?(?$errno?=?curl_errno($curl)?)?{
????????error_log(sprintf("%10d\t%s\n",?$errno,?curl_error($curl)),?3,?'php://stderr');
????????return?FALSE;
????}
????return?$response;
}
?
function?get_js_timestamp()?{
????return?time()?*?1000?+?rand(0,?999);
}
function?http_build_query_no_encode($datas)?{
????$r??? =?array();
????foreach?(?$datas?as?$k?=>?$v?)?
????????$r[]??? =?$k?.?'='?.?$v;
????return?implode('&',?$r);
}
?
function?makeUrl($url,?$info,?$encode?=?TRUE)?{
????if?(?!is_array($info)?||?empty($info)?)?return?$url;
????$components??? =?parse_url($url);
????if?(?array_key_exists('query',?$components)?)?
????????$query??? =?parse_str($components['query']);
????else?
????????$query??? =?array();
????if?(?is_string($info)?)?$info?=?parse_str($info);
????$query??????? =?array_merge($query,?$info);
????$query??????? =?$encode
??????????????????http_build_query($query)
????????????????:?http_build_query_no_encode($query);
????$components['scheme']??? =?array_key_exists('scheme',?$components)
??????????????????????????????$components['scheme']?.?'://'
????????????????????????????:?'';
????$components['user']??????? =?array_key_exists('user',?$components)
??????????????????????????????$components['user']?.?':'?.?$components[HTTP_URL_PASS]?.?'@'
????????????????????????????:?'';
????$components['host']??????? =?array_key_exists('host',?$components)
??????????????????????????????$components['host']
????????????????????????????:?'';
????$components['port']??????? =?array_key_exists('port',?$components)
??????????????????????????????':'?.?$components['port']
????????????????????????????:?'';
????$components['path']??????? =?array_key_exists('path',?$components)
??????????????????????????????'/'?.?ltrim($components['path'],?'/')
????????????????????????????:?'';
????$components['query']??? =?$query?
??????????????????????????????'?'?.?$query
????????????????????????????:?'';
????$components['fragment']??? =?array_key_exists('fragment',?$components)
??????????????????????????????'#'?.?$components['fragment']
????????????????????????????:?'';
????return?sprintf('%s%s%s%s%s%s%s',?$components['scheme'],?$components['user'],?$components['host'],?
????????????????????????????????$components['port'],?$components['path'],?
????????????????????????????????$components['query'],?$components['fragment']);
}
?
function?encode_username($username)?{
????return?base64_encode(urlencode($username));
}
function?encode_password($pub_key,?$password,?$servertime,?$nonce)?{
????#这里是要用nodejs执行新浪的js文件
????$response??? =?`/usr/local/node.js-0.8.8/bin/node?sina.js?"$pub_key"?"$servertime"?"$nonce"?"$password"`;
????return?substr($response,?0,?strlen($response)?-?1);
}
?
?
function?main_page()?{
????return?curl_request('weibo.com');
}
function?prepare_login_info()?{
????$time??? =?get_js_timestamp();
????$url??? =?makeUrl('http://login.sina.com.cn/sso/prelogin.php',?array(
????????'entry'??????? =>?'sso',?
????????'callback'??? =>?'sinaSSOController.preloginCallBack',?
????????'su'??????? =>?encode_username('undefined'),?
????????'rsakt'??????? =>?'mod',?
????????'client'??? =>?'ssologin.js(v1.4.2)',?
????????'_'??????????? =>?$time,?
????),?FALSE);
????$response??? =?curl_request($url);
????$length??????? =?strlen($response);
????$left??????? =?0;
????$right??????? =?$length?-?1;
????while?(?$left?<?$length?)?
????????if?(?$response[$left]?==?'{'?)?break;
????????else?$left?++;
????while?(?$right?>?0?)
????????if?(?$response[$right]?==?'}'?)?break;
????????else?$right?--;
????$response??? =?substr($response,?$left,?$right?-?$left?+?1);
????return?array_merge(json_decode($response,?TRUE),?array(
????????'preloginTime'??? =>?max(get_js_timestamp()?-?$time,?100),?
????));
}
?
function?login($info,?$username,?$password)?{
????$feedbackurl??? =?makeUrl('http://weibo.com/ajaxlogin.php',?array(
????????'framelogin'??????? =>?1,?
????????'callback'??????????? =>?'parent.sinaSSOController.feedBackUrlCallBack',?
????));
????$datas??? =?array(
????????'encoding'??????????? =>?'UTF-8',?
????????'entry'??????????????? =>?'weibo',?
????????'from'??????????????? =>?'',?
????????'gateway'??????????? =>?1,?
????????'nonce'??????????????? =>?$info['nonce'],?
????????'prelt'??????????????? =>?$info['preloginTime'],?
????????'pwencode'??????????? =>?'rsa2',?
????????'returntype'??????? =>?'META',?
????????'rsakv'??????????????? =>?$info['rsakv'],?
????????'savestate'??????????? =>?7,?
????????'servertime'??????? =>?$info['servertime'],?
????????'service'??????????? =>?'miniblog',?
????????'sp'??????????????? =>?encode_password($info['pubkey'],?$password,?$info['servertime'],?$info['nonce']),?
????????'ssosimplelogin'??? =>?1,?
????????'su'??????????????? =>?encode_username($username),?
????????'url'??????????????? =>?$feedbackurl,?
????????'useticket'??????????? =>?1,?
????????'vsnf'??????????????? =>?1,?
????);
????$url??? =?makeUrl('http://login.sina.com.cn/sso/login.php',?array(
????????'client'??? =>?'ssologin.js(v1.4.2)',?
????),?FALSE);
????$response??? =?curl_request($url,?REQUEST_METHOD_POST,?$datas);
????$sign??????? =?'location.replace(\'';
????$response??? =?substr($response,?strpos($response,?$sign)?+?strlen($sign));
????$location??? =?substr($response,?0,?strpos($response,?'\''));
????$response??? =?curl_request($location);
????$length??????? =?strlen($response);
????$left??????? =?0;
????$right??????? =?$length?-?1;
????while?(?$left?<?$length?)?
????????if?(?$response[$left]?==?'{'?)?break;
????????else?$left?++;
????while?(?$right?>?0?)
????????if?(?$response[$right]?==?'}'?)?break;
????????else?$right?--;
????$response??? =?substr($response,?$left,?$right?-?$left?+?1);
????return?json_decode($response,?true);
}
?
$info??? =?prepare_login_info();
$info??? =?login($info,?'用户名',?'密码');
echo?curl_request('http://weibo.com/u/'?.?$info['userinfo']['uniqueid']?.?$info['userinfo']['userdomain']);