PHP微信公众平台开发高级篇--群发接口

群发消息接口

  • 订阅号:每天一条的群发权限
  • 服务号:每月(自然月)4条群发权限

实例

<?php
/**
 * 群发接口
 * PS:群发之前调用“预览接口”进行测试
 * PS:通过第三方后台调用微信上传图片素材接口,获取图片url,如:{"url":"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/BdxWN2kspVgJOFpRHJojlWmbl0pMxUaJibxrb33qm8Hkukvr6WTIxFibiccRhf5kibfpnEYMEOqKYSwuIe82w2O2Xg\/0"}
 *
 * 图文消息群发步骤:
 * 1.调用“新增临时素材”接口,获取到media_id:https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE(调用"新增永久素材"接口,获取media_id测试可以)
 * 2.调用"上传图文消息素材"接口,传入参数thumb_media_id即为上面接口返回的media_id,调用成功后会返回一个media_idhttps://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=ACCESS_TOKEN
 * 3.调用"预览接口"传入参数media_id即为上面返回的media_id,https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=ACCESS_TOKEN
 * 4.预览成功后,调用"根据标签进行群发"或者"根据OpenID列表群发"进行群发
 *
 *
 * 调用"新增永久素材"接口中的参数"thumb_media_id"是由调用"新增其他类型永久素材"接口返回的media_id,https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738729
 *
 */
header("Content-type: text/html; charset=utf-8");
$accessTokenInfo = file_get_contents("access_token.log");
$tokenArr = json_decode($accessTokenInfo,true);
if($accessTokenInfo && isset($tokenArr['expires_in']) && ($tokenArr['expires_in'] >time())){//保证不过期,做缓冲
    //1.获取全局access_token
    $access_token = $tokenArr['access_token'];
    //2.组装群发接口数据 array
    /*文本消息*/
//        $array = [
//            'touser' => 'oL-zT1Hkbx6Zza5Ny4ZZJr3Ze1-U',//openId,oL-zT1MGda2BndEV7x1m6c7NDk4o
//            'text'   => [
//                'content' =>urlencode('群发接口测试之文本消息')
//            ],
//            'msgtype' => 'text'//消息类型--文本
//        ];
    /*单图文*/
    $array = [
        'touser' => 'oL-zT1Hkbx6Zza5Ny4ZZJr3Ze1-U',//openId,
        'mpnews'   => [
            'media_id' =>'QvcxvrH6VnkO-Hxp_RYgo7sb32-bNQkxfm4JlNimVbha5l39llewHBGnW--dbJGL'//z6N0TsCHgLpDG_IOm4cI8OmbiClbDHPL7dKjXUBJsHQ
        ],
        'msgtype' => 'mpnews',//消息类型--单图文
        'send_ignore_reprint' =>0
    ];

    //3.array===>json
    $postJson = urldecode(json_encode($array));
    //调用curl
    $url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=".$access_token;
//    $url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=".$access_token;
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$postJson);
    $output = curl_exec($ch);
    curl_close($ch);
    var_dump(json_decode($output,true));

}else{
    //重新请求access_token写入文件
    $ch = curl_init();
    $appId = "wxc7991ed87e25dc13";
    $appSecret = "3c1be9b400aae7042afb78dbd577f206";
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    $outPut = curl_exec($ch);
    curl_close($ch);
    $arr = json_decode($outPut,true);
    $tmp = [
        'access_token' =>$arr['access_token'],
        'expires_in' =>time() + ($arr['expires_in'] - 200)
    ];
    file_put_contents("access_token.log",json_encode($tmp));
}

猜你喜欢

转载自blog.csdn.net/qq_29627497/article/details/81099172
今日推荐