当前位置:首页 > PHP教程 > php应用 > 列表

php实现发送微信模板消息的方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-15 22:03:35 浏览: 评论:0 

这篇文章主要介绍了php实现发送微信模板消息的方法,实例分析了php操作curl及自定义模板消息的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了php实现发送微信模板消息的方法。分享给大家供大家参考。具体如下:

该方法基于thinkphp实现实现,具体OrderPush.class.php文件如下:

  1. <?php 
  2. namespace Org\Weixin; 
  3. /** 
  4.  * Created by PhpStorm. 
  5.  * User: StandOpen 
  6.  * Date: 15-1-7 
  7.  * Time: 9:41 
  8.  */ 
  9. class OrderPush 
  10. { 
  11.     protected $appid; 
  12.     protected $secrect; 
  13.     protected $accessToken; 
  14.     function  __construct($appid, $secrect) 
  15.     { 
  16.         $this->appid = $appid; 
  17.         $this->secrect = $secrect; 
  18.         $this->accessToken = $this->getToken($appid, $secrect); 
  19.     } 
  20.     /** 
  21.      * 发送post请求 
  22.      * @param string $url 
  23.      * @param string $param 
  24.      * @return bool|mixed 
  25.      */ 
  26.     function request_post($url = '', $param = '') 
  27.     { 
  28.         if (emptyempty($url) || emptyempty($param)) { 
  29.             return false; 
  30.         } 
  31.         $postUrl = $url; 
  32.         $curlPost = $param; 
  33.         $ch = curl_init(); //初始化curl 
  34.         curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页 
  35.         curl_setopt($ch, CURLOPT_HEADER, 0); //设置header 
  36.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上 
  37.         curl_setopt($ch, CURLOPT_POST, 1); //post提交方式 
  38.         curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); 
  39.         $data = curl_exec($ch); //运行curl 
  40.         curl_close($ch); 
  41.         return $data; 
  42.     } 
  43.     /** 
  44.      * 发送get请求 
  45.      * @param string $url 
  46.      * @return bool|mixed 
  47.      */ 
  48.     function request_get($url = '') 
  49.     { 
  50.         if (emptyempty($url)) { 
  51.             return false; 
  52.         } 
  53.         $ch = curl_init(); 
  54.         curl_setopt($ch, CURLOPT_URL, $url); 
  55.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  56.         $data = curl_exec($ch); 
  57.         curl_close($ch); 
  58.         return $data; 
  59.     } 
  60.     /** 
  61.      * @param $appid 
  62.      * @param $appsecret 
  63.      * @return mixed 
  64.      * 获取token 
  65.      */ 
  66.     protected function getToken($appid, $appsecret) 
  67.     { 
  68.         if (S($appid)) { 
  69.             $access_token = S($appid); 
  70.         } else { 
  71.             $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret; 
  72.             $token = $this->request_get($url); 
  73.             $token = json_decode(stripslashes($token)); 
  74.             $arr = json_decode(json_encode($token), true); 
  75.             $access_token = $arr['access_token']; 
  76.             S($appid, $access_token, 720); 
  77.         } 
  78.         return $access_token; 
  79.     } 
  80.     /** 
  81.      * 发送自定义的模板消息 
  82.      * @param $touser 
  83.      * @param $template_id 
  84.      * @param $url 
  85.      * @param $data 
  86.      * @param string $topcolor 
  87.      * @return bool 
  88.      */ 
  89.     public function doSend($touser, $template_id, $url, $data, $topcolor = '#7B68EE') 
  90.     { 
  91.         /* 
  92.          * data=>array( 
  93.                 'first'=>array('value'=>urlencode("您好,您已购买成功"),'color'=>"#743A3A"), 
  94.                 'name'=>array('value'=>urlencode("商品信息:微时代电影票"),'color'=>'#EEEEEE'), 
  95.                 'remark'=>array('value'=>urlencode('永久有效!密码为:1231313'),'color'=>'#FFFFFF'), 
  96.             ) 
  97.          */ 
  98.         $template = array( 
  99.             'touser' => $touser, 
  100.             'template_id' => $template_id, 
  101.             'url' => $url, 
  102.             'topcolor' => $topcolor, 
  103.             'data' => $data 
  104.         ); 
  105.         $json_template = json_encode($template); 
  106.         $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $this->accessToken; 
  107.         $dataRes = $this->request_post($url, urldecode($json_template)); 
  108.         if ($dataRes['errcode'] == 0) { 
  109.             return true; 
  110.         } else { 
  111.             return false; 
  112.         } 
  113.     } 
  114. }

Tags: php发送微信模板

分享到: