Thinkphp to sign up for Weibo

Source: Internet
Author: User

Micro-Blog Login principle, a brief introduction, through a specific URL to carry a fixed parameter, initiate a request login, then through the callback address, get the code value, through the code value to obtain the Access_token value and the UID value, through the above obtained the value of the user information.

The following basic information briefly describes

First, apply for an account

Address: https://weibo.com/signup/signup.php

Second, improve the information

Perfect information, real-name authentication and other operations

Third, apply for application

Select "Weibo login" and fill in the relevant information to complete the application.

Iv. Application Audit

Get relevant information, app key and app Sercet, and fill in the callback address

Five, front-end code

Fill in "App Key" and "callback address" to replace the order on the link

<a class= "btn btn-info" href= "Https://api.weibo.com/oauth2/authorize?client_id=*****&response_type=code &redirect_uri=****** "> Weibo login </a>

Six, PHP code

Weibo callback public Function Wbback () {//parameter setting $Client _id = "* * *";    Appkey $Client _secret = "* * *"; APP sercet $Redirect _uri = "* * *";        Callback address//Receive Code value $Code = I (' Get.code '); if (! $Code) $this->error ("Code acquisition failed!        "); Get Access_token$url = "https://api.weibo.com/oauth2/access_token?client_id= $Client _id&client_secret= $Client        _secret&grant_type=authorization_code&redirect_uri= $Redirect _uri&code= $Code ";        $info = $this->httpsrequest ($url);        JSON to array $info _json = Json_decode ($info, true);        Obtain the Access_token and UID for obtaining user information $access _token = $info _json[' Access_token ');        $uid = $info _json[' uid '];            if ($info) {//sets the Chinese character set header ("Content-type:text/html;charset=utf-8");            Get user information $info _url = "https://api.weibo.com/2/users/show.json?access_token= $access _token&uid= $uid";            $ch = Curl_init (); Set options, including URL Curl_setopT ($ch, Curlopt_url, $info _url);            curl_setopt ($ch, Curlopt_returntransfer, 1);            curl_setopt ($ch, Curlopt_header, 0);            Executes and obtains the contents of the HTML document $output = curl_exec ($ch);            Curl_close ($ch);            Here you can print all the user information//dump ($output);            $userinfo = Json_decode ($output, true);            $username = $userinfo [' Screen_name '];        echo "Login successful, user name: $username";        }}//httpsrequest Public function httpsrequest ($url, $post _data) {$ch = Curl_init ();        curl_setopt ($ch, Curlopt_url, $url);        curl_setopt ($ch, Curlopt_returntransfer, 1);        Set request for POST type curl_setopt ($ch, Curlopt_post, 1);        Add post data to the request curl_setopt ($ch, Curlopt_postfields, $post _data);        Perform post request, get reply $res = curl_exec ($ch);        Curl_close ($ch);    return $res; }

Vii. Follow-up development, please self-operation, thank you
--------------------------------------------------------Reprint please mark the source, thank you! ---------------------------------------------------------------

Thinkphp to sign up for Weibo

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.