PHPCurl simulates logon to the WeChat public platform, Sina Weibo instance code, curl instance _ PHP Tutorial

Source: Internet
Author: User
PHPCurl simulates logon to the public platform, Sina Weibo instance code, and curl instance. PHPCurl simulates the code used to log on to the public platform and Sina Weibo instances. before using curl for a curl instance, enable the curl configuration. The password uses PHP Curl to simulate logon to the public platform, Sina Weibo instance code, and 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', '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.

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

Http://www.bkjia.com/PHPjc/1097315.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1097315.htmlTechArticlePHP Curl simulation login public platform, Sina Weibo instance code, curl instance before using curl to open the curl configuration, the specific way Baidu will know, enable curl extension. Password...

Related Article

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.