Esempio n. 1
0
/**
 * 构造返回http请求头部
 *
 * @param array  $http_body     HTTP包体。可能为NULL,比如GET请求时
 * @param string $http_url      请求的url
 * @param string $http_method   http method,比如 'GET', 'POST' 或 'PUT'
 * @param bool   $trade         是否交易请求操作,下单、改单、撤单操作需要为true,其他为false
 * @param string $lang          语言,sc:简体,tc:繁体, en:英文
 *
 * @return array http请求头部
 */
function make_header($http_body, $http_url, $http_method, $trade, $lang = 'sc')
{
    $http_header = array('Accept' => 'application/vnd.futu5.openapi-v1+json', 'Content-Type' => 'application/vnd.futu5.openapi-v1+json', 'X-Futu-Oauth-Appid' => Config::$appid, 'X-Futu-Oauth-App-Account' => Config::$app_account, 'X-Futu-Oauth-Nonce' => rand(1, 100000), 'X-Futu-Oauth-Accesstoken' => Config::$accesstoken, 'X-Futu-Oauth-Signature-Method' => 'HMAC-SHA1', 'X-Futu-Oauth-Timestamp' => time(), 'X-Futu-Oauth-Version' => '1.0', 'X-Futu-Oauth-Signature' => '', 'X-Futu-Oauth-Lang' => $lang);
    if ($trade) {
        $http_header['X-Futu-Oauth-Tradetoken'] = Config::$tradetoken;
    }
    // ----------------------数字签名---------------------- //
    // 需要参与签名的头字段
    $sig_header = array('X-Futu-Oauth-Appid' => $http_header['X-Futu-Oauth-Appid'], 'X-Futu-Oauth-App-Account' => $http_header['X-Futu-Oauth-App-Account'], 'X-Futu-Oauth-Nonce' => $http_header['X-Futu-Oauth-Nonce'], 'X-Futu-Oauth-Lang' => $http_header['X-Futu-Oauth-Lang'], 'X-Futu-Oauth-Accesstoken' => $http_header['X-Futu-Oauth-Accesstoken'], 'X-Futu-Oauth-Signature-Method' => $http_header['X-Futu-Oauth-Signature-Method'], 'X-Futu-Oauth-Timestamp' => $http_header['X-Futu-Oauth-Timestamp'], 'X-Futu-Oauth-Version' => $http_header['X-Futu-Oauth-Version']);
    if ($trade) {
        $sig_header['X-Futu-Oauth-Tradetoken'] = $http_header['X-Futu-Oauth-Tradetoken'];
    }
    $http_header['X-Futu-Oauth-Signature'] = gen_signature($sig_header, $http_body, $http_url, $http_method, Config::$app_secret, Config::$accesstoken);
    return $http_header;
}
Esempio n. 2
0
function wh_handler()
{
    set_time_limit(0);
    $items = queue_out('webhook');
    if (empty($items)) {
        return;
    }
    $hooks_data = array();
    foreach ($items as $item) {
        $hooks = $item['hooks'];
        $domain = $item['domain'];
        $db_name = $item['db'];
        $table_name = $item['table'];
        $event = $item['event'];
        foreach ($hooks as $hook_url) {
            //顶层key:webhook的url
            if (!isset($hooks_data[$hook_url])) {
                $hooks_data[$hook_url] = array();
            }
            $send_obj =& $hooks_data[$hook_url];
            //第一层key:域名如db.appgame.com
            if (!isset($send_obj[$domain])) {
                $send_obj[$domain] = array();
            }
            $domain_obj =& $send_obj[$domain];
            //第二层key:数据库名称如default
            if (!isset($domain_obj[$db_name])) {
                $domain_obj[$db_name] = array();
            }
            $db_obj =& $domain_obj[$db_name];
            //第三层key:数据表名称如default
            if (!isset($db_obj[$table_name])) {
                $db_obj[$table_name] = array();
            }
            $table_obj =& $db_obj[$table_name];
            //第四层key:事件名称如update、create
            if (!isset($table_obj[$event])) {
                $table_obj[$event] = array();
            }
            $datas =& $table_obj[$event];
            //第五层就是id数组了
            if (empty($item['items'])) {
                if (!in_array(0, $datas)) {
                    $datas[] = 0;
                }
            } else {
                foreach ($item['items'] as $itme_id) {
                    if (!in_array($item_id, $datas)) {
                        $datas[] = $itme_id;
                    }
                }
            }
        }
    }
    list($timestamp, $nonce, $signature) = gen_signature('ECrLAewF9nZ4qwdm');
    foreach ($hooks_data as $hook_url => $data) {
        $get_params = http_build_query(array('source' => 'gamedb', 'timestamp' => $timestamp, 'nonce' => $nonce, 'signature' => $signature));
        $ori_query = parse_url($hook_url, PHP_URL_QUERY);
        if (empty($ori_query)) {
            $hook_url = $hook_url . '?' . $get_params;
        } else {
            $hook_url = $hook_url . '&' . $get_params;
        }
        curl_post_content($hook_url, $data);
    }
}