PHP implements everyone OAuth login and API calls

Source: Internet
Author: User
Tags error code json oauth domain name root directory

Everyone open platform to give the PHP SDK, personal feeling write is not how, and in My computer running, always 113 error, check the document unexpectedly still did not have this error code, so had to own according to the official documents, with PHP itself to achieve a bit. The code is as follows, no encapsulation, just walk the process. It is also not necessary to encapsulate the login verification and simple API calls that are used only for all connections. As a PHP demonstration using everyone's API. The process of writing their own code also refers to the official website of the SDK Code and demo code.

Description:

1, the use of httpclient class to initiate get and post requests, file download address http://scripts.incutio.com/httpclient/, put HttpClient.class.php to the server root directory;

2, need to apply for the 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 following code gives the Apikey and Secretkey is the official website demo code in the parameters (http://wiki.dev.renren.com/wiki/Php-sdk-connect-demo-2.0), temporarily or can be used;

3, code file, please use Utf-8 code, save the name "renren.php", put to the server directory;

4, through the 127.0.0.1/renren.php visit;

  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 = ' f6d6d1308e314737ac955ba24a5aecc0 ';


$redirecturi = ' http://127.0.0.1/renren.php ';


$scope = ' publish_feed,photo_upload ';





//Generate API Signature Sig,sig A 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 '


//After obtaining request token (code) is ' 2 '


//Get the value of ' magic ' after access token





$state = ' 1 ';


if (isset ($_request[' code ')) {


if (emptyempty ($_session[' Atoken '))


$state = ' 2 ';


}


if (isset ($_request[' magic ')) {


$state = $_request[' magic '];


}





//pages that generate responses based on state


switch ($state) {


case ' 1 '://gives a connection to the login authentication and application License 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 \ > Login </a><br/> with everyone's account;


break;


case ' 2 '://Get access token, give the connection to the API call





//Get request token, code


$code = $_request[' code '];





//Initiate 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 returns JSON


$jsond = Json_decode ($json);


$access _token = $jsond->access_token;





//Generate page


if (!emptyempty ($access _token)) {


$_session[' atoken ' = $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 API User.getinfo, display user's name, UID, and Avatar





//Initiating API call request


$access _token = $_session[' Atoken '];


$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 returns JSON


$jsond = Json_decode ($json);


$uid = $jsond [' 0 ']->uid;


$tinyurl = $jsond [' 0 ']->tinyurl;


$name = $jsond [' 0 ']->name;





//Generate page


echo "Hello $name, your uid is $uid<br/>";


echo "<img src=\" $tinyurl \ ">";


break;


Default:


break;


}


?>







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.