Rumah > php教程 > php手册 > 多说评论实时同步回数据库

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

WBOY
Lepaskan: 2016-06-07 11:34:59
asal
1520 orang telah melayarinya

多说评论实时同步回数据库,依赖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元

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan