PHP implements Renren oauth login and API call

Source: Internet
Author: User
Tags oauth

The php sdk provided by everyone's open platform, I personally feel that the writing is not very good, and I cannot run it on my computer. It is always a 113 error. I have not found this error code in the document, so I had to follow the official documents and use PHP to implement it myself. The Code is as follows. It is not encapsulated. It just follows the process. It is not necessary to encapsulate logon authentication and Simple API calls only for everyone connection. As a PHP demonstration of using Renren API. You can also refer to the SDK code and Demo code on the official website when writing your own code.

Note:
1. Use the httpclient class to initiate get and post requests. Put httpclient.class.php to the server root directory at the file http://scripts.incutio.com/httpclient;
2, you need to apply for API key and key secret (see http://wiki.dev.renren.com/wiki/Authentication), and set the bound domain name (set to localhost is also allowed ), the apikey and secretkey given in the following code are parameters in the Demo code of the official website (http://wiki.dev.renren.com/wiki/Php-sdk-connect-demo-2.0), can still be used for the moment;
3. Use UTF-8 encoding to save the code file named "Renren. php" and put it in the server directory;
4. Access through 127.0.0.1/Renren. php;

The Code is as follows:

<? PHP session_start ();?> <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/> <? PHP require_once "httpclient. class. PHP "; $ apikey = 'dd3ffbf2bd894ca9819f5dbc82c2f39c'; $ secretkey = 'authorization'; $ redirecturi = 'HTTP: // 127.0.0.1/Renren. PHP '; $ scope = 'Publish _ feed, photo_upload'; // generate the API signature Sig. A function gensig ($ Params, $ secret_key) parameter of the SIG Renren API) {ksort ($ Params); reset ($ Params); $ STR = ""; foreach ($ Params as $ key => $ value) {$ Str. = "$ key = $ value";} return MD5 ($ Str. $ secret_key );} // Page Status settings for page routing // The default value is '1' // get the request token (CODE) followed by '2' // The value of 'Magic 'after obtaining the access token $ state = '1'; If (isset ($ _ request ['code']) {If (empty ($ _ session ['oken']) $ state = '2';} If (isset ($ _ request ['Magic ']) {$ state = $ _ request ['Magic '];} // The page switch ($ state) {Case '1' that generates a response based on the state ': // gives a connection to the login authentication and application license page $ _ session ['oken'] = ''; $ url =" http://graph.renren.com/oauth/authorize? Client_id = $ apikey ". "& response_type = Code & scope = $ Scope & redirect_uri = $ redirecturi "; echo "<a href = \" $ URL \ "> log on with a Renren account </a> <br/>"; break; Case '2': // get access token, give the API call connection // get the request token, that is, code $ code = $ _ request ['code']; // initiate the get 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 ); // parse the returned JSON $ jsond = json_decode ($ JSON); $ access_token = $ jsond-> access_token; // generate the page if (! Empty ($ access_token) {$ _ session ['oken'] = $ access_token; $ url = $ redirecturi ."? Magic = 3 "; echo" access token: $ access_token <br/> "; echo" <a href = \ "$ URL \"> call API: users. getinfo </a> ";}else {echo" wrong! <Br/> ";} break; Case '3': // call the API user. getinfo: displays the user's name, uid, and profile picture. // initiates an API call request $ access_token =$ _ session ['oken']; $ Params = array ("method" => "users. getinfo "," V "=>" 1.0 "," access_token "=> $ access_token," format "=>" JSON "); $ Params ['sig '] = gensig ($ Params, $ secretkey); $ url = "http://api.renren.com/restserver.do"; $ JSON = httpclient: quickpost ($ URL, $ Params ); // parse the returned JSON $ jsond = json_decode ($ JSON); $ uid = $ jsond ['0']-> Uid; $ tinyurl = $ jsond ['0']-> tinyurl; $ name = $ jsond ['0']-> name; // generate the page echo "Hello $ name, your uid is $ uid <br/>"; echo "

  

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.