PHP Curl simulates logon to the WeChat public platform, Sina Weibo instance code, curl instance

Source: Internet
Author: User

PHP Curl simulates logon to the public platform, Sina Weibo instance code, curl instance

Enable curl configuration before using curl. You can see the specific method for Baidu to enable curl extension. 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 ', 'cookies'); $ 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, expires, 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, CURLOPT_RET URNTRANSFER, 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 micro-beat curl simulated login class. source: Response ($ 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 login, you can directly continue to use the http function to access other Rong. unset ($ sina) will automatically log out. */public $ status; function _ construct ($ su, $ sp, $ flags = 1) {$ this-> status = $ this-> login ($ su, $ sp, $ flags);} function _ destruct () {// logout login $ this-> logout ();} function logout () {http ("http://weibo.com/logout.php "); unset ($ this-> status);}/* No, as long as you do not set the CURLOPT_COOKIESESSION parameter 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 ", $ 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.

Articles you may be interested in:
  • Php curl simulates login Sina Weibo capture page content based on EaglePHP framework development
  • Example of using curl to simulate the collection page after login in php
  • PHP CURL
  • Use CURL in PHP to simulate logon and obtain data instances
  • PHP uses CURL to simulate logon to websites with verification Codes
  • PHP reads CURL to generate Cookie files during simulated Login
  • PHP uses CURL to simulate Logon
  • PHP curl simulates logon to a website with a verification code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.