PHP OAuth Authorization and local encryption implementation method _php instance

Source: Internet
Author: User
Tags curl oauth openid

1.Oauth(Open Licensing) is an open standard that allows a third party to apply access to private resources (such as photos, videos, contact lists) stored on a Web site without having to provide a user name and password to a third party

Keywords: appkey appsecret token (token)

2.SSO Authorization

If the local mobile phone is equipped with microblogging client, then jump directly to the microblog client, just click on the authorization button, you can log in

QQ Third party login using Oauth2.0 implementation, test code

Click on the connection below

https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=101334262&redirect_uri=http:// www.qingguow.cn/sso.php

Specific code sso.php file:

<?php//QQ Login class sso{const app_id= "101334262";
  Const app_key= "xxxxxxxxxxxxxxx";
  Initializes the public static function init () {Header ("content-type:text/html;charset=utf-8");
    }//main function public static function main () {//Request control $action =$_get[' action '];
      if (!empty ($action)) {Sso:: $action ();
    Return } $par = ' Grant_type=authorization_code '. ' &client_id= '. sso::app_id. ' &client_secret= '. Sso::app_key. ' &code= '. $_request[' code '.
    ' &redirect_uri= '. UrlEncode (' http://www.qingguow.cn/sso.php ');
    $rec =sso::p osturlcontents ("Https://graph.qq.com/oauth2.0/token", $par);
      if (Strpos ($rec, ' Access_token ')!== false) {parse_str ($rec, $accessToken); $openidJson =sso::geturlcontents ("https://graph.qq.com/oauth2.0/me?callback=callback&access_token={$
      accesstoken[' Access_token ']};
      $openidJson =str_replace ("Callback", "", $openidJson);
      $openidJson =str_replace (");", "", $openidJson); $oPenidjson=json_decode ($openidJson, true); Header ("location:sso.php?action=getqqinfo&openid={$openidJson [' OpenID ']}&access_token={$accessToken [']
    Access_token ']} ");
    }///Get user Information public static function Getqqinfo () {sso::init ();
    $openid =$_get[' OpenID '];
    $access _token=$_get[' Access_token ']; $userJson =sso::geturlcontents ("https://graph.qq.com/user/get_user_info?openid={$openid}&access_token={$ Access_token}&oauth_consumer_key= ".
    SSO::APP_ID);
    $user =json_decode ($userJson, true);
  Print_r ($user);
    //get-mode Request data public static function Geturlcontents ($url) {$ch = Curl_init ();
    curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);
    curl_setopt ($ch, Curlopt_header, false);
    curl_setopt ($ch, curlopt_followlocation, true);
    curl_setopt ($ch, Curlopt_url, $url);
    curl_setopt ($ch, Curlopt_referer, $url);
    curl_setopt ($ch, Curlopt_returntransfer, TRUE);
    $result = curl_exec ($ch);
    Curl_close ($ch);
  return $result; }//pOST request data public static function Posturlcontents ($url, $data = null) {$curl = Curl_init ();
    curl_setopt ($curl, Curlopt_url, $url);
    curl_setopt ($curl, Curlopt_ssl_verifypeer, FALSE);
    curl_setopt ($curl, Curlopt_ssl_verifyhost, FALSE);
    if (!empty ($data)) {curl_setopt ($curl, Curlopt_post, 1);
    curl_setopt ($curl, Curlopt_postfields, $data);
    curl_setopt ($curl, Curlopt_returntransfer, 1);
    $output = curl_exec ($curl);
    Curl_close ($curl);
  return $output; } sso::main ();

Above this PHP OAuth authorization and local encryption implementation method is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.