getAccessToken(); $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit'; $data = array_merge([ 'width' => 280 ], $param); $result = HttpClient::request('post', $url, [ 'body' => json_encode($data, JSON_UNESCAPED_UNICODE), 'query' => ["access_token" => $access_token['access_token']], 'headers' => ['Content-Type' => 'application/json'] ]); $result = $result->getBody()->getContents(); // Log::write('getWxCodeUnlimited:'.$result); // 尝试解析为JSON,如果解析成功说明是错误信息,如果失败说明是图片buffer $jsonResult = json_decode($result, true); if (json_last_error() === JSON_ERROR_NONE) { // JSON解析成功,返回错误信息数组 return $jsonResult; } else { // JSON解析失败,说明是图片二进制数据,直接返回 return $result; } } /** * 获取手机号 * @param string $code * @return array */ public function getWechatMobile($code) { $access_token = $this->getAccessToken(); $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber"; $res = HttpClient::request('post', $url, [ 'body' => json_encode(['code' => $code]), 'query' => ["access_token" => $access_token['access_token']], 'headers' => ['Content-Type' => 'application/json'] ]); $res = $res->getBody()->getContents(); $res = (array)json_decode($res, true); if (!isset($res['phone_info']) && Config::get('app_debug')) { Log::write($res, 'get_phone'); } return $res['phone_info'] ?? []; } /** * 获取Session信息 * @param string $code * @return array */ public function getWechatSession($code) { // $access_token = $this->getAccessToken(); // $url = "https://api.weixin.qq.com/sns/jscode2session"; // $result = HttpClient::request('get', $url, [ // 'query' => [ // "access_token" => $access_token['access_token'], // 'appid' => $this->app->app_id, // 'secret' => $this->app->app_secret, // 'js_code' => $code, // 'grant_type' => 'authorization_code' // ], // ]); $result = $this->app->auth->session($code); Log::write('getWechatSession:'.$result); //$result = $result->getBody()->getContents(); $result = json_decode($result, true); if ($result['errcode'] == 0) { return $result; } if (Config::get('app_debug')) { Log::write($result, 'get_session'); } return ['errmsg' => Config::get('app_debug') ? $result['errmsg'] : '网络错误']; } }