微信小程序服务通知

项目中用到了小程序的服务消息通知,通知订单状态信息,下边就是整理的一下代码,放到项目中,把项目的小程序appid和小程序的secret写进去,直接运行即可

提前申请好小程序服务信息通知短信模板,代码需要用到模板id

public static function curl($url, $params = false, $ispost = 0, $https = 0)

    {

        $httpInfo = array();

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

        curl_setopt( $ch, CURLOPT_HTTPHEADER, array(

                'Content-Type: application/json; charset=utf-8'

            )

        );

        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);

        curl_setopt($ch, CURLOPT_TIMEOUT, 30);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        if ($https) {

            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查

            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在

        }

        if ($ispost) {

            curl_setopt($ch, CURLOPT_POST, true);

            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

            curl_setopt($ch, CURLOPT_URL, $url);

        } else {

            if ($params) {

                if (is_array($params)) {

                    $params = http_build_query($params);

                }

                curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);

            } else {

                curl_setopt($ch, CURLOPT_URL, $url);

            }

        }

        $response = curl_exec($ch);

        if ($response === FALSE) {

            return false;

        }

        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));

        curl_close($ch);

        return $response;

    }

    public static function access_token(){

        $appid="--------";  //小程序appid

        $secret="-------"; //小程序secret

        $Url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=". $appid."&secret=".$secret; //微信给出的获取access_token的接口

        $access_token=Cache::get("access_token");  //查询缓存中是否已存在access_token

        if($access_token==""){

            $access_token=json_decode(self::curl($Url))->{"access_token"};  //访问接口获取access_token

        }

        return $access_token;

    }

    public static function SendMsg($data,$access_token){

        $MsgUrl="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$access_token; //微信官方接口,需要拼接access_token

        return json_decode(self::curl($MsgUrl,$params=json_encode($data),$ispost=1,$https=1)); //访问接口,返回参数

    }

    public function test(Request $request){

        $openid='------------';//接收信息用户的openID,如果存在数据库中,可在数据库中查找

        $access_token=$this->access_token();

        $data=[

            "touser"=>$openid, //接收用户的openid

            "template_id"=>"---------",  //短信模板id

            "page"=>"pages/index/index",//点击模板消息跳转至小程序的页面

            "data"=>[

                //data数组下的键值一定要和申请短信模板一直,否则会报错

                "thing2"=>[

                    "value"=> "您邀请的好友'E-June'订单已完成", //自定义参数

                ],

                "amount1"=>[

                    "value"=> "3.8元",//自定义参数

                ],

                "thing3"=>[

                    "value"=> "水果熟了 推荐有奖",//自定义参数

                ],

                "time4"=>[

                    "value"=> "2021年05月26日 11:15",//自定义参数

                ],

                "thing5"=>[

                    "value"=> "客服审核通过自动增加收益",//自定义参数

                ],

            ]

        ];

        $res=$this->SendMsg($data,$access_token); //返回结果

        var_dump($res);die;

    }

}

执行方法test即可发送成功,前提是小程序要允许小程序的服务通知才行

测试时执行结果是0,并且ok就发送成功,其他返回代码可以在微信公众平台查看,这里就不解释了

你可能感兴趣的:(微信小程序服务通知)