gateway = $gateway; $this->appkey = $appkey; $this->app_secret = $app_secret; $this->access_token = $access_token; } function getMillisecond() { list($t1, $t2) = explode(' ', microtime()); return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000); } private function sign($appkey, $timestamp, $nonce, $access_token, $api, $secret, $biz_params) { $unsignRequestParams = array( 'appkey' => $appkey, 'timestamp' => $timestamp, 'nonce' => $nonce, 'api' => $api ); if ($access_token != NULL) { $unsignRequestParams["access_token"] = $access_token; } ksort($unsignRequestParams); $unsignStr = ''; foreach ($unsignRequestParams as $key => $value) { $unsignStr = $unsignStr . $key . '=' . $value . '&'; } $unsignStr = $unsignStr . 'body=' . $biz_params; $unsignStr = $unsignStr . '&secret=' . $secret; return sha1($unsignStr); } function request($api, $apiParam) { $bodyStr = $apiParam == NULL ? "" : json_encode($apiParam); $timestamp = $this->getMillisecond(); $nonce = mt_rand(0, 1000000); $sign = $this->sign($this->appkey, $timestamp, $nonce, $this->access_token, $api, $this->app_secret, $bodyStr); $url = $this->gateway . '?appkey=' . $this->appkey . '×tamp=' . $timestamp . '&nonce=' . $nonce . '&access_token=' . $this->access_token . '&api=' . $api . '&sign=' . $sign; $options = array( 'http' => array( 'header' => "Content-type: application/json; charset=utf-8", 'method' => 'POST', 'content' => $bodyStr ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); if ($result === FALSE) { throw new Exception("请求gateway异常"); } return json_decode($result, true); } } ?>