PHP微信小程序消息推送写法

public function getAccessToken()
    {
    	//小程序appid
        $appId = 'wxf6bce3a3c80e8602';
        //秘钥
        $appSecret = 'cf58ad3c4ab89020b47d22cb9cedf6f8';

        $r = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?
        grant_type=client_credential&appid=$appId&secret=$appSecret"); //返回的是字符串,需要用json_decode转换成数组
        $data = json_decode($r, true);
        return $data['access_token'];

    }


    //接收下单数据接口
    //$door上门时间,$name联系人姓名,$tel联系人电话,$address地址,$detailed详细地址,$type废品类型,$welfare公益赠送
    public function purchase(){
        $data=input('post.');
        $rows=model('user')->where('city','=',$data['address'])->find();
     
      //判断用户提交订单的城市有没有开通服务
       if($rows==null){
       return json(['code'=>402,'msg'=>'此地区暂未开通服务']);
       }
       
        $token = $this->getAccessToken();
        //接收消息的用户openid
        $openid = $rows['openid'];
        //定义模板消息
        $datas=[
            'thing4'=>[
                'value'=>input('type')
            ],
            'name7'=>[
                'value'=>input('name')
            ],
            'phone_number9'=>[
                'value'=>input('tel')
            ],
            'thing10'=>[
                'value'=>input('detailed')
            ],
            'date2'=>[
                'value'=>input('door')
            ],
        ];
        $postData = [
            'access_token'=>$token,
            'touser'=>$openid,
            //模板id
            'template_id'=>'z0lL9PBe3s3DLMxN4vgCFCbFMyYwcjuQx92hQdlH5Xo',
            'data'=>$datas,
        ];

        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=$token";
//        halt($url);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData,320));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen(json_encode($postData,320))
            )
        );
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
$data['create_time']=time();
        $row=model('Purchase')->insert($data);
      return json(['code'=>200]);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值