微信发送文本消息

public function massAction(){
        Y::disableView();
        
        //对于认证服务号虽然开发者使用高级群发接口的每日调用限制为100次,
     //但是用户每月只能接收4条,无论在公众平台网站上,
     //还是使用接口群发,用户每月只能接收4条群发消息,
     //多于4条的群发将对该用户发送失败;
        //群发接口每分钟限制请求60次,超过限制的请求会被拒绝
        //订阅号每天只能群发一条消息
        //服务号每月四条群发权限
        //预览接口可以调用100次/天


        //1获取全局access_token
        echo $access_token=$this->accessTokenAction();
        $url="https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=".$access_token;
        //2组装群发数据接口数据array
        //单文本
        $con='我就是想发打中文';
//        $cont=urlencode($con);
        $array=[
            'touser'=>'o0727vy3NDqPvVq-6P_ADqpdPXIA',//微信用户的openid 
            'text'=>array(
//                'content'=>         urlencode(htmlspecialchars(str_replace("\"", "'", '你叫啥名字啊.....我猜叫哈哈哈'))),//文本内容
                'content'=>       $con,//文本内容
            ),
            'msgtype'=>'text',   //消息类型

        ];


        //3将数组转成json格式
//        $postjson=json_encode($array);
        $postjson=json_encode($array,JSON_UNESCAPED_UNICODE);
        //4调用curl
        $res=$this->httpCurlAction($url,'post','json',$postjson);
        var_dump($res);

    }

在Java中使用HTTP请求调用企业微信发送文本消息的接口,你需要先获取到企业微信提供的API访问凭证,如access_token。以下是一个基本的例子,展示如何使用Apache HttpClient库来完成这个任务: ```java import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class WechatMessageSender { private static final String魏信公众平台接口URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=<your_access_token>"; public void sendMessage(String text) throws Exception { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(WechatMessageSender魏信公众平台接口URL); // 设置POST数据,这里是发送文本消息的数据格式 String jsonPayload = "{\"touser\":\"@all\",\"msgtype\":\"text\",\"text\":{\"content\":\"" + text + "\"}}"; StringEntity stringEntity = new StringEntity(jsonPayload, "UTF-8"); httpPost.setEntity(stringEntity); // 发送请求 CloseableHttpResponse response = httpClient.execute(httpPost); try { HttpEntity entity = response.getEntity(); if (entity != null) { System.out.println("Response: " + EntityUtils.toString(entity)); } else { System.out.println("No response content"); } } finally { response.close(); } } public static void main(String[] args) throws Exception { WechatMessageSender sender = new WechatMessageSender(); String message = "Hello from Java!"; sender.sendMessage(message); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值