php调用微信客服消息接口给用户发送信息
2017-09-07 17:32
585 查看
$token_file = fopen("token.txt", "r"); //获取文本里的access_token和时间戳
$rs = fgets($token_file);
fclose($token_file);
$attr = explode(',',$rs);
$time2 = time();
$token = $attr[0];
if(intval($time2)-intval($attr[1])>7000) { //判断时间戳是否过期,如果过期就重新调用接口,获取access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=公众号的sppid&secret=公众号的secret";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
$output = json_decode($output, true);
$token_file = fopen("token.txt","w");//打开token.txt文件
fwrite($token_file,$output['access_token'].','.time());//重写tken.txt全部内容
fclose($token_file);//关闭文件流
curl_close($curl);
$token = $output['access_token'];
}
$postdata ='{"touser":"用户的openid","msgtype":"text","text":{"content":"内容"}}';
$opts = array(
'http' => array(
'method' => 'POST',
'Content-Length' => strlen($postdata),
'Host' => 'api.weixin.qq.com',
'Content-Type' => 'application/json',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$token.'', true, $context);
$rs = fgets($token_file);
fclose($token_file);
$attr = explode(',',$rs);
$time2 = time();
$token = $attr[0];
if(intval($time2)-intval($attr[1])>7000) { //判断时间戳是否过期,如果过期就重新调用接口,获取access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=公众号的sppid&secret=公众号的secret";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
$output = json_decode($output, true);
$token_file = fopen("token.txt","w");//打开token.txt文件
fwrite($token_file,$output['access_token'].','.time());//重写tken.txt全部内容
fclose($token_file);//关闭文件流
curl_close($curl);
$token = $output['access_token'];
}
$postdata ='{"touser":"用户的openid","msgtype":"text","text":{"content":"内容"}}';
$opts = array(
'http' => array(
'method' => 'POST',
'Content-Length' => strlen($postdata),
'Host' => 'api.weixin.qq.com',
'Content-Type' => 'application/json',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$token.'', true, $context);
相关文章推荐
- 微信开发:客服接口向用户发送消息
- 微信消息接口发送信息到分组和用户,错误代码40003和40008
- 调用微信接口发送消息
- 接口测试-工作心得记录三(调用微信接口发送监控消息)
- 微信客服给单个用户发送消息
- [实例]JAVA调用微信接口发送图文消息,不用跳到详情页
- php发送微信客服消息
- PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息限制
- php微信高级接口调用方法(自定义菜单接口、客服接口、二维码)
- C# 调用微信接口上传素材和发送图文消息
- Thinkphp5微信小程序获取用户信息接口调用笔记
- 微信开发 python django 多客服接口发送到客户端的消息乱码
- django 调用微信接口(微信支付、微信用户信息)
- php微信高级接口调用方法(自定义菜单接口、客服接口、二维码)
- PHP版微信公共平台消息主动推送,突破订阅号一天只能发送一条信息限制
- 调用微信OAuth2.0网页授权接口实现用户信息的绑定
- 微信开发怎么被动发送图文消息用户输入信息之后回复
- php微信高级接口调用方法(自定义菜单接口、客服接口、二维码)
- 微信公众号客服接口给指定用户openid发送消息
- php 微信模拟登陆给用户发送消息(文字,图片,图文)