派克斯代理ip微信小程序发送订阅消息(服务端PHP)

参考文档:subscribeMessage.send | 开放文档

1.请求地址

https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN

首先获取access_token

1.派克斯代理ip公共封装访问url方法

/** * $url : url地址, * $data: 参数值, * $cookie:页面cookie * $is_post:是否为post传值 1/是 0/否 * $header:头部信息 * $ip:代理ip * $port:代理ip端口 **/public function httpcurls($url,$data,$cookie=null,$is_post=0,$header=null,$ip=null,$port=null){$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);//初始化curl会话if (!empty($header)){curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//自动设置访问header信息}curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//将获取的信息以字符串返回curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);//禁止进行证书验证 httpsif ($is_post == 1){curl_setopt($ch, CURLOPT_POST, 1);//curl post 访问}if (!empty($data)){curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//post传递的参数值}if (!empty($cookie)){curl_setopt($ch, CURLOPT_COOKIE, $cookie);//携带cookie访问}if (!empty($ip)){curl_setopt($ch,CURLOPT_PROXY,$ip);//如果有代理ip 此处为代理ip地址 没有则不填curl_setopt($ch,CURLOPT_PROXYPORT,$port);//代理ip端口}$output = curl_exec($ch);curl_close($ch);//关闭curl资源 并且释放return $output;}

获取:

/** * 获取access_token*/public function access_token($appid=,$secret=){$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";$data= $this->httpcurls($url,,0);$access_token= json_decode($data,true);return$access_token[access_token];}

2.调用 access_token 访问 send 发送模板消息

/** * 订阅消息发送*/public function (string $openid ,string $msg,string $name,int $phone,string $address,string $time){$appid= $this->APPID;$secret = $this->secret;$access_token = $this->access_token($appid,$secret);//获取token$url= "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=$access_token";//订阅url subscribeMessage.send$data = [touser=>$openid,//接收者(用户)的 openidtemplate_id=>0GlqBiB_1xutepKiheKinJ02MBXBDHG4S1c8jUx6vIk,//所需下发的订阅模板id//page=>index?foo=bar,//点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,该字段不填则模板无跳转。data=>[thing2=>[value=>$msg],//备注name5=>[value=>$name],//客户姓名phone_number6=>[value=>$phone],//客户电话thing7=>[value=>$address],//客户地址date8=>[value=>$time],//下单时间], // miniprogram_state =>developer,//跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版];$send = $this->httpcurls($url,json_encode($data),,1);return $send;}

至此 直接调用就可以了

/** * 发送订阅消息*/public function send(){$send = new \app\api\controller\Message();$list = $send->(openid,有最新订单,cong,,9-1-902,2022-05-10);dump($list);}