标签:
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‘=>$callback_url, ‘scope‘=>$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‘=>$this->client_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‘=>$this->client_id, ‘client_secret‘=>$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.config.php
<?php //配置文件 header(‘Content-Type: text/html; charset=UTF-8‘); $kaixin_k=‘‘; //开心网应用API Key $kaixin_s=‘‘; //开心网应用Secret Key $callback_url=‘http://yoururl/callback.php‘; //授权回调网址 $scope=‘create_records‘; //权限列表,具体权限请查看官方的api文档 ?>
3.index.php
<?php session_start(); require_once(‘config.php‘); require_once(‘kaixin.php‘); $kaixin_t=isset($_SESSION[‘kaixin_t‘])?$_SESSION[‘kaixin_t‘]:‘‘; //检查是否已登录 if($kaixin_t!=‘‘){ $kaixin=new kaixinPHP($kaixin_k, $kaixin_s, $kaixin_t); //获取登录用户信息 $result=$kaixin->me(); var_dump($result); /** //access token到期后使用refresh token刷新access token $result=$kaixin->access_token_refresh($_SESSION[‘kaixin_r‘]); var_dump($result); **/ /** //发布记录 $img=‘http://www.baidu.com/img/baidu_sylogo1.gif‘; $result=$kaixin->records_add(‘记录内容‘, $img); var_dump($result); **/ }else{ //生成登录链接 $kaixin=new kaixinPHP($kaixin_k, $kaixin_s); $login_url=$kaixin->login_url($callback_url, $scope); echo ‘<a href="‘,$login_url,‘">点击进入授权页面</a>‘; } ?>
4.callback.php
<?php //授权回调页面,即配置文件中的$callback_url session_start(); 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 ‘授权完成,请记录<br/>access token:<input size="50" value="‘,$result[‘access_token‘],‘"><br/>refresh token:<input size="50" value="‘,$result[‘refresh_token‘],‘">‘; //保存登录信息,此示例中使用session保存 $_SESSION[‘kaixin_t‘]=$result[‘access_token‘]; //access token $_SESSION[‘kaixin_r‘]=$result[‘refresh_token‘]; //refresh token }else{ echo ‘授权失败‘; } echo ‘<br/><a href="./">返回</a>‘; ?>
标签:
原文地址:http://www.cnblogs.com/qhorse/p/4844161.html