PHP Curl Demo Login WeChat public platform, Sina Weibo instance code, curl Instance _php tutorial

Source: Internet
Author: User
Tags set cookie sha1

PHP Curl Demo Login Public platform, Sina Weibo instance code, curl instance


Use curl to turn on the Curl configuration first, the specific way Baidu will know, turn on the curl extension. Password with MD5 encryption, which is tested successfully, the user with the password to change to your on the line.

Here is a code to introduce PHP to the public platform using curl simulation, the specific code is as follows:

<?php//Analogue login $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 ']; Get login Page source code $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);?>

Sign in to Sina Weibo using PHP CURL emulation

Sometimes we get some data from Sina Weibo, but don't want to use the API, we have to use the simulation login.

Find out that the previously available Curl emulation login code is not working, and Google has found that a lot of people have encountered this problem. But did not find a solution, so he studied it and found the reason.

May be because Sina limited to not allow the impersonation login, the same login parameters, with the Web login all normal, with curl login, the return of cookies is temporary.

So it looks like the login was successful, and the user information was obtained, but the access is still not logged in. My solution is simple, just modify the limitation of the cookie.

Attached to my own test through the PHP code below, I hope to have the same problem of friends useful, if you have a better plan welcome to share.

It is found that as long as the curlopt_cookiesession parameter is not set, you do not need to modify the cookie_file.

<?phpclass sina{/* A simple Sina micro-stroke Curl Simulation login class. Source: http://chenall.net/post/sina_curl_login/Usage: The HTTP function is a simple curl wrapper function, To implement it yourself, the HTTP function prototype is as follows: http ($url, $post _data = null) returns the Web page content. The first parameter, the URL address to access, $post _data is the post data, if empty, Represents a get access. 1. Use encrypted password Login encryption method: SHA1 (SHA1 ($pass)) $sina = new Sina ($username, $sha 1pass) 2. Log in directly with the original password $sina = new Sina ($ Username, $sha 1pass,0) after execution if the $sina->status is not empty, the login succeeds, or the login fails. Once the login is successful, you can continue to use the HTTP function directly to access other content. Using unset ($sina) The login is automatically logged off. */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);} /* not required, as long as not set the HTTP function does not set the Curlopt_cookiesession parameter is OK, to set can be set to False.function Resetcookie ()//Reset Related Cookie{global $cookie _ file; $str = file_get_contents ($cookie _file), $t = time () +3600;//set cookie valid for one hours $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->server Time). $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 to introduce the PHP Curl simulation login Public platform, Sina Weibo instance code, I hope that this article is helpful to everyone.

Articles you may be interested in:

    • PHP Curl Emulation Login Sina Weibo crawl page content based on eaglephp framework Development
    • PHP uses curl to simulate the example of a page capture after login
    • PHP Curl Get cookies to simulate login method
    • Use curl to simulate login and get data instance in PHP
    • PHP uses curl to implement a mock login to a website with a CAPTCHA
    • PHP read the method of generating a cookie file when the curl is impersonating a login
    • How PHP uses Curl to impersonate a login
    • PHP Curl Emulation Login website with verification code

http://www.bkjia.com/PHPjc/1097315.html www.bkjia.com true http://www.bkjia.com/PHPjc/1097315.html techarticle PHP Curl Simulation login public platform, Sina Weibo instance code, curl instance using Curl before opening curl configuration, specific way Baidu will know, turn on curl extension. Password with ...

  • 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.