>  기사  >  php教程  >  多说评论实时同步回数据库

多说评论实时同步回数据库

WBOY
WBOY원래의
2016-06-07 11:34:591499검색

多说评论实时同步回数据库,依赖Http库,随便找一个就行

<?php <br /> /**<br>  * 多说 实时同步评论回本地数据库 API<br>  */<br> namespace Home\Controller;<br> use Think\Controller;<br> class DuoshuoController extends Controller {<br>     public function duoshuo(){<br> <br>         if (check_signature($_POST)) {<br> <br>             $Comment = M('comment');<br>             $where['name'] = array('in','last_log_id');<br>             $last_log_id = M('setting')->where($where)->getField('value');<br>             Vendor('CurlHTTP.Http');<br>             <br>             $limit = 20;<br> <br>             $params = array(<br>                 'limit' => $limit,<br>                 'order' => 'asc',<br>             );<br> <br> <br>             $posts = array();<br> <br>             if (!$last_log_id)<br>                 $last_log_id = 0;<br> <br>             $params['since_id'] = $last_log_id;<br>             $params['short_name'] = C(DUOSHUO_SHORT_NAME);<br>             $params['secret'] = C(DUOSHUO_SECRET);<br>             $http_client = new \Http();<br>             $response = $http_client->request('http://api.duoshuo.com/log/list.json', $params ,'GET');<br> <br>             $response = json_decode($response,true);<br> <br>             if (!isset($response['response'])) {<br>                 //处理错误,错误消息$response['message'], $response['code']<br>                 file_log("签名错误");<br>             } else {<br>                 //遍历返回的response,你可以根据action决定对这条评论的处理方式。<br>                 foreach($response['response'] as $k => $log){<br>                     switch($log['action']){<br>                         case 'create':<br>                             foreach ($log['meta'] as $key => $value) {<br>                                 $create[$key]['post_id'] = $log['meta']['post_id'];<br>                                 $create[$key]['thread_id'] = $log['meta']['thread_id'];<br>                                 $create[$key]['author_id'] = $log['meta']['author_id'];<br>                                 $create[$key]['author_name'] = $log['meta']['author_name'];<br>                                 $create[$key]['author_email'] = $log['meta']['author_email'];<br>                                 $create[$key]['author_url'] = $log['meta']['author_url'];<br>                                 $create[$key]['author_key'] = $log['meta']['author_key'];<br>                                 $create[$key]['ip'] = $log['meta']['ip'];<br>                                 $create[$key]['created_at'] = $log['meta']['created_at'];<br>                                 $create[$key]['message'] = $log['meta']['message'];<br>                                 $create[$key]['status'] = $log['meta']['status'];<br>                                 $create[$key]['type'] = $log['meta']['type'];<br>                                 if($log['meta']['parent_id'] != ""){<br>                                     $create[$key]['parent_id'] = $log['meta']['parent_id'];<br>                                 }<br>                                 $create[$key]['thread_key'] = $log['meta']['thread_key'];<br>                                 $create[$key]['user_id'] = $log['user_id'];<br>                                 $create[$key]['date'] = $log['date'];<br>                             }<br>                             break;<br>                         case 'approve':<br>                             //这条评论是通过的评论<br>                             foreach ($log['meta'] as $key => $value) {<br>                                 $approve[$key]['post_id'] .= $value.",";<br>                             }<br>                             break;<br>                         case 'spam':<br>                             //这条评论是标记垃圾的评论<br>                             foreach ($log['meta'] as $key => $value) {<br>                                 $spam[$key]['post_id'] .= $value.",";<br>                             }<br>                             break;<br>                         case 'delete':<br>                             //这条评论是删除的评论<br>                             foreach ($log['meta'] as $key => $value) {<br>                                 $delete[$key]['post_id'] .= $value.",";<br>                             }<br>                             break;<br>                         case 'delete-forever':<br>                             //彻底删除的评论<br>                             foreach ($log['meta'] as $key => $value) {<br>                                 $delete_forever[$key]['post_id'] .= $value.",";<br>                             }<br>                             break;<br>                         default:<br>                             break;<br>                     }<br>                     // 更新处理数据<br>                     switch($log['action']){<br>                         case 'create':<br>                             foreach ($create as $key => $value) {<br>                                 if($value != ""){<br>                                     $Comment->add($value);<br>                                 }<br>                             break;<br>                             }<br>                         case 'approve':<br>                             //这条评论是通过的评论<br>                             foreach ($approve as $key => $value) {<br>                                 if($value != ""){<br>                                     $Comment->where(array('post_id'=>array('in',''. substr($value['post_id'], 0,-1) .'')))->setField('status','approved');<br>                                 }<br>                             }<br>                             break;<br>                         case 'spam':<br>                             //这条评论是标记垃圾的评论<br>                             foreach ($spam as $key => $value) {<br>                                 if($value != ""){<br>                                     $Comment->where(array('post_id'=>array('in',''. substr($value['post_id'], 0,-1) .'')))->setField('status','spam');<br>                                 }<br>                             }<br>                             break;<br>                         case 'delete':<br>                             //这条评论是删除的评论<br>                             foreach ($delete as $key => $value) {<br>                                 if($value != ""){<br>                                     $Comment->where(array('post_id'=>array('in',''. substr($value['post_id'], 0,-1) .'')))->setField('status','delete');<br>                                 }<br>                             }<br>                             break;<br>                         case 'delete-forever':<br>                             //彻底删除的评论<br>                             foreach ($delete_forever as $key => $value) {<br>                                 if($value != ""){<br>                                     $Comment->where(array('post_id'=>array('in',''. substr($value['post_id'], 0,-1) .'')))->delete();<br>                                 }<br>                             }<br>                             break;<br>                         default:<br>                             break;<br>                     }<br>                     //更新last_log_id,记得维护last_log_id。(如update你的数据库)<br>                     if (strlen($log['log_id']) > strlen($last_log_id) || strcmp($log['log_id'], $last_log_id) > 0) {<br>                         M('setting')->where(array('name'=>"last_log_id"))->setField('value',$log['log_id']);<br>                     }<br>                 }<br>             }<br> <br>         }<br> <br>     }<br> }/**<br>  *<br>  * 多说检查签名<br>  *<br>  */<br> function check_signature($input){<br> <br>     $signature = $input['signature'];<br>     unset($input['signature']);<br> <br>     ksort($input);<br>     $baseString = http_build_query($input, null, '&');<br>     $expectSignature = base64_encode(hmacsha1($baseString, C(DUOSHUO_SECRET)));<br>     if ($signature !== $expectSignature) {<br>         return false;<br>     }<br>     return true;<br> }<br> <br> <br> // from: http://www.php.net/manual/en/function.sha1.php#39492<br> // Calculate HMAC-SHA1 according to RFC2104<br> // http://www.ietf.org/rfc/rfc2104.txt<br> // 多说<br> function hmacsha1($data, $key) {<br>     if (function_exists('hash_hmac'))<br>         return hash_hmac('sha1', $data, $key, true);<br> <br>     $blocksize=64;<br>     if (strlen($key)>$blocksize)<br>         $key=pack('H*', sha1($key));<br>     $key=str_pad($key,$blocksize,chr(0x00));<br>     $ipad=str_repeat(chr(0x36),$blocksize);<br>     $opad=str_repeat(chr(0x5c),$blocksize);<br>     $hmac = pack(<br>             'H*',sha1(<br>                     ($key^$opad).pack(<br>                             'H*',sha1(<br>                                     ($key^$ipad).$data<br>                             )<br>                     )<br>             )<br>     );<br>     return $hmac;<br> }配置

//多说
'DUOSHUO_SECRET'=>'78bd15a3d4fb3000657741a1319bbbbe',
'DUOSHUO_SHORT_NAME'=>'muxu',

参考 http://dev.duoshuo.com/threads/50037b11b66af78d0c000009


网站/微信/App/技术交流:QQ群 74976648

AD:真正免费,域名+虚机+企业邮箱=0元

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.