Home > php教程 > PHP源码 > body text

开心网的账号登录及api操作

PHP中文网
Release: 2016-05-25 17:06:53
Original
4061 people have browsed it

开心网的账号登录及api操作,使用oauth 2.0 
官方提供的sdk都太过庞大,这是我自己简化的,提供简单的账号登录、获取个人信息、发布分享等功能,如果需要其他功能可以根据官方的api文档自行添加

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=&#39;&#39;){
        $params=array(
            &#39;response_type&#39;=>&#39;code&#39;,
            &#39;client_id&#39;=>$this->client_id,
            &#39;redirect_uri&#39;=>$callback_url,
            &#39;scope&#39;=>$scope
        );
        return &#39;http://api.kaixin001.com/oauth2/authorize?&#39;.http_build_query($params);
    }
 
    function access_token($callback_url, $code){
        $params=array(
            &#39;grant_type&#39;=>&#39;authorization_code&#39;,
            &#39;code&#39;=>$code,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret,
            &#39;redirect_uri&#39;=>$callback_url
        );
        $url=&#39;https://api.kaixin001.com/oauth2/access_token&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    function access_token_refresh($refresh_token){
        $params=array(
            &#39;grant_type&#39;=>&#39;refresh_token&#39;,
            &#39;refresh_token&#39;=>$refresh_token,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret
        );
        $url=&#39;https://api.kaixin001.com/oauth2/access_token&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    function me(){
        $params=array();
        $url=&#39;https://api.kaixin001.com/users/me.json&#39;;
        return $this->api($url, $params);
    }
 
    function records_add($content, $picurl=&#39;&#39;){
        $params=array(
            &#39;content&#39;=>$content
        );
        if($picurl!=&#39;&#39;)$params[&#39;picurl&#39;]=$picurl;
        $url=&#39;https://api.kaixin001.com/records/add.json&#39;;
        return $this->api($url, $params, &#39;POST&#39;);
    }
 
    function records_me($num=10, $start=0){
        $params=array(
            &#39;start&#39;=>$start,
            &#39;num&#39;=>$num
        );
        $url=&#39;https://api.kaixin001.com/records/me.json&#39;;
        return $this->api($url, $params);
    }
 
    function comment_list($id, $uid, $num=10, $start=0){
        $params=array(
            &#39;objtype&#39;=>&#39;records&#39;,
            &#39;objid&#39;=>$id,
            &#39;ouid&#39;=>$uid,
            &#39;start&#39;=>$start,
            &#39;num&#39;=>$num
        );
        $url=&#39;https://api.kaixin001.com/comment/list.json&#39;;
        return $this->api($url, $params);
    }
 
    function forward_list($id, $uid, $num=10, $start=0){
        $params=array(
            &#39;objtype&#39;=>&#39;records&#39;,
            &#39;objid&#39;=>$id,
            &#39;ouid&#39;=>$uid,
            &#39;start&#39;=>$start,
            &#39;num&#39;=>$num
        );
        $url=&#39;https://api.kaixin001.com/forward/list.json&#39;;
        return $this->api($url, $params);
    }
 
    function like_show($id, $uid, $num=10, $start=0){
        $params=array(
            &#39;objtype&#39;=>&#39;records&#39;,
            &#39;objid&#39;=>$id,
            &#39;ouid&#39;=>$uid,
            &#39;start&#39;=>$start,
            &#39;num&#39;=>$num
        );
        $url=&#39;https://api.kaixin001.com/like/show.json&#39;;
        return $this->api($url, $params);
    }
 
    function api($url, $params, $method=&#39;GET&#39;){
        $params[&#39;access_token&#39;]=$this->access_token;
        if($method==&#39;GET&#39;){
            $result=$this->http($url.&#39;?&#39;.http_build_query($params));
        }else{
            $result=$this->http($url, http_build_query($params), &#39;POST&#39;);
        }
        return $result;
    }
 
    function http($url, $postfields=&#39;&#39;, $method=&#39;GET&#39;, $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==&#39;POST&#39;){
            curl_setopt($ci, CURLOPT_POST, TRUE);
            if($postfields!=&#39;&#39;)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!=&#39;&#39;)$json_r=json_decode($response, true);
        return $json_r;
    }
}
Copy after login

config.php

<?php
//配置文件
header(&#39;Content-Type: text/html; charset=UTF-8&#39;);
 
$kaixin_k=&#39;&#39;; //开心网应用API Key
$kaixin_s=&#39;&#39;; //开心网应用Secret Key
$callback_url=&#39;http://yoururl/callback.php&#39;; //授权回调网址
$scope=&#39;create_records&#39;; //权限列表,具体权限请查看官方的api文档
?>
Copy after login

index.php

<?php
session_start();
require_once(&#39;config.php&#39;);
require_once(&#39;kaixin.php&#39;);
 
$kaixin_t=isset($_SESSION[&#39;kaixin_t&#39;])?$_SESSION[&#39;kaixin_t&#39;]:&#39;&#39;;
 
//检查是否已登录
 
if($kaixin_t!=&#39;&#39;){
    $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[&#39;kaixin_r&#39;]);
    var_dump($result);
    **/
 
    /**
    //发布记录
    $img=&#39;http://www.baidu.com/img/baidu_sylogo1.gif&#39;;
    $result=$kaixin->records_add(&#39;记录内容&#39;, $img);
    var_dump($result);
    **/
 
}else{
    //生成登录链接
    $kaixin=new kaixinPHP($kaixin_k, $kaixin_s);
    $login_url=$kaixin->login_url($callback_url, $scope);
    echo &#39;<a href="&#39;,$login_url,&#39;">点击进入授权页面</a>&#39;;
}
?>
Copy after login

calllback.php

<?php
//授权回调页面,即配置文件中的$callback_url
session_start();
require_once(&#39;config.php&#39;);
require_once(&#39;kaixin.php&#39;);
 
if(isset($_GET[&#39;code&#39;]) && $_GET[&#39;code&#39;]!=&#39;&#39;){
    $kaixin=new kaixinPHP($kaixin_k, $kaixin_s);
    $result=$kaixin->access_token($callback_url, $_GET[&#39;code&#39;]);
}
if(isset($result[&#39;access_token&#39;]) && $result[&#39;access_token&#39;]!=&#39;&#39;){
    echo &#39;授权完成,请记录<br/>access token:<input size="50" value="&#39;,$result[&#39;access_token&#39;],&#39;"><br/>refresh token:<input size="50" value="&#39;,$result[&#39;refresh_token&#39;],&#39;">&#39;;
 
    //保存登录信息,此示例中使用session保存
    $_SESSION[&#39;kaixin_t&#39;]=$result[&#39;access_token&#39;]; //access token
    $_SESSION[&#39;kaixin_r&#39;]=$result[&#39;refresh_token&#39;]; //refresh token
}else{
    echo &#39;授权失败&#39;;
}
echo &#39;<br/><a href="./">返回</a>&#39;;
?>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template