微信号分为服务号和订阅号(还有企业号内部用),服务号像qq,对方给你发信息,能马上看到,订阅号像邮件,你必须自己打开对方的订阅号才能看到即时的信息。所以服务号更像一个普通微信好友,可以在线沟通。但是微信为了控制广告效果,服务号每月只能发4次消息,而订阅号却可以每天一次消息。虽然公众号也可以用高级群发接口编程发送,但是更厉害的是微信从客户端限制每用户只能接收4次公众号的消息。所以如果只编程发送,超过4次,用户根本不知道有这消息,微信服务器就给你拦掉了。还好,微信为企业服务留了个口子,图文消息不让发,但是简单文本消息可以发,突破公众号限制,每天发送一条消息的方法就走的这个接口。微信监测这个接口,每日限额10000次的调用,我们只有小几千个关注者,够用了。
- <?php
- $appid = "wx";
- $appsecret = "0b97ad645797ee2";
- $times= date('y年m月d日 H:i:s',time());
- echo $times."</br>";
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;//取得token
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($ch);
- curl_close($ch);
- $jsoninfo = json_decode($output, true);
- $access_token = $jsoninfo["access_token"];
- // echo $access_token."</br>";
- $begin=0;
- if($begin>=10000)
- {$url= "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$access_token."&next_openid=".$_GET['next_openid'];}
- else{$url= "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$access_token; }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($ch);
- curl_close($ch);
- $array = json_decode($output, true);
- echo $array["count"]."</br>";
- // foreach ($array["data"]["openid"] as $key=>&$value) {//获取用户的唯一标识
- // echo ($key)."->".$value."</br>" ;
- // } //群发注释
- $times= date('y年m月d日 H:i:s',time());
- $template = array('touser' =>"oC282t60QEDt0El-hcVOShgzgTmQ",//&$value, // //
- 'template_id' => "gJ_qahsRIMxRtxeWtKuhChzfA6w8HHdZE2xa7_XQ5u8",
- // 'url' =>"http://eqxiu.com/s/7pWq",
- 'url' => "http://mp.weixin.qq.com/",
- 'topcolor' => "#7B68EE",
- 'data' => array('first' => array('value' => urlencode("新闻标题:"),
- 'color' => "#743A3A",
- ),
- 'keyword1' => array('value' => urlencode("XXXXXX"),
- 'color' => "#FF0000",
- ),
- 'keyword2' => array('value' => urlencode("全体关心XXX"),
- 'color' => "#C4C400",
- ),
- 'keyword3' => array('value' => urlencode($times),
- 'color' => "#0000FF",
- ),
- 'keyword4' => array('value' => urlencode("工作先进集体"),
- 'color' => "#FF0000",
- ),
- 'remark' => array('value' => urlencode("\\n你好。点击查看“详情”!"),
- 'color' => "#008000",
- ),
- )
- );
- // var_dump($template)."</br>";
- // echo urldecode(json_encode($template))."</br>";
- $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
- $result = https_request($url, urldecode(json_encode($template)));//"";//群发命令
- // var_dump($result)."</br>";
- // } //群发关闭
- function https_request($url,$data = null){
- $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);
- // echo "</br>";
- // echo $output;
- return $output;
- }
<?php
$appid = "wx";
$appsecret = "0b97ad645797ee2";
$times= date('y年m月d日 H:i:s',time());
echo $times."</br>";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;//取得token
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$jsoninfo = json_decode($output, true);
$access_token = $jsoninfo["access_token"];
// echo $access_token."</br>";
$begin=0;
if($begin>=10000)
{$url= "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$access_token."&next_openid=".$_GET['next_openid'];}
else{$url= "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$access_token; }
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$array = json_decode($output, true);
echo $array["count"]."</br>";
// foreach ($array["data"]["openid"] as $key=>&$value) {//获取用户的唯一标识
// echo ($key)."->".$value."</br>" ;
// } //群发注释
$times= date('y年m月d日 H:i:s',time());
$template = array('touser' =>"oC282t60QEDt0El-hcVOShgzgTmQ",//&$value, // //
'template_id' => "gJ_qahsRIMxRtxeWtKuhChzfA6w8HHdZE2xa7_XQ5u8",
// 'url' =>"http://eqxiu.com/s/7pWq",
'url' => "http://mp.weixin.qq.com/",
'topcolor' => "#7B68EE",
'data' => array('first' => array('value' => urlencode("新闻标题:"),
'color' => "#743A3A",
),
'keyword1' => array('value' => urlencode("XXXXXX"),
'color' => "#FF0000",
),
'keyword2' => array('value' => urlencode("全体关心XXX"),
'color' => "#C4C400",
),
'keyword3' => array('value' => urlencode($times),
'color' => "#0000FF",
),
'keyword4' => array('value' => urlencode("工作先进集体"),
'color' => "#FF0000",
),
'remark' => array('value' => urlencode("\\n你好。点击查看“详情”!"),
'color' => "#008000",
),
)
);
// var_dump($template)."</br>";
// echo urldecode(json_encode($template))."</br>";
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
$result = https_request($url, urldecode(json_encode($template)));//"";//群发命令
// var_dump($result)."</br>";
// } //群发关闭
function https_request($url,$data = null){
$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);
// echo "</br>";
// echo $output;
return $output;
}
保存成php文件,设置好
$appid = "wx";
$appsecret = "0b97ad645797ee2";
改掉几处注释,即可以使用,最好先发到自己号上测试一下