php获取微信小程序UrlScheme,通过H5页面或微信浏览器唤起小程序
并进入指定页面
//微信小程序操作类 class MP_WEIXIN { private $appid = null; private $secret = null; function __construct($appid, $secret) { $this->appid = $appid; $this->secret = $secret; } public function GetAccessToken() { $file = 'token_' . $this->appid; if (file_exists($file)) { $data = $this->ReadFile($file); $now = time(); if ($data['exptime'] > $now) { return $data['token']; } } $json = Http::getJson("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret=" . $this->secret); if (empty($json) || !isset($json['access_token'])) { //获取失败了 print_r($json); return ''; } $data = [ 'token' => $json['access_token'], 'exptime' => $now + 7200 ]; file_put_contents($file, "<?php\nreturn " . var_export($data, true) . ';'); return $data['token']; } private function ReadFile($file) { return include $file; } public function CreateUrlScheme($access_token, $param) { $url = "https://api.weixin.qq.com/wxa/generatescheme?access_token=" . $access_token; $data = [ 'jump_wxa' => [ 'path' => '/pages/index/index', 'query' => 'scene=' . urlencode($param) ], 'expire_type' => 1, 'expire_interval' => 30, ]; $postdata = json_encode($data); $res = Http::post($url, $postdata, [ 'Content-Type: application/json' ]); //{"errcode":0,"errmsg":"ok","openlink":"weixin:\/\/dl\/business\/?t=CXFdofLUcAp"} $obj = json_decode($res, true); if (empty($obj) || !isset($obj['errcode']) || $obj['errcode'] !== 0) { return false; } return $obj['openlink']; } //登录凭证 code 换取用户唯一标识 OpenID、UnionID、会话密钥 session_key public function code2Session($jscode) { $url = 'https://api.weixin.qq.com/sns/jscode2session'; $url .= "?appid={$this->appid}&secret={$this->secret}&js_code={$jscode}"; $url .= '&grant_type=authorization_code'; $res = Http::getJson($url); if (!isset($res['openid'])) return false; return $res; } //获取手机号 //参数:$access_token、手机号获取凭证code public function getphonenumber($access_token, $code) { $url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=' . $access_token; $res = Http::post( $url, json_encode([ 'code' => $code ]), [ 'Content-Type: application/json' ] ); return json_decode($res, true); } }
其中一个Http类可以参考curl的相关操作,