使用curl之前先打開(kāi)curl配置,具體方式百度一下就知道,開(kāi)啟curl擴(kuò)展。密碼用md5加密,這是經(jīng)過(guò)測(cè)試成功的,把用戶跟密碼改成你的就行了。
下面一段代碼給大家介紹php使用curl模擬登錄微信公眾平臺(tái),具體代碼如下所示:
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
|
<?php //模擬微信登入 $cookie_file = tempnam( './temp' , 'cookie' ); $login_url = ' https://mp.weixin.qq.com/cgi-bin/login ' ; $pwd = md5( "********" ); $data = "f=json&imgcode=&pwd=$pwd&username=*****@***.com" ; $ch = curl_init( $login_url ); curl_setopt( $ch ,CURLOPT_RETURNTRANSFER,true); curl_setopt( $ch ,CURLOPT_POST,1); curl_setopt( $ch ,CURLOPT_COOKIEJAR, $cookie_file ); curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER, false); curl_setopt( $ch ,CURLOPT_SSL_VERIFYHOST,false); curl_setopt( $ch ,CURLOPT_REFERER, ' https://mp.weixin.qq.com ' ); curl_setopt( $ch ,CURLOPT_POSTFIELDS, $data ); $content = curl_exec( $ch ); curl_close( $ch ); $newurl = json_decode( $content ,1); //var_dump($newurl); //exit; $newurl = $newurl [ 'redirect_url' ]; //獲取登入后頁(yè)面的源碼 $go_url = ' https://mp.weixin.qq.com ' . $newurl ; $ch = curl_init( $go_url ); curl_setopt( $ch ,CURLOPT_RETURNTRANSFER,true); curl_setopt( $ch ,CURLOPT_COOKIEFILE, $cookie_file ); curl_setopt( $ch ,CURLOPT_CONNECTTIMEOUT,0); curl_setopt( $ch , CURLOPT_HEADER, 0); curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER, false); $content = curl_exec( $ch ); //var_dump(curl_error($ch)); print_r( $content ); curl_close( $ch ); ?> |
有時(shí)候我們獲取一些新浪微博的數(shù)據(jù),但又不想使用API,只好使用模擬登錄了.
發(fā)現(xiàn)以前可以使用的CURL模擬登錄代碼失效了,Google一下,發(fā)現(xiàn)有很多人碰到這個(gè)問(wèn)題.但是沒(méi)有找到解決方法,所以就自己研究了一下,發(fā)現(xiàn)了原因.
可能是因?yàn)樾吕讼拗屏瞬辉试S模擬登錄,同樣的登錄參數(shù),用網(wǎng)頁(yè)登錄一切正常,用CURL登錄,返回的COOKIES竟然是臨時(shí)的.
所以看起來(lái)是登錄成功了,并且獲取到了用戶信息,但是再次訪問(wèn)還是未登錄狀態(tài).我的解決方法比較簡(jiǎn)單,直接修改COOKIES的時(shí)效這樣就行了.
附上我自己測(cè)試通過(guò)的PHP代碼如下,希望有對(duì)有同樣問(wèn)題的朋友有用,如果你有更好的方案歡迎分享一下.
發(fā)現(xiàn)只要不設(shè)置CURLOPT_COOKIESESSION參數(shù)就行了,不需要修改COOKIE_FILE.
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
|
<?php class sina { /* 一個(gè)簡(jiǎn)單的新浪微搏curl模擬登錄類(lèi). 來(lái)源: http://chenall.net/post/sina_curl_login/ 使用方法: http函數(shù)是一個(gè)簡(jiǎn)單的curl封裝函數(shù),需要自己去實(shí)現(xiàn), http函數(shù)原型如下: http($url,$post_data = null) 返回網(wǎng)頁(yè)內(nèi)容. 第一個(gè)參數(shù)$url,就是要訪問(wèn)的url地址,$post_data是post數(shù)據(jù),如果為空,則代表GET訪問(wèn). 1.使用加密后密碼登錄 加密方法: sha1(sha1($pass)) $sina = new sina($username,$sha1pass) 2.直接使用原始密碼登錄 $sina = new sina($username,$sha1pass,0) 執(zhí)行之后如果$sina->status非空,則登錄成功,否則登錄失敗. 登錄成功之后,你就可以直接繼續(xù)使用http函數(shù)來(lái)訪問(wèn)其它內(nèi)容. 使用 unset($sina) 會(huì)自動(dòng)注銷(xiāo)登錄. */ public $status ; function __construct( $su , $sp , $flags = 1) { $this ->status = $this ->login( $su , $sp , $flags ); } function __destruct() { //注銷(xiāo)登錄 $this ->logout(); } function logout() { http( " http://weibo.com/logout.php " ); unset( $this ->status); } /*不需要了,只要不設(shè)置HTTP函數(shù)中不設(shè)置CURLOPT_COOKIESESSION參數(shù)就行了,要設(shè)可以設(shè)為false. function ResetCookie()//重置相關(guān)cookie { global $cookie_file; $str = file_get_contents($cookie_file); $t = time()+3600;//設(shè)置cookie有效時(shí)間一個(gè)小時(shí) $str = preg_replace("/\t0\t/", "\t".$t."\t", $str); $f = fopen($cookie_file,"w"); fwrite($f,$str); fclose($f); } */ function login( $su , $sp , $flags = 0) { $su = urlencode( base64_encode ( $su )); $data = http( " http://login.sina.com.cn/sso/prelogin.php?entry=miniblog&client=ssologin.js&user= " . $su ); if ( empty ( $data )) return null; //$data = substr($data,35,-1); $data = json_decode( $data ); if ( $data ->retcode != 0) return null; if ( $flags == 0) $sp = sha1(sha1( $sp )); $sp .= strval ( $data ->servertime). $data ->nonce; $sp = sha1( $sp ); $data = "url=http%3A%2F%2Fweibo.com%2Fajaxlogin.php%3F&returntype=META&ssosimplelogin=1&su=" . $su . '&service=miniblog&servertime=' . $data ->servertime. "&nonce=" . $data ->nonce. '&pwencode=wsse&sp=' . $sp ; $data = http( " http://login.sina.com.cn/sso/login.php?client=ssologin.js " , $data ); //$this->ResetCookie(); if (preg_match( "/location\.replace\('(.*)'\)/" , $data , $url )) { $data = http( $url [1]); //$this->ResetCookie(); $data = json_decode( substr ( $data ,1,-2)); if ( $data ->result == true) return $data ->userinfo; } return null; } } ?> |
以上內(nèi)容給大家介紹了PHP Curl模擬登錄微信公眾平臺(tái)、新浪微博實(shí)例代碼,希望本文所述對(duì)大家有所幫助。