在php中要實現用戶登錄我們一般都會到curl模擬登陸功能,下面我就基于php的curl來實現登錄人人網,完整的例子希望對各位有幫助,代碼如下:
- <?php
- $cookie_file = dirname(__FILE__)."/renren.cookie";
- $login_url = 'http://passport.renren.com/PLogin.do';
- $post_fields['email'] = '';//人人的帳號
- $post_fields['password'] = '';//人人密碼
- $post_fields['origURL'] = 'http%3A%2F%2Fhome.renren.com%2FHome.do';
- $post_fields['domain'] = 'renren.com';
- $ch = curl_init($login_url);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
- curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
- $content = curl_exec($ch);
- curl_close($ch);
- //匹配用戶的ID
- $send_url='http://www.renren.com/home';
- $ch = curl_init($send_url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
- curl_exec($ch);
- $info = curl_getinfo($ch);
- curl_close($ch);
- //獲取用戶id
- $tmp = explode('/',$info['redirect_url']);
- $uid = array_pop($tmp);
- unset($tmp);
- //$uid = "305115027";
- //獲取token和rtk
- $send_url='http://www.renren.com/'.$uid;
- $ch = curl_init($send_url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
- $tmp = curl_exec($ch);
- curl_close($ch);
- preg_match_all("/get_check:'(.*?)',get_check_x:'(.*?)',/is",$tmp,$arr);
- $token = $arr[1][0];//1121558104
- $rtk = $arr[2][0];//e9a9cb2
- //發布信息
- $poststr['content'] = "這就是一個測試而已!!!";
- $poststr['withInfo'] = '{"wpath":[]}';
- $poststr['hostid:'] = $uid;
- $poststr['privacyParams'] = '{"sourceControl": 99}';
- $poststr['requestToken'] = $token;
- $poststr['_rtk'] = $rtk;
- $poststr['channel'] = "renren";
- $head = array(
- 'Referer:http://shell.renren.com/ajaxproxy.htm',
- 'X-Requested-With:XMLHttpRequest',
- );
- $ch = curl_init("http://shell.renren.com/{$uid}/status");
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
- curl_setopt($ch,CURLOPT_HTTPHEADER,$head);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $poststr);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
- $content = curl_exec($ch);//開源代碼Vevb.com
- curl_close($ch);
- $data = json_decode($content,true);
- if($data["code"] == "0"){
- echo "發布成功!";
- }else{
- echo "shit !!!";
- }
新聞熱點
疑難解答