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