php调用微信客服消息接口给用户发送信息

$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);

转载于:https://www.cnblogs.com/bilibiliganbei/p/7490886.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值