/** * 请求Api * @param strign $http_method , post or get * @param string $url , '/orgcode/demo/site/index' * @param array $params ,['id'=>1,'type'=>'test' ,...] ,也可以不传,如果参数在url参数上,这里必须传空 * @param array|string $data post提交数据的时候必须传 * @return string * @throws \Exception */ private function _callApi($http_method, $url, $params = [], $data = []) { // $cache_result = $this->_getFromCache($url,$params); // if($cache_result){ // return $cache_result; // } $curl = new Curl(); if ($this->curl_options) { //设置超时请求 foreach ($this->curl_options as $key => $val) { $curl->setOption($key, $val); //设置curl options } } //将签名参数加入到GET请求里,最终生成一个参数值,从而简化了请求 $params[$this->sign_key] = $this->_getSign($this->appid, $this->appsecret, time()); switch (strtoupper($http_method)) { case 'GET': $result = $curl->get($url, $params, $this->debug); break; case 'POST': $result = $curl->post($url, $data, $params, $this->debug); break; default: throw new \Exception('不支持的HTTP请求方法'); } if ($curl->getError()) { return \mysoft\helpers\String::jsonEncode(['success' => 0, 'data' => $curl->getError()]); } return $result; }
private function getAccessToken() { $curl = new Curl(); $url = $this->_buildUrl('api/sys/GetAccessToken.ashx', $this->getApiPlartformSite()); $param = ['timestamp' => date('Y-m-d\\TH:i:s'), 'appid' => $this->_appid]; $token = $curl->get($url, $param); //发现http请求错误,抛出异常 if ($curl->getError()) { throw new ErpApiTokenException($curl->getError()); } $token = json_decode($token, true); if (!isset($token['errmsg']) && !isset($token['access_token'])) { throw new ErpApiTokenException('令牌服务器异常'); } return $token; }