1 . kaixin.php
<?php/** * php Library for kaixin001.com * * @author piscdong (http://www.piscdong.com/) */class kaixinphp{function __construct ($client _id, $client _secret, $access _token=null) {$this->client_id= $client _id; $this->client_secret= $client _secret; $this->access_token= $access _token; } function Login_url ($callback _url, $scope = ") {$params =array (' response_type ' = ' code '), ' client_id ' = $this->client_id, ' Redirect_uri ' and $callback _url, ' scope ' and $scope) ; Return ' Http://api.kaixin001.com/oauth2/authorize? '. Http_build_query ($params); } function Access_token ($callback _url, $code) {$params =array (' grant_type ' = ' authorization_code ', ' Code ' = $code, ' client_id ' = $this->client_id, ' Client_secret ' and $this->cli Ent_secret, ' redirect_uri ' = $callback _url); $url = ' https://api.kaixin001.coM/oauth2/access_token '; return $this->http ($url, Http_build_query ($params), ' POST '); } function Access_token_refresh ($refresh _token) {$params =array (' grant_type ' = ' refresh_token ', ' Refresh_token ' = $refresh _token, ' client_id ' and $this->client_id, ' Client_secret ' =&G t; $this->client_secret); $url = ' Https://api.kaixin001.com/oauth2/access_token '; return $this->http ($url, Http_build_query ($params), ' POST '); } function Me () {$params =array (); $url = ' Https://api.kaixin001.com/users/me.json '; return $this->api ($url, $params); } function Records_add ($content, $picurl = ") {$params =array (' content ' = = $content); if ($picurl! = ") $params [' Picurl ']= $picurl; $url = ' Https://api.kaixin001.com/records/add.json '; return $this->api ($url, $params, ' POST '); } function Records_me ($num =10, $start =0) {$params =aRray (' Start ' = $start, ' num ' = $num); $url = ' Https://api.kaixin001.com/records/me.json '; return $this->api ($url, $params); } function Comment_list ($id, $uid, $num =10, $start =0) {$params =array (' objtype ' = ' records '), ' ObjID ' = $id, ' ouid ' = $uid, ' start ' = $start, ' num ' = $num); $url = ' Https://api.kaixin001.com/comment/list.json '; return $this->api ($url, $params); } function Forward_list ($id, $uid, $num =10, $start =0) {$params =array (' objtype ' = ' records '), ' ObjID ' = $id, ' ouid ' = $uid, ' start ' = $start, ' num ' = $num); $url = ' Https://api.kaixin001.com/forward/list.json '; return $this->api ($url, $params); } function Like_show ($id, $uid, $num =10, $start =0) {$params =array (' objtype ' = ' records '), ' ObjID' = ' $id, ' ouid ' = $uid, ' start ' = $start, ' num ' = $num); $url = ' Https://api.kaixin001.com/like/show.json '; return $this->api ($url, $params); } function API ($url, $params, $method = ' GET ') {$params [' Access_token ']= $this->access_token; if ($method = = ' GET ') {$result = $this->http ($url. '? '). Http_build_query ($params)); }else{$result = $this->http ($url, Http_build_query ($params), ' POST '); } return $result; } function http ($url, $postfields = ', $method = ' GET ', $headers =array ()) {$ci =curl_init (); curl_setopt ($ci, Curlopt_ssl_verifypeer, FALSE); curl_setopt ($ci, Curlopt_returntransfer, 1); curl_setopt ($ci, Curlopt_connecttimeout, 30); curl_setopt ($ci, Curlopt_timeout, 30); if ($method = = ' POST ') {curl_setopt ($ci, Curlopt_post, TRUE); if ($postfields! = ") curl_setopt ($ci, Curlopt_postfields, $postfields); } $headers []= "user-agent:kaixinphp (piscdong.com)"; curl_setopt ($ci, Curlopt_httpheader, $headers); curl_setopt ($ci, Curlopt_url, $url); $response =curl_exec ($CI); Curl_close ($CI); $json _r=array (); if ($response! = ") $json _r=json_decode ($response, true); return $json _r; }}
2
<?php//configuration file header (' content-type:text/html; Charset=utf-8 '); $kaixin _k= '; NET Application API Key $kaixin _s= "; Happy NET application secret Key $callback _url= ' http://yoururl/callback.php '; Authorization callback URL $scope = ' create_records '; Permissions list, see official API documentation for specific permissions?>
3. index.php
<?php session_start (); Require_once (' config.php '); Require_once (' kaixin.php '); $kaixin _t=isset ($_session[' kaixin_t ')? $_session[' kaixin_t ']: '; Check if you are signed in if ($kaixin _t!= ") { $kaixin =new kaixinphp ($kaixin _k, $kaixin _s, $kaixin _t); Get login user information $result = $kaixin->me (); Var_dump ($result); /** //access token expires with the refresh token refreshed access token $result = $kaixin->access_token_refresh ($_session[' Kaixin_r ']); Var_dump ($result); **/ /** //release record $img = ' http://www.baidu.com/img/baidu_sylogo1.gif '; $result = $kaixin->records_add (' record content ', $img); Var_dump ($result); **/}else{ //Generate Login link $kaixin =new kaixinphp ($kaixin _k, $kaixin _s); $login _url= $kaixin->login_url ($callback _url, $scope); Echo ' <a href= ', $login _url, ' > Click to enter the authorization page </a> '; }?>
4
<?php//authorization callback page, i.e. $callback_urlsession_start () in config file, require_once (' config.php '); require_once (' kaixin.php '); if (isset ($_get[' Code ')) && $_get[' code ']!= ') { $kaixin =new kaixinphp ($kaixin _k, $kaixin _s); $result = $kaixin->access_token ($callback _url, $_get[' Code ');} if (Isset ($result [' Access_token ']) && $result [' Access_token ']!= ') { echo ' authorization completed, please record <br/>access Token:<input size= "value=" ', $result [' Access_token '], ' "><br/>refresh token:<input size=" value = "', $result [' Refresh_token '], ' > '; Save the login information in this example using SESSION save $_session[' kaixin_t ']= $result [' Access_token '];//access token $_session[' kaixin _r ']= $result [' Refresh_token ']; Refresh token}else{ echo ' authorization failed ';} Echo ' <br/><a href= './' > Return </a> ';? >
Net account login and API operation