小程序获取手机号码wordpress做服务端

2022-10-21

样式如下,点击获取手机号,小程序就可获取微信绑定的手机号码

<view class="tel">
<input type="number" name="tel" value='{{tel}}' maxlength="11" placeholder="请输入手机号码" placeholder-style="color:#c9c9c9;"/>
<button class="gettel" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取手机号</button>
</view>
<view class="tel"> <input type="number" name="tel" value='{{tel}}' maxlength="11" placeholder="请输入手机号码" placeholder-style="color:#c9c9c9;"/> <button class="gettel" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取手机号</button> </view>
 <view class="tel">
    <input type="number" name="tel" value='{{tel}}'  maxlength="11"  placeholder="请输入手机号码" placeholder-style="color:#c9c9c9;"/>
    <button class="gettel" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取手机号</button>
 </view>
getPhoneNumber (e) {
var that = this
var code = e.detail.code
wx.request({
url: 'https://www.xalhsz.cn/wp-json/getphone/v2',
method:"post",
data:{
code:code
},
success:(res)=>{
that.setData({
tel:res.data
})
}
})
},
getPhoneNumber (e) { var that = this var code = e.detail.code wx.request({ url: 'https://www.xalhsz.cn/wp-json/getphone/v2', method:"post", data:{ code:code }, success:(res)=>{ that.setData({ tel:res.data }) } }) },
  getPhoneNumber (e) {
        var that = this
        var code = e.detail.code
        wx.request({
            url: 'https://www.xalhsz.cn/wp-json/getphone/v2',
            method:"post",
            data:{
                code:code
            },
            success:(res)=>{
                that.setData({
                    tel:res.data
                })
            }
        })
    },

wordpress服务端代码如下:

// 获取手机号
add_action('rest_api_init','wp_api_get_phone');
function wp_api_get_phone(){
register_rest_route('getphone','/v2',array(
'methods' => 'POST',
'callback' => 'getphone_callback',
));
}
function getphone_callback($request_data){
$token = getAccessToken();
$para = $request_data->get_params();
$data['code'] = $para['code'];
$url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=".$token;
$arr = json_encode($data);
$info = _request($url,true,'POST',$arr);
$tmpinfo = json_decode($info);
$code = $tmpinfo->errcode;
$phone_info = $tmpinfo->phone_info;
$phoneNumber = $phone_info->phoneNumber;
if($code == '0'){
return $phoneNumber;
}else{
return "not get phoneNumber";
}
}
// 获取手机号 add_action('rest_api_init','wp_api_get_phone'); function wp_api_get_phone(){ register_rest_route('getphone','/v2',array( 'methods' => 'POST', 'callback' => 'getphone_callback', )); } function getphone_callback($request_data){ $token = getAccessToken(); $para = $request_data->get_params(); $data['code'] = $para['code']; $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=".$token; $arr = json_encode($data); $info = _request($url,true,'POST',$arr); $tmpinfo = json_decode($info); $code = $tmpinfo->errcode; $phone_info = $tmpinfo->phone_info; $phoneNumber = $phone_info->phoneNumber; if($code == '0'){ return $phoneNumber; }else{ return "not get phoneNumber"; } }
// 获取手机号
add_action('rest_api_init','wp_api_get_phone');
function wp_api_get_phone(){
    register_rest_route('getphone','/v2',array(
        'methods' => 'POST',
        'callback' => 'getphone_callback',
    ));
}
function getphone_callback($request_data){
    $token = getAccessToken();
    $para = $request_data->get_params();
    $data['code'] = $para['code'];

    $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=".$token;
    $arr = json_encode($data);
    $info = _request($url,true,'POST',$arr);
    $tmpinfo = json_decode($info);
    $code = $tmpinfo->errcode;
    $phone_info = $tmpinfo->phone_info;
    $phoneNumber = $phone_info->phoneNumber;
    if($code == '0'){
        return $phoneNumber;
    }else{
        return "not get phoneNumber";
    }
}

getAccessToken和_request()如下:

/*获取access_token,不能用于获取用户信息的token*/
function getAccessToken()
{
$appid = "wx46ccf5dfdfd9531f";
$appsecret = "306a8b61dfddfdfddfdfcb035282bf80";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$con = _request($url);
$cont = json_decode($con);
return $cont->access_token;
}
//设置网络请求配置
function _request($curl,$https=true,$method='GET',$data=null){
// 创建一个新cURL资源
$ch = curl_init();
//header('Content-Type:application/json; charset=utf-8');
$headers = [
'form-data' => ['Content-Type: multipart/form-data'], 'json' => ['Content-Type: application/json'],
];
// 设置URL和相应的选项
curl_setopt($ch, CURLOPT_URL, $curl); //要访问的网站
//启用时会将头文件的信息作为数据流输出。
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers[$type]);
//将curl_exec()获取的信息以字符串返回,而不是直接输出。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if($https){
//FALSE 禁止 cURL 验证对等证书(peer's certificate)。
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); //验证主机
}
if($method == 'POST'){
curl_setopt($ch, CURLOPT_POST, true); //发送 POST 请求
//全部数据使用HTTP协议中的 "POST" 操作来发送。
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
// 抓取URL并把它传递给浏览器
$content = curl_exec($ch);
//关闭cURL资源,并且释放系统资源
curl_close($ch);
return $content;
}
/*获取access_token,不能用于获取用户信息的token*/ function getAccessToken() { $appid = "wx46ccf5dfdfd9531f"; $appsecret = "306a8b61dfddfdfddfdfcb035282bf80"; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret; $con = _request($url); $cont = json_decode($con); return $cont->access_token; } //设置网络请求配置 function _request($curl,$https=true,$method='GET',$data=null){ // 创建一个新cURL资源 $ch = curl_init(); //header('Content-Type:application/json; charset=utf-8'); $headers = [ 'form-data' => ['Content-Type: multipart/form-data'], 'json' => ['Content-Type: application/json'], ]; // 设置URL和相应的选项 curl_setopt($ch, CURLOPT_URL, $curl); //要访问的网站 //启用时会将头文件的信息作为数据流输出。 curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers[$type]); //将curl_exec()获取的信息以字符串返回,而不是直接输出。 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if($https){ //FALSE 禁止 cURL 验证对等证书(peer's certificate)。 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); //验证主机 } if($method == 'POST'){ curl_setopt($ch, CURLOPT_POST, true); //发送 POST 请求 //全部数据使用HTTP协议中的 "POST" 操作来发送。 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } // 抓取URL并把它传递给浏览器 $content = curl_exec($ch); //关闭cURL资源,并且释放系统资源 curl_close($ch); return $content; }
/*获取access_token,不能用于获取用户信息的token*/
function getAccessToken()
{
    $appid = "wx46ccf5dfdfd9531f";
    $appsecret = "306a8b61dfddfdfddfdfcb035282bf80";
    
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
    
    $con = _request($url);
    $cont = json_decode($con);
    return $cont->access_token;
}

  
//设置网络请求配置
function _request($curl,$https=true,$method='GET',$data=null){
    // 创建一个新cURL资源
    $ch = curl_init();
    //header('Content-Type:application/json; charset=utf-8');
    $headers = [
        'form-data' => ['Content-Type: multipart/form-data'], 'json' => ['Content-Type: application/json'],
    ]; 
    // 设置URL和相应的选项
    curl_setopt($ch, CURLOPT_URL, $curl);  //要访问的网站
    //启用时会将头文件的信息作为数据流输出。
    curl_setopt($ch, CURLOPT_HEADER, false);  
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers[$type]);
    //将curl_exec()获取的信息以字符串返回,而不是直接输出。
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
     
    if($https){
    //FALSE 禁止 cURL 验证对等证书(peer's certificate)。
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); //验证主机
    }
    if($method == 'POST'){
    curl_setopt($ch, CURLOPT_POST, true); //发送 POST 请求
     //全部数据使用HTTP协议中的 "POST" 操作来发送。
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    }
     
     
    // 抓取URL并把它传递给浏览器
    $content = curl_exec($ch);
     
    //关闭cURL资源,并且释放系统资源
    curl_close($ch);
     
    return $content;
}
相关内容
最新

坚持的力量

wordpress建站,视频剪辑拍摄,动画制作