php微信公众号模版消息发送(群发)

public function sendall(){
    $all_openid=$this->tosendall();
    foreach($all_openid as $value){
       $this->set_msg($value);
    }
}
public function tosendall(){
    $access_token=$this->getAccesstoken();
    $url="https://api.weixin.qq.com/cgi-bin/user/get?access_token={$access_token}&next_openid=";

    $ch=curl_init($url);
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $data=curl_exec($ch);
    $data=json_decode($data,true);
    return $data['data']['openid'];
}
public function set_msg($openid){
    $access_token = $this->getAccesstoken();
    $template = array(
        "touser" => "$openid",//用户openid
        "template_id" => "-dRb-6prGX6AO6Pt_uTirmH2fztkVHLFG7HHfISwjXg",//模板id
        "url" => "",//跳转链接
        "data" => array(
            'first' => array('value' => urlencode("为更好的为您提供服务,原公众号功能升级,特此通知。")),
            'keyword1' => array('value' => urlencode("公众号功能")),
            'keyword2' => array('value' => urlencode("全站业务")),
            'keyword3' => array('value' => urlencode("2018年3月30日")),
            'keyword4' => array('value' => urlencode("现已升级完毕")),
            'remark' => array('value' => urlencode("点击这里,更多优惠!更多惊喜都在等你来发现。")),
        )
    );
    $json_template = json_encode($template);
    $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
    $json = $this->curl_post($url, urldecode($json_template));
    $rs = json_decode($json, true);
    if ($rs['errcode'] == 0) {echo 'ok';
    } else {
        echo "no";
        print_r($rs);
    }
}
//发送请求
    function curl_post($url,$data){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
 public function getAccesstoken(){
        $tokenFile = "./accessToken.json"; // 缓存文件名
        $data = json_decode(file_get_contents($tokenFile)); //转换为json格式
        if ($data->expire_time < time() or ! $data->expire_time) {
            $appid='公众号的appid';//微信公众平台上的appid和secret;
            $secret='公众号的secret';
           $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            $info = curl_exec($ch);
            $dataJson = json_decode($info, true);
            if ($dataJson) {
                $data['expire_time'] = time() + 3600; //保存1小时
                $data['access_token'] = $dataJson['access_token'];
                $fp = fopen($tokenFile, "w"); //只写文件
                fwrite($fp, json_encode($data)); //写入json格式文件
                fclose($fp); //关闭连接
            }
        } else {
            $access_token = $data->access_token;
        }
        return $access_token;
    }

 

你可能感兴趣的:(PHP,微信)