Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn more. Comments are synchronized back to the database in real time, relying on the Http library. Just find one.
/**
* More real-time synchronous comments back to local database API
*/
Namespace Home \ Controller;
Use Think \ Controller;
Class DuoshuoController extends Controller {
Public function duoshuo (){
If (check_signature ($ _ POST )){
$ Comment = M ('comment ');
$ Where ['name'] = array ('in', 'last _ log_id ');
$ Last_log_id = M ('setting')-> 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']) {
// Handle error, error message $ response ['message'], $ response ['code']
File_log ("signature error ");
} Else {
// Traverses the returned response. You can determine the method of processing the comment based on the 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 ':
// This comment is approved
Foreach ($ log ['meta'] as $ key => $ value ){
$ Approve [$ key] ['Post _ id']. = $ value .",";
}
Break;
Case 'spam ':
// This comment is a comment marked as spam
Foreach ($ log ['meta'] as $ key => $ value ){
$ Spam [$ key] ['Post _ id']. = $ value .",";
}
Break;
Case 'delete ':
// This comment is deleted
Foreach ($ log ['meta'] as $ key => $ value ){
$ Delete [$ key] ['Post _ id']. = $ value .",";
}
Break;
Case 'delete-forever ':
// Delete comments permanently
Foreach ($ log ['meta'] as $ key => $ value ){
$ Delete_forever [$ key] ['Post _ id']. = $ value .",";
}
Break;
Default:
Break;
}
// Update the processed data
Switch ($ log ['action']) {
Case 'create ':
Foreach ($ create as $ key => $ value ){
If ($ value! = ""){
$ Comment-> add ($ value );
}
Break;
}
Case 'approve ':
// This comment is approved
Foreach ($ approve as $ key => $ value ){
If ($ value! = ""){
$ Comment-> where (array ('Post _ id' => array ('in ',''. substr ($ value ['Post _ id'], 0,-1 ). '')-> setField ('status', 'apache ');
}
}
Break;
Case 'spam ':
// This comment is a comment marked as 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 ':
// This comment is deleted
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 ':
// Delete comments permanently
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;
}
// Update last_log_id. Remember to maintain last_log_id. (For example, update your database)
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']);
}
}
}
}
}
}
/**
*
* For more information, check the signature.
*
*/
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
// More
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;
}
Configuration
// More
'Duoshuo _ secret' => '78bd15a3d4fb3000657741a1319bbbbe ',
'Duoshuo _ SHORT_NAME '=> 'muxu ',
Reference http://dev.duoshuo.com/threads/50037b11b66af78d0c000009
Website // App/technical exchange: QQ Group 74976648
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB