zoukankan      html  css  js  c++  java
  • 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);

  • 相关阅读:
    (转)我是怎么治愈鼻窦炎的
    Linq to SQL 资源
    桥牌笔记:忍让几墩?
    读书笔记2013第1本:餐巾纸的背面
    《Two Dozen Short Lessons in Haskell》学习(十五) Encapsulation — modules
    《Two Dozen Short Lessons in Haskell》学习(十三)迭代及重复的常规模式
    读书笔记2013第4本:《上帝掷骰子吗?》
    用Haskell写的卡普雷尔卡kaprekar黑洞小程序
    读书笔记2013第5本:《拖延心理学》
    使用Supermemo背单词6周年了
  • 原文地址:https://www.cnblogs.com/bilibiliganbei/p/7490886.html
Copyright © 2011-2022 走看看