小程序
小游戏
企业微信
微信支付
扫描小程序码分享
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/phonenumber/phonenumber.getPhoneNumber.html
请求手机号码时报:{"errcode":47001,"errmsg":"data format error hint: [WhoCWV0sf-St5rSA] rid: 62bc035c-2666c259-76d33c29"}
3 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
PHP完整代码(2023年6月7日 11:32:37):
<?php $dug = true; //微信小程序令牌 $dynamicToken = (isset($_GET['dynamicToken']) && !empty($_GET['dynamicToken']))? $_GET['dynamicToken']:""; if($dug) { echo "小程序动态令牌(dynamicToken): ".$dynamicToken."<br>"; } $appid = "111"; $secret = "222"; $access_token = getAccessToken($appid, $secret); if($access_token !== false){ //获取成功,加上小程序传来的临时令牌, 再去请求详情 //$dynamicToken $access_token -> getuserphonenumber $data = array( //$access_token 必须放到url上, post参数仅有code一个 //'access_token' => $access_token, 'code' => $dynamicToken ); getUserPhoneNumber("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=".$access_token,$data); } function getAccessToken($appid,$secret){ $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $data = curl_exec($ch); if(curl_errno($ch)){ echo '<br>Curl error: ' . curl_error($ch)."<br>"; return curl_error($ch); } curl_close($ch); $result = json_decode($data,true); var_dump($result); echo '<br>'; if(isset($result["access_token"])){ return $result["access_token"]; }else{ echo "<br>getAccessToken Failed: ".$result["errcode"].";".$result["errmsg"]."<br>"; return false; } } function getUserPhoneNumber($remote_server, $data) { $json_data = json_encode($data); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $remote_server); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $data = curl_exec($ch); if(curl_errno($ch)){ echo '<br>Curl error: ' . curl_error($ch)."<br>"; return curl_error($ch); } curl_close($ch); var_dump($data); //$result = json_decode($data,true); //var_dump($result); return $data; } ?>
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
已解决
//JAVA HttpUtil类中
public static String doPostJson(String url, String json) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type","application/json;charset=UTF-8");
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
response.close();
} catch (IOException e) {
}
•
return resultString;
//WechatUtil类中
public static JSONObject getPhoneNumber(String code){
JSONObject accessMessage=WechatUtil.getAccessToken();
String accessToken=accessMessage.getString("access_token");
System.out.println(accessToken);
System.out.println(code);
String requestUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+accessToken;
JSONObject obj = new JSONObject();
//小程序发来的code
obj.put("code", code);
//发送Post请求读取调用微信接口获取用户手机号
JSONObject jsonObject = JSONObject.fromObject(HttpClientUtil.doPostJson(requestUrl, obj.toString()));
return jsonObject;
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
PHP完整代码(2023年6月7日 11:32:37):
<?php $dug = true; //微信小程序令牌 $dynamicToken = (isset($_GET['dynamicToken']) && !empty($_GET['dynamicToken']))? $_GET['dynamicToken']:""; if($dug) { echo "小程序动态令牌(dynamicToken): ".$dynamicToken."<br>"; } $appid = "111"; $secret = "222"; $access_token = getAccessToken($appid, $secret); if($access_token !== false){ //获取成功,加上小程序传来的临时令牌, 再去请求详情 //$dynamicToken $access_token -> getuserphonenumber $data = array( //$access_token 必须放到url上, post参数仅有code一个 //'access_token' => $access_token, 'code' => $dynamicToken ); getUserPhoneNumber("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=".$access_token,$data); } function getAccessToken($appid,$secret){ $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $data = curl_exec($ch); if(curl_errno($ch)){ echo '<br>Curl error: ' . curl_error($ch)."<br>"; return curl_error($ch); } curl_close($ch); $result = json_decode($data,true); var_dump($result); echo '<br>'; if(isset($result["access_token"])){ return $result["access_token"]; }else{ echo "<br>getAccessToken Failed: ".$result["errcode"].";".$result["errmsg"]."<br>"; return false; } } function getUserPhoneNumber($remote_server, $data) { $json_data = json_encode($data); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $remote_server); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $data = curl_exec($ch); if(curl_errno($ch)){ echo '<br>Curl error: ' . curl_error($ch)."<br>"; return curl_error($ch); } curl_close($ch); var_dump($data); //$result = json_decode($data,true); //var_dump($result); return $data; } ?>
已解决
//JAVA HttpUtil类中
public static String doPostJson(String url, String json) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type","application/json;charset=UTF-8");
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
•
return resultString;
}
•
//WechatUtil类中
public static JSONObject getPhoneNumber(String code){
JSONObject accessMessage=WechatUtil.getAccessToken();
String accessToken=accessMessage.getString("access_token");
System.out.println(accessToken);
System.out.println(code);
String requestUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+accessToken;
JSONObject obj = new JSONObject();
//小程序发来的code
obj.put("code", code);
//发送Post请求读取调用微信接口获取用户手机号
JSONObject jsonObject = JSONObject.fromObject(HttpClientUtil.doPostJson(requestUrl, obj.toString()));
return jsonObject;
}