PHP Curl Analog Login Micro-trust public platform, Sina Weibo instance code _php instance

Source: Internet
Author: User
Tags curl function prototype md5 md5 encryption php class sha1

Use curl before opening curl configuration, the specific way Baidu will know, open curl expansion. Password with MD5 encryption, this is tested successfully, the user and password changed to your on the line.

The following section of code to introduce PHP using Curl Analog login micro-trust public platform, the specific code is as follows:

<?php//Analog micro-letter 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 After the source of the page $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); ?>

Use PHP CURL analog login Sina Weibo

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

Found that the previously available Curl analog login code is invalid, Google, and found that a lot of people encounter this problem. But did not find a solution, so I studied it, found the reason.

May be because Sina limited not allow analog login, the same login parameters, with the Web login all normal, with curl login, returned cookies unexpectedly is temporary.

So it looks like the login was successful, and the user information was obtained, but again or not logged in. My solution is relatively simple, directly modify the validity of the cookie so on the line.

Attached to my own test through the PHP code below, I would like to have a friend with the same problem, if you have a better solution to welcome to share.

It is found that you do not have to set the Curlopt_cookiesession parameter, you do not need to modify the cookie_file.

<?php class Sina {/* A simple SINA micro-curl Analog login class. Source: http://chenall.net/post/sina_curl_login/Use Method: http function is a simple curl encapsulation function,
You need to implement the HTTP function prototype as follows: HTTP ($url, $post _data = null) returns the content of the Web page.
The first parameter, $url, is the URL address to access, $post _data is the post data, and if it is null, it represents get access. 1. Use encrypted password Login encryption method: SHA1 (SHA1 ($pass)) $sina = new Sina ($username, $sha 1pass) 2. Login directly with the original password $sina = new Sina ($username, $sha 1pa
ss,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) automatically logs off logins.
* * 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 the HTTP function is not set 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 the cookie valid time 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->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 to introduce the PHP Curl analog login Micro-trust public platform, Sina micro-blog Instance code, I hope this article will help you.

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.