php微信公众号消息推送

public function template($id)
{
    $user = User::where('id',$id)->value('openid');//微信openid
    $url = $_SERVER['HTTP_HOST'];
    $wxurl = 'https://api.weixin.qq.com/cgi-bin/token/template/send?access_token=ACCESS_TOKEN';
    $weixin = Weixin::where('id',1)->select();
    $appid = $weixin[0]['appid'];
    $secret = $weixin[0]['appsecret'];
    //获得access_token
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
    $weix = file_get_contents($url);//获得网页输出
    $obj=json_decode($weix,true );//解码
    $access_token= $obj['access_token'];//网页授权接口调用凭证
    //发送模板消息
    $fasuerl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
    $data = array(
        "touser"=>$user,
        "template_id"=>"2GSNHCC4xOrz9p9fqmKLbUwknwVa1iZnClqzbQb5xhw",
        "data" => array(
            "first" => array(
                "value"=>"咨询通知",
                "color"=>"#173177"
            ),
            "keyword1" => array(
                "value"=>"巧克力!",
                "color"=>"#000000"
            ),
            "keyword2" => array(
                "value"=>"巧克力!",
                "color"=>"#000000"
            ),
            "remark" => array(
                "value"=>"巧克力!",
                "color"=>"#000000"
            ),
        )
    );
    $params = json_encode($data);
    $res=$this->curl_post($fasuerl,$params);
    print_r($res);
}
//发送post请求
function curl_post($url , $data=array()){
    $ch = curl_init();//创建curl请求
    curl_setopt($ch, CURLOPT_URL,$url); //设置发送数据的网址
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设置有返回值,0,直接显示
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); //禁用证书验证
    curl_setopt($ch, CURLOPT_POST, 1);//post方法请求
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//post请求发送的数据包
    $data = curl_exec($ch);
    curl_close($ch);
    $data = json_decode($data,true); //将json数据转成数组
    return $data;
}

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大得369

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值