• php版 短信跳转微信小程序


    实现这功能首先,小程序端添加业务域名 

    php代码

    1. declare (strict_types=1);
    2. namespace app\controller\Admin;
    3. use app\model\Set;
    4. use app\Request;
    5. class Admin_Url_Scheme
    6. {
    7. public function getScheme(Request $request) {
    8. $appid = '小程序appid';
    9. $secret = '小程序appsecret';
    10. //path是要跳转的小城页面地址,query为要携带的参数
    11. $body = ['jump_wxa'=>['path'=>'/pages/index/index','query'=>$scheme['name']]];
    12. $tokenurl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret;
    13. $data = file_get_contents($tokenurl);
    14. $data = json_decode($data, true);
    15. $token = $data['access_token'];
    16. $url = "https://api.weixin.qq.com/wxa/generatescheme?access_token=" . $token;
    17. $data = $this->curl_post($url, null, $body);
    18. if ($data['errmsg'] == 'ok'){
    19. return json(['code' => 200,'data'=>$data['openlink'], 'msg' =>'操作成功']);
    20. }else{
    21. return json(['code' => 201,'data'=>$data, 'msg' =>'异常']);
    22. }
    23. }
    24. function curl_post($url,$herder,$body){
    25. //一般框架都会自带GuzzleHttp,没有的请手动安装,或者利用curl post请求
    26. $client = new \GuzzleHttp\Client();
    27. try {
    28. $pram = $client->post($url,[
    29. 'headers'=>$herder,
    30. 'json'=>$body,
    31. ]);
    32. $content = json_decode($pram->getBody()->getContents(),true);
    33. return $content;
    34. }catch (ErrorException $exception){
    35. return $exception->getCode();
    36. }
    37. }
    38. }

    下面使用js的location.href

    location.href = 'https://***.***.com'

    我前端用的是uniapp,vue的话要安装axios或者jq