/** * Request access_token * @param string $code * @return mixed(string|array|boolean) */ public function request_access_token($code) { $requrl = ''; $params = array(); if ('weixin' == $this->_platform) { $requrl = 'https://api.weixin.qq.com/sns/oauth2/access_token'; $params['appid'] = $this->client_id; $params['secret'] = $this->secret_key; $params['code'] = $code; $params['grant_type'] = 'authorization_code'; $req = new ApiRequest(['method' => 'get', 'protocol' => 'https', 'timeout' => 60, 'timeout_connect' => 30]); return $req->setUrl($requrl)->setParams($params)->send()->recv(TRUE); } return ''; }
/** * 微信接口通用调用函数 * * @param string $uri_path URI path * @param array $params 对应请求参数 * @param string $method http请求方法:GET,POST * @param string $type API地址类型,对应self::$apiUrlPrefix的key部分:api_cgi,api_sns,open_conn * @param string $outfile 下载地址 * @return mixed JSON Array or string */ public function apiCall($uri_path, $params = array(), $method = 'get', $type = 'api_cgi', $outfile = '') { if (!in_array($type, array_keys(self::$apiUrlPrefix))) { return false; } $method = strtolower($method); if (!in_array($method, array('get', 'post'))) { return false; } if (empty($uri_path)) { return false; } if ($method == 'post' && is_array($params)) { $params = json_encode($params, JSON_UNESCAPED_UNICODE); } $requrl = self::$apiUrlPrefix[$type] . $uri_path; $apiConfig = ['method' => $method, 'protocol' => 'https', 'timeout' => 60, 'timeout_connect' => 30]; if ('' !== $outfile) { $apiConfig['outfile'] = $outfile; } $req = new ApiRequest($apiConfig); return $req->setUrl($requrl)->setParams($params)->send()->recv(TRUE); }