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