This article mainly introduces PHPCurl simulated logon to the public platform and Sina Weibo instance code. it involves phpcurl simulated logon knowledge. if you need it, refer to enable curl configuration before using curl, the specific method is baidu. you can see that curl extension is enabled. The password is encrypted with md5, which has been tested successfully. just change the user and password to yours.
The following code describes how php uses curl to simulate logon to the public platform. the specific code is as follows:
<? Php // simulate login $ cookie_file = tempnam ('./temp', 'cooke'); $ 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, success, 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']; // Obtain the source code of the logon page $ go_url =' https://mp.weixin.qq.com '. $ Newurl; $ ch = curl_init ($ go_url); curl_setopt ($ ch, success, 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);?>
Use php curl to simulate logon to Sina Weibo
Sometimes we get some Sina Weibo data, but don't want to use the API, so we have to use simulated login.
I found that the previously usable CURL simulated login code was invalid. Google, I found that many people encountered this problem, but did not find a solution. so I did my research and found the cause.
It may be because Sina restricts the possibility of simulated logon. Similarly, it is normal to log on to the website using the same login parameter. the returned COOKIES are temporary when you log on with CURL.
Therefore, it seems that the logon is successful and the user information is obtained, but the access status is still not logged on again. my solution is relatively simple. simply modify the validity period of COOKIES.
I have attached the following PHP code that I tested. I hope it will be useful to anyone who has the same problem. if you have a better solution, please share it with me.
It is found that you do not need to set the CURLOPT_COOKIESESSION parameter. you do not need to modify COOKIE_FILE.
<? Phpclass sina {/* a simple sina Weibo curl simulated login class. Source: http://chenall.net/post/sina_curl_login/ Usage: The http function is a simple curl encapsulation function that needs to be implemented by yourself. the prototype of the http function is as follows: http ($ url, $ post_data = null) returns the webpage content. the first parameter $ url is the url to be accessed. $ post_data is post data. if it is null, it indicates GET access. 1. use the encrypted password to log on. The encryption method is sha1 (sha1 ($ pass) $ sina = new sina ($ username, $ sha1pass) 2. log on to $ sina = new sina ($ username, $ sha1pass, 0) with the original password. if $ sina-> status is not empty after execution, the logon succeeds. Otherwise, the logon fails. after successful logon, you can directly use the http function to access other content. unset ($ sina) will automatically log out. */public $ status; function _ construct ($ su, $ sp, $ flags = 1) {$ this-> status = $ this-> login ($ su, $ sp, $ flags);} function _ destruct () {// log out $ this-> logout ();} function logout () {http (" http://weibo.com/logout.php "); Unset ($ this-> status);}/* No, as long as the CURLOPT_COOKIESESSION parameter is not set in the HTTP function, you can set it to false. function ResetCookie () // reset related cookies {global $ cookie_file; $ str = file_get_contents ($ cookie_file); $ t = time () + 3600; // Set the cookie validity period to one hour $ 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?1.1.9 ", $ 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 ;}}?>
The above content introduces the PHP Curl simulated logon to the public platform and Sina Weibo instance code. I hope this article will help you.