PHP 微信客服推送消息跨坑记录。

统一格式即可,支付宝回调里面也可以写:

1、发送文本消息

{
    "touser":"OPENID",
    "msgtype":"text",
    "text":
    {
         "content":"Hello World"
    }
}
参数 是否必须 说明
access_token 调用接口凭证
touser 普通用户openid
msgtype 消息类型,text
content 文本消息内容
$txt = '您的好友  '.$user['username'].'  已缴费成为  '.$level_name.'! ';
$data = '{"touser":"'.$userpa1['openid'].'","msgtype":"text","text":{"content":"'.$txt.'"}}';
    $opts = array(
        'http' => array(
        'method' => 'POST',
        'Content-Length' => strlen($data),
        'Host' => 'api.weixin.qq.com',
        'Content-Type' => 'application/json',
        'content' => $data,
    )
);
$context = stream_context_create($opts);
file_get_contents('https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$access_token, true, $context);
//要先获取openid和access_token才能发送

2、发送图文消息

图文消息条数限制在10条以内,注意,如果图文数超过10,则将会无响应。

{
    "touser":"OPENID",
    "msgtype":"news",
    "news":{
        "articles": [
         {
             "title":"这里是标题",
             "description":"这里是简介",
             "url":"URL",
             "picurl":"图片URL"
         },
         {
             "title":"这里是标题",
             "description":"这里是简介",
             "url":"URL",
             "picurl":"图片URL"
         }
         ]
    }
}
参数 是否必须 说明
access_token 调用接口凭证
touser 普通用户openid
msgtype 消息类型,news
title 标题
description 描述
url 点击后跳转的链接
picurl 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80
$openid = db('users')->where('id',16880)->value('openid');
$value = db('wx_config')->where([ 'key' => 'SHOPWCHAT'])->value('value');
$value = json_decode($value,true);
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$value['appid']."&secret=".$value['appsecret'];
$cont = json_decode($this->getToken($url));
$data = '{"touser":"'.$openid.'","msgtype":"news","news":{ "articles": [{"title":"'.input('title').'","description":"'.input('description').'","url":"'.input('url').'","picurl":"PIC_URL"}]}}';
    $opts = array(
        'http' => array(
            'method' => 'POST',
            'Content-Length' => strlen($data),
            'Host' => 'api.weixin.qq.com',
            'Content-Type' => 'application/json',
            'content' => $data,
        )
    );
    $context = stream_context_create($opts);
    file_get_contents('https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$cont->access_token, true, $context);

以上。

猜你喜欢

转载自blog.csdn.net/qq_34876813/article/details/82455249