hash_hmac 簽名


<?php /** * =========================================================== * Model_Base * Description * Model_Base * @Author wzhu.email@gmail.com * @Version 1.0 * @Copyright Zhuweiwei * Copyright © 2008-2012 * China. All Rights Reserved. * =========================================================== */ namespace Action; use ZW\Utility as Utility; use ZW\Http\Client as HttpClient; class Expo { CONST URL_BASE = 'https://api.webexpotec.cn'; CONST URL_EVENT = '/v1/event'; //登錄
    CONST URL_AUTH_SIGNIN = '/v1/auth/signin'; //獲取驗證碼和驗證驗證碼
    CONST URL_AUTH_VCODE = '/v1/auth/vcode'; //下訂單
    CONST URL_ORDER = '/v1/order'; //生成憑證
    CONST URL_ADMISSION = '/v1/admission'; //查詢用戶是否已經有憑證
    CONST URL_ADMISSION_LISTING = '/v1/admission/listing'; //二維碼
    CONST URL_QR = '/v1/qr'; //問卷調查
    CONST URL_QURESTION = '/v1/question/listing'; //提交問題
    CONST URL_SURVEY = '/v1/survey/submit'; //lifecbd 接口路徑
    CONST URL_ARTICLE           = '/v1/article'; /* protected $app_key = '1467721313'; protected $app_secret = '21e122dba62dcd9aed1488aced61f2c2'; protected $eventid = '1440860341';// protected $ticketid = '1463800736';//票務ID*/

    protected $app_key    = '1467721309'; protected $app_secret = '567aa793d2cfb865eec8623dc7b9de92'; protected $eventid    = '1440860340';//活動id
    protected $ticketid   = '000000';//票務ID--隨便寫


    protected $app_ver = '1.0.0'; protected $app_uuid = 'ABCDEFG'; protected $sig_method = 'HMAC-SHA1'; protected $il8n_lang = 'zh-cn'; public function decodeQrcode($string) { $arrParam = array( 'string' => $string, ); return $this->get(self::URL_QR, $arrParam); } public function getSignin($arrParam = array()) { $arrParam = array( 'username'    => '13436919765',
            'password'    => '919765',
            'countrycode' => 'CN', ); return $this->get(self::URL_AUTH_SIGNIN, $arrParam); } //登錄
    public function getEventListing(array $arrParam = array(), $start = 0, $num = 100, $scope = 0) { $arrParam['start'] = (int)$start; $arrParam['num']   = (int)$num; $arrParam['scope'] = $scope; $url               = sprintf('%s/listing', self::URL_EVENT); return $this->get($url, $arrParam); } //獲取活動內容
    public function getEvent($eventId, $scopes = 1) { $url      = sprintf('%s/%d', self::URL_EVENT, $eventId); $arrParam = array( 'id'     => (int)$eventId,
            'scopes' => (int)$scopes, ); return $this->get($url, $arrParam); } //獲取驗證碼
    public function getAuthVcode($username, $type = 4, $countryCode = 'CN') { $arrParam = array( 'username'    => $username,
            'countrycode' => $countryCode,
            'type'        => $type, ); return $this->get(self::URL_AUTH_VCODE, $arrParam); } /** *讀取列表數據 */
    public function getArticleList($channel, $start = 0, $num = 20, $type = 0){ $arrParam = array( 'channel'    => $channel,
            'type'       => $type,
            'start'      => $start,
            'num'        => $num, ); $url = sprintf('%s/listing', self::URL_ARTICLE); return $this->get($url, $arrParam); } /**讀取文章數據 * @param $articleId 文章id */
    public function getArticle($articleId){ $url = sprintf('%s/%d', self::URL_ARTICLE, $articleId); return $this->get($url, array()); } //校驗驗證碼
    public function chkAuthVcode($username, $vcode = '', $type = 0, $countryCode = 'CN') { if (empty($vcode)) { $vcode = "abcdfg"; } $arrParam = array( 'username'    => $username,
            'countrycode' => $countryCode,
            'type'        => $type,
            'vcode'       => $vcode, ); return $this->post(self::URL_AUTH_VCODE, $arrParam); } //提交注冊表單
    public function orderForm($arr) { $goods    = [ 'id'  => $arr['id'],
            'num' => $arr['num'], ]; $goods    = '[' . json_encode($goods) . ']'; $arrParam = array( 'type'        => $arr['type'],
            'goods'       => $goods,
            'realname'    => $arr['realname'],
            'cellphone'   => $arr['cellphone'],
            'email'       => $arr['email'],
            'corp'        => $arr['corp'],
            'title'       => $arr['title'],
            'address'     => $arr['address'],
            'countrycode' => $arr['countrycode'],
            'adminmemo'   => $arr['adminmemo'],
            'promocode'   => $arr['promocode'], ); return $this->post(self::URL_ORDER, $arrParam); } //檢查訂單狀態
    public function chkOrder($orderid) { $arrParam = array( 'orderid' => $orderid, ); return $this->get(self::URL_ORDER . "/" . $orderid, $arrParam); } //注冊憑證
    public function registerAdmission($ticketnoid, $arr) { $arrParam = array( 'ticketno'    => $ticketnoid,
            'realname'    => $arr['realname'],
            'cellphone'   => $arr['cellphone'],
            'email'       => $arr['email'],
            'corp'        => $arr['corp'],
            'title'       => $arr['title'],
            'countrycode' => 'CN',
            'extdata'     => $arr['adminmemo'], ); return $this->post(self::URL_ADMISSION, $arrParam); } public function getUserAdmissionList($arrParam) { $arrParam['eventid']  = $this->eventid; $arrParam['ticketid'] = $this->ticketid; return $this->get(self::URL_ADMISSION_LISTING, $arrParam); } //獲取問題列表
    public function getQuestionList($token, $suveryid) { $arrParam = array('surveyid' => (int)$suveryid,
                          'token'    => $token,); return $this->get(self::URL_QURESTION, $arrParam); } //提交問題列表
    public function submitSuvery($id, $answers) { return $this->post(self::URL_SURVEY . '/' . $id, $answers); } ////////////////////////////////////////////////////////////////////////// //stdClass Object轉array 
    public function objectArray($array) { if (is_object($array)) { $array = (array)$array; } else if (is_array($array)) { foreach ($array as $key => $value) { $array[$key] = $this->objectArray($value); } } return $array; } //get組裝curl字符串
    protected function urlGetstr($url, $arrParam) { $url = $url . "?" . $this->arrayTostr($arrParam); return $url; } //curl調用接口
    protected function httpCurlExce($url, $arrParam = array(), $method) { //如果是get提交需要組裝url,post的則不需要
        if ($method == 'get') { $url = $this->urlGetstr($url, $arrParam); } //echo $url; //curl發送
        $curl = curl_init(); //設置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url); //設置頭文件的信息作為數據流輸出
        curl_setopt($curl, CURLOPT_HEADER, 1); //設置獲取的信息以文件流的形式返回,而不是直接輸出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //繞過ssl驗證 //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //在嘗試連接時等待的秒數
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 90); //最大執行時間
        curl_setopt($curl, CURLOPT_TIMEOUT, 90); if ($method == 'post') { //設置post方式提交
            curl_setopt($curl, CURLOPT_POST, 1); //設置post數據
            curl_setopt($curl, CURLOPT_POSTFIELDS, $arrParam); } //執行命令
        $data = curl_exec($curl); //處理頭信息 // 獲得響應結果里的:頭大小
        $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); // 根據頭大小去獲取頭信息內容
        $ret = substr($data, $headerSize); //關閉URL請求
        curl_close($curl); //stdClass Object轉array
        $ret = json_decode($ret); $ret = $this->objectArray($ret); //echo "<pre>"; //print_r($data);
        return $ret; } //簽名算法
    protected function getSignature($str, $key) { $signature = ""; if (function_exists('hash_hmac')) { //$signature = bin2hex(hash_hmac("sha1", $str, $key, true));
            $signature = hash_hmac("sha1", $str, $key, true); } else { $blocksize = 64; $hashfunc  = 'sha1'; if (strlen($key) > $blocksize) { $key = pack('H*', $hashfunc($key)); } $key  = str_pad($key, $blocksize, chr(0x00)); $ipad = str_repeat(chr(0x36), $blocksize); $opad = str_repeat(chr(0x5c), $blocksize); $hmac = pack( 'H*', $hashfunc( ($key ^ $opad) . pack( 'H*', $hashfunc( ($key ^ $ipad) . $str ) ) ) ); //$signature = bin2hex($hmac);
            $signature = $hmac; } return $signature; } //base64UrlEncode
    protected function base64UrlEncode($str) { $search  = array('+', '/', '='); $replace = array('*', '-', ''); $basestr = base64_encode($str); return str_replace($search, $replace, $basestr); } /** * 將數組中的值,按照鍵值排序,之后將每個元素拼接成url樣式 * @param $arrParam:數組 * @return $str : 拼接的url地址 */
    protected function arrayTostr($arrParam) { $i   = $arr_num = 0; $str = ""; ksort($arrParam, SORT_NATURAL); $arr_num = count($arrParam); foreach ($arrParam as $key => $val) { $i++; if ($i < $arr_num) { $str .= $key . '=' . urlencode($val) . '&'; } else { $str .= $key . '=' . urlencode($val); } } return $str; } protected function buildSignature($arrParam) { $str       = $this->arrayTostr($arrParam);//url地址拼接
        $signature = $this->getSignature($str, $this->app_secret);//hash_hmac 簽名
        $signature = $this->base64UrlEncode($signature);//base64加密
        return $signature; } protected function getRandStr() { $str = ''; for ($i = 1; $i <= 10; $i++) { $str .= chr(rand(65, 90)); } return $str; } protected function adaptSysParam(array $arrParam) { $arrParam['app_key']    = $this->app_key; $arrParam['timestamp']  = NOW_TIME; $arrParam['sig_method'] = $this->sig_method; $arrParam['app_uuid']   = $this->app_uuid; $arrParam['app_ver']    = '1.0.0'; $arrParam['il8n_lang']  = $this->il8n_lang; $arrParam['req_uuid']   = $this->getRandStr(); $arrParam['signature']  = $this->buildSignature($arrParam); return $arrParam; } /** * @param :$url url地址 * @param :adaptSysParam 組合需要的傳參,最后生成簽名:簽名方法:將數組組成url參數,生成hmac簽名,base64加密 * @param :httpCurlExce curl獲取接口內容 */
    protected function get($url, array $arrParam) { $arrParam = $this->adaptSysParam($arrParam); $arr      = $this->httpCurlExce(self::URL_BASE . $url, $arrParam, 'get'); return $arr; } protected function post($url, array $arrParam) { $arrParam = $this->adaptSysParam($arrParam); $arr      = $this->httpCurlExce(self::URL_BASE . $url, $arrParam, 'post'); return $arr; } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM