微信公众平台开发 客服接口 发送客服消息
作者:方倍工作室
地址:http://www.cnblogs.com/txw1958/p/weixin73-custom-send.html
当用户主动发消息给公众号的时候(包括发送信息、点击自定义菜单clike事件、订阅事件、扫描二维码事件、支付成功事件、用户维权),微信将会把消息数据推送给开发者,开发者在一段时间内(目前为24小时)可以调用客服消息接口,通过POST一个JSON数据包来发送消息给普通用户,在24小时内不限制发送次数。此接口主要用于客服等有人工消息处理环节的功能,方便开发者为用户提供更加优质的服务。
一、获取ACCESS_TOKEN
http请求方式: GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
参数 | 是否必须 | 说明 |
---|---|---|
grant_type | 是 | 获取access_token填写client_credential |
appid | 是 | 第三方用户唯一凭证 |
secret | 是 | 第三方用户唯一凭证密钥,既appsecret |
正常情况下,微信会返回下述JSON数据包给公众号:
{"access_token":"ACCESS_TOKEN","expires_in":7200}
二、发送消息
发送文本消息
<span $txt</span> = '<span { "touser":"oLVPpjqs9BhvzwPj5A-vTYAX3GLc", "msgtype":"text", "text": { "content":"Hello World" } }</span>'<span ; </span><span $access_token</span> = "NU7Kr6v9L9TQaqm5NE3OTPctTZx797Wxw4Snd2WL2HHBqLCiXlDVOw2l-Se0I-WmOLLniAYLAwzhbYhXNjbLc_KAA092cxkmpj5FpuqNO0IL7bB0Exz5s5qC9Umypy-rz2y441W9qgfnmNtIZWSjSQ"<span ; </span><span $url</span> = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".<span $access_token</span><span ; </span><span $result</span> = https_post(<span $url</span>,<span $txt</span><span ); </span><span var_dump</span>(<span $result</span><span ); </span><span function</span> https_post(<span $url</span>,<span $data</span><span ) { </span><span $curl</span> =<span curl_init(); curl_setopt(</span><span $curl</span>, CURLOPT_URL, <span $url</span><span ); curl_setopt(</span><span $curl</span>, CURLOPT_SSL_VERIFYPEER, <span FALSE</span><span ); curl_setopt(</span><span $curl</span>, CURLOPT_SSL_VERIFYHOST, <span FALSE</span><span ); curl_setopt(</span><span $curl</span>, CURLOPT_POST, 1<span ); curl_setopt(</span><span $curl</span>, CURLOPT_POSTFIELDS, <span $data</span><span ); curl_setopt(</span><span $curl</span>, CURLOPT_RETURNTRANSFER, 1<span ); </span><span $result</span> = curl_exec(<span $curl</span><span ); </span><span if</span> (curl_errno(<span $curl</span><span )) { </span><span return</span> 'Errno'.curl_error(<span $curl</span><span ); } curl_close(</span><span $curl</span><span ); </span><span return</span> <span $result</span><span ; }</span>
发送图片 ( media_id需要先上传图片得到)
<span $img</span> = '<span { "touser":"oLVPpjqs9BhvzwPj5A-vTYAX3GLc", "msgtype":"image", "image": { "media_id":"jViS8sQUXIh6rTdkz2qUOD5pvChYpp50i9rsLR4YnCm-pqdpiHhz6xbN2KAboScV" } }</span>';
============================================
方倍工作室微信公众平台账号关注方法:
1. 微信通讯录-添加朋友-查找公众号-搜索“方倍工作室”
2. 微信通讯录-添加朋友-搜号码-输入“pondbaystudio”
3. 使用微信扫描下面的二维码