收藏
回答

获取手机号码报data format error hint: [WhoCWV0sf-St5rSA]

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 个回答

  • 锦之年华
    锦之年华
    2023-06-07

    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;
    }
    ?>
    
    2023-06-07
    有用 1
    回复
  • 雷博斯特
    雷博斯特
    2022-06-29

    已解决

    2022-06-29
    有用
    回复 2
    • Оливер Пуппю
      Оливер Пуппю
      2022-07-14
      请问是怎么解决的呀
      2022-07-14
      回复
    • Оливер Пуппю
      Оливер Пуппю
      2022-07-14回复Оливер Пуппю
      我也解决了,请求格式很重要。access_token一定要放在url里面,code放在data里,请求头里还要加content-type:application/json。
      2022-07-14
      回复
  • Frank
    Frank
    2022-06-29

    2022-06-29
    有用
    回复
登录 后发表内容