PHP for everyone oauth login and API call

Source: Internet
Author: User
Tags oauth

Everyone open platform to give the PHP SDK, personal feeling write not how, and in My computer run not out, always 113 error, check the document unexpectedly did not have this error code, so had to own according to the official document, with PHP own implementation of a bit. The code below is not encapsulated, just a walk through the process. If it is just for everyone to connect such login authentication and simple API calls, it is not very necessary to encapsulate. As a PHP demonstration using the Everyone API. The process of writing your own code also refers to the official website of the SDK Code and demo code.

Description
1, using the HttpClient class to initiate get and post requests, file http://scripts.incutio.com/httpclient/, put HttpClient.class.php in the server root directory;
2, need to apply for their own API Key and key Secret (see Http://wiki.dev.renren.com/wiki/Authentication), and set the binding domain name (set to localhost is also allowed), The following code is given in the Apikey and Secretkey is the official website demo Code parameters (http://wiki.dev.renren.com/wiki/Php-sdk-connect-demo-2.0), temporarily or can be used;
3, code files please use UTF-8 encoding, save the name "renren.php", put to the server directory;
4, through 127.0.0.1/renren.php access;

The code is as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 The "the" <?php session_start ();? > <meta http-equiv= "content-type"  content= "text/html; Charset=utf-8 " /> <?php require_once " HttpClient.class.php ";   $APIKey  =  ' dd3ffbf2bd894ca9819f5dbc82c2f39c '; $SecretKey  =  ' f6d6d1308e314737ac955ba24a5aecc0 '; $redirecturi  =  ' http://127.0.0.1/renren.php '; $scope  = ' publish_feed,photo_upload ';  //Generate API Signature Sig,sig One parameter for everyone API Function gensig ($params, $secret _key) {    ksort ($ params);     reset ($params);      $str  =  "";     foreach ($params  as  $key = $value) {          $str  .=  "$key = $value";          return md5 ($str. $secret _key);; }  //page status settings for page routing//default is ' 1 '//Get request token (code) after ' 2 '//Get access token is ' magic ' after value   $state  =& nbsp; ' 1 '; if (isset ($_request[' code ')) { &Nbsp;  if (Empty ($_session[' Atoken '))          $state  =   ' 2 '; } if (Isset ($_request[' Magic ")) {     $state  = $_request[' magic '];}  // Page switch ($state) {    case  ' 1 ' that generates a response based on state:  //gives the connection to the login Verification and app Licensing page                    $_session[' atoken '] =   ";          $url  =  "Http://graph.renren.com/oauth/authorize? Client_id= $APIKey ".              "&response_type=code&scope=$ Scope&redirect_uri= $redirecturi ";         echo  "<a href=\" $url \ "> Sign in with everyone's account </a><br/> ";         break;     case  ' 2 ':  //gets access token and gives the connection to the API call &NBSP;&NBSP;&NBSP;&NBSP;              //GET request token, code           $code  = $_request[' code ';                   //Launch acquisition Access token request          $url  =  http://graph.renren.com/oauth/ token?client_id= $APIKey &code= $code ".              "&grant_type=authorization_code &client_secret= $SecretKey &redirect_uri= $redirecturi ";          $json  = httpclient::quickget ($url);                   //parsing back JSON          $jsond  = Json_decode ($json);          $access _token =  $jsond->acceSs_token;                   //Generation Page         if (!empty ($access _token)) {             $_session[' Atoken '] =  $access _token;              $url  =  $redirecturi. "? Magic=3 ";             echo  access token: $access _token <br/> ";             echo  "<a href=\" $url \ "> Invoke API : Users.getinfo</a> ";         }else{             echo  "wrong!<br/>";         }         break;     case  ' 3 ':  //callAPI User.getinfo, displaying the user's name, UID, and Avatar                    //initiating API call request          $access _token = $ _session[' Atoken '];          $params  = array ("Method" = "Users.getinfo", "V" = > "1.0",              "Access_token" and "= $access _ token, "format" = "json");          $params [' sig '] = Gensig ($params, $SecretKey);          $url  =  "http://api.renren.com/restserver.do";          $json  = httpclient::quickpost ($url, $params);                   // Parse returns JSON          $jsond  = Json_decode ($json);          $uid  =  $jsond [' 0 ']->uid;          $tinyurl  =  $jsond [' 0 ']->tinyurl;          $name  =  $jsond [' 0 ']->name;           //Build Page          echo  "Hello $name, your uid is $uid<br/>";         echo  "

PHP implementation for everyone OAuth login and API call

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.