前言情景剧
业务场景是为了在用户登录的时候判断其是否已经成功注册,没有成功注册的话就将获取到的openid和session_key加密后作为token传给前端,然后让前台通过组件获得code之后连着token一起传给后端,后端拿着code再去请求微信接口获取到用户的电话号码,以此完成注册。
实现过程中的问题
结合微信官方手册:phonenumber.getPhoneNumber | 微信开放文档 (qq.com)
怪我没好好看手册,中间发生了hin多的插曲。比如报错返回:
require POST method hint errcode: 43002
一查文档告诉我:这个请求需要用post请求!可是,我明明是用的post请求啊~~~
后面通过面向百度编程,在找了5678个公共发起post请求的方法之后,终于有一个post请求没问题,但又遇到了一个问题,他返回: [0,null]
这里的原因是比较让我耗费时间的:这个接口的请求,必须在用户处于登录的条件下,并且必须在互联网能够访问到的公共网站上(也就是得在我的项目配置好的域名下去请求,才能够返回值!)我在本地试了好久,气煞我也!
如果你后面写好了对返回值的判断的话会报错:
Trying to access array offset on value of type null 。
就是告诉你不能尝试将 null,bool,int,float 或 resource 类型的值用作数组 ( 例如 $null[“key”] ) 会产生一个通知。
遇到的这个问题我是万万妹想到,搞了整整一下午,最后在公司大佬的帮助下半个小时帮我解决了问题。ps:第一个参数access_token那是轻轻松松(有问题可以看看和我的代码哪里不同)
废话不多say,上代码!
common.php中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | /** * 发送curl get * @param string $url * @return mixed */ function curl_get($url) { $oCurl = curl_init(); curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1 } if (defined( 'CURLOPT_IPRESOLVE' ) && defined( 'CURL_IPRESOLVE_V4' )) { curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); curl_close($oCurl); if (intval($aStatus[ "http_code" ]) == 200) { return $sContent; } else { return false ; } } if (!function_exists( 'http_post_json' )){ //这一行是判断公共方法有无这个方法,避免重名~ /** * PHP发送Json对象数据 * @param $url string * @param $jsonStr string * @param string[] $headers * @return array */ function http_post_json(string $url, string $jsonStr, array $headers = array( 'Content-Type: application/json; charset=utf-8' , )): array { $headers[] = 'Content-Length: ' . strlen($jsonStr); $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return array($httpCode, $response); } } |
调用接口代码:(有空可以自己封装一下~)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /**必须先进入登录状态,然后拿到phone的code去请求然后拿到access_code,请求phone的接口 */ $appid = getConfig( 'appid_y' ); //填写自己的appid,小程序中看 $secret = getConfig( 'secret_y' ); //填自己的secret,公众平台看 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret" ; $access_token = json_decode(curl_get($url), true ); if (isset($access_token[ 'errcode' ])) return [ 'errcode' =>$access_token[ 'errcode' ], 'msg' => '请求失败' , 'data' =>$access_token]; $access_token = $access_token[ 'access_token' ]; //获取到了access_token //请求电话号使用方法只能在公网能访问的目录下进行,本地进行没有返回值 $json_code = json_encode([ 'code' =>$param[ 'code' ]]); $headers = [ 'Accept: application/json' , 'User-Agent: */*' , 'Content-Type: application/json; charset=utf-8' , ]; $phone = http_post_json($url,$json_code,$headers); $phone[1] = json_decode($phone[1], true ); if (empty($phone[1])||$phone[1][ 'errcode' ]!=0) throw new Exception( '系统获取手机号失败' ); $phoneNumber = $phone[1][ 'phone_info' ][ 'phoneNumber' ]; /**拿到电话号码end */ |
另外,thinkphp5获取微信授权用户手机号的相关实现方法,可参考前面一篇:https://www.2it.club/article/229956.htm