$wchat = new WchatOauth(); $access_token = $wchat->get_access_token();//封装的获取accesstoken方法,自己看官方文档,这里需要做下缓存。 $appid = \'wx3eb1e********\'; //填入服务号AppID // 获取ticket $ticket_res = curlRequests("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$access_token}&type=jsapi"); $ticket = $ticket_res[\'ticket\']; // 时间戳 $timestamp = time(); // nonceStr为16位随机数即可 $nonceStr = createNonceStr(); // URL为调用JSSDK的页面地址,后面有参数也需要放进去 $url = "http://wechat.com/xxxxxxxxxxxxxxxxx"; // 调用JSSDK的页面地址 // sha1加密 $str = "jsapi_ticket={$ticket}&noncestr={$nonceStr}×tamp={$timestamp}&url={$url}"; $sha_str = sha1($str); $ret =["appid"=>$appid,"timestamp"=>$timestamp,"nonceStr"=>$nonceStr,"signature"=>$sha_str]; function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } function curlRequests($url, $data = null) { // curl 初始化 $curl = curl_init(); // curl 设置 curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 判断 $data get or post if (!empty($data)) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 执行 $res = curl_exec($curl); curl_close($curl); return json_decode($res, true); }
微信公众号内h5页面调起微信小程序
内容版权声明:除非注明,否则皆为本站原创文章。