// +---------------------------------------------------------------------- // | Copyright (c) 2009 http://826v.com All rights reserved. // +---------------------------------------------------------------------- // | Author: qiandutianxia <852997402@qq.com> // +---------------------------------------------------------------------- namespace Org\Util; /** * 多说评论扩展类 */ class Duoshuo { // 站点注册的多说二级域名 private $short_name; //站点密钥 private $secret; public function __construct(){ $this->short_name="826v"; $this->secret="c9a0ab2eada36f2ef4674cd899865c92"; } /** * 获取文章评论、转发数 * @param id(int) 文章id * return response(json) 包含评论数和转发数 **/ public function getComments($id){ $url="http://api.duoshuo.com/threads/counts.json"; $param[&#039;short_name&#039;]=$this->short_name; $param[&#039;threads&#039;]=$id; $param =http_build_query($param,&#039;&#039;,&#039;&&#039;); $url=$url."?".$param; $respOnse=$this->getUrl($url); if(empty($response)) exit("返回结果错误"); if(json_decode($response)->code==0){ return $response; }else{ exit(json_decode($response)->errorMessage); } } /** * 获取热评文章 * @param range 可选值daily:每日热评文章 weekly:每周热评文章 monthly:每月热评文章;all:总热评文章 * @param num_items(int)评论的条数 默认是5条 * @param channel_key(int) 文章所属分类 * return response(json) 包含文章的id,title等信息 **/ public function getTop($range="all",$num_items=5,$channel_key=""){ $url="http://api.duoshuo.com/sites/listTopThreads.json"; $param[&#039;short_name&#039;]=$this->short_name; $param[&#039;range&#039;]=$range; $param[&#039;num_items&#039;]=$num_items; $param[&#039;channel_key&#039;]=$channel_key; $param =http_build_query($param,&#039;&#039;,&#039;&&#039;); $url=$url."?".$param; $respOnse=$this->getUrl($url); if(empty($response)) exit("返回结果错误"); if(json_decode($response)->code==0){ return $response; }else{ exit(json_decode($response)->errorMessage); } } /** * 获取文章评论 * @param range 可选值daily:每日热评文章 weekly:每周热评文章 monthly:每月热评文章;all:总热评文章 * @param id(int) 文章id * @param page(int) 获取第几页 * @param limit(int) 每一页显示的条数 * @param order(string) 返回的评论的排序规则,可选择desc或者asc,默认是desc * return response(json) **/ public function getComment($id,$page,$limit,$order="desc"){ $url="http://api.duoshuo.com/threads/listPosts.json"; $param[&#039;short_name&#039;]=$this->short_name; $param[&#039;thread_key&#039;]=$id; $param[&#039;page&#039;]=$page; $param[&#039;limit&#039;]=$limit; $param[&#039;order&#039;]=$order; $param =http_build_query($param,&#039;&#039;,&#039;&&#039;); $url=$url."?".$param; $respOnse=$this->getUrl($url); if(empty($response)) exit("返回结果错误"); if(json_decode($response)->code==0){ return $response; }else{ exit(json_decode($response)->errorMessage); } } /** * 获取多说用户信息 * @param id 用户id * return response(json) **/ public function getUsrInfo($id){ $url="http://api.duoshuo.com/users/profile.json"; $param[&#039;user_id&#039;]=$id; $param =http_build_query($param,&#039;&#039;,&#039;&&#039;); $url=$url."?".$param; $respOnse=$this->getUrl($url); if(empty($response)) exit("返回结果错误"); if(json_decode($response)->code==0){ return $response; }else{ exit(json_decode($response)->errorMessage); } } /** * 同步用户到多说 * @param usr(array) 是一个数组可以是一维数组也可以是二维 * 例如:$usr[&#039;user_key&#039;]=1111 这个一般就是用户数据库的ID * $usr[&#039;name&#039;]="zhangsan" 这个一般就是用户数据库的name * 可选参数role(角色),avatar_url(头像),url(网站),email(邮箱),created_at(时间) **/ public function syncUsr($usr){ $url="http://api.duoshuo.com/users/import.json"; $param[&#039;short_name&#039;]=$this->short_name; $param[&#039;secret&#039;]=$this->secret; $s=$this->isOneOrTwo($usr); if($s==1){ $param[&#039;users&#039;][]=$usr; }else{ foreach($usr as $k=>$v){ $param[&#039;users&#039;]=$v; } } $param =http_build_query($param,&#039;&#039;,&#039;&&#039;); $respOnse=$this->postUrl($url,$param); if(empty($response)) exit("返回结果错误"); if(json_decode($response)->code==0){ return $response; }else{ exit(json_decode($response)->errorMessage); } } /** * SSO登录用户同步到多说 * 多说的js代码中设置回调登录地址 然后回调地址中有code **/ public function ssoSync($code){ $url="http://api.duoshuo.com/sites/join.json"; $param[&#039;short_name&#039;]=$this->short_name; $param[&#039;code&#039;]=$code; $param =http_build_query($param, &#039;&#039;, &#039;&&#039;); $respOnse=$this->postUrl($url,$param); if(empty($response)) exit("返回结果错误"); if(json_decode($response)->code==0){ return $response; }else{ exit(json_decode($response)->errorMessage); } } //判断是几维数组 private function isOneOrTwo($arr){ if(is_array($arr)){ $s=1; foreach($arr as $v){ if(is_array($v)){ $s=2; break; } } return $s; }else{ exit("数组错误"); } } //CURL GET private function getUrl($url){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 5); if (!empty($options)){ curl_setopt_array($ch, $options); } $data = curl_exec($ch); curl_close($ch); return $data; } //CURL POST private function postUrl($url,$post_data){ $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); ob_start(); curl_exec($ch); $result = ob_get_contents(); ob_end_clean(); return $result; } }
AD:真正免费,域名+虚机+企业邮箱=0元