PHP實現多說評論即時同步回網站資料庫

來源:互聯網
上載者:User

PHP實現多說評論即時同步回網站資料庫

社會化評論-多說評論即時同步回資料庫,依賴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實現多說評論即時同步回網站資料庫的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.