Home  >  Article  >  Backend Development  >  PHP implements the method of sending WeChat template messages, PHP letter template messages_PHP tutorial

PHP implements the method of sending WeChat template messages, PHP letter template messages_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:05:271387browse

php implements the method of sending WeChat template messages, php letter template message

This article describes the example of php implementing the method of sending WeChat template messages. Share it with everyone for your reference. The details are as follows:

This method is implemented based on thinkphp. The specific OrderPush.class.php file is as follows:
Copy code The code is as follows: namespace OrgWeixin;
/**
 * Created by PhpStorm.
 * User: StandOpen
 * Date: 15-1-7
 * Time: 9:41
 */
class OrderPush
{
protected $appid;
protected $secrect;
protected $accessToken;
Function __construct($appid, $secrect)
{
            $this->appid = $appid;
           $this->secrect = $secrect;
$this->accessToken = $this->getToken($appid, $secrect);
}
/**
*Send post request
* @param string $url
* @param string $param
* @return bool|mixed
​​*/
Function request_post($url = '', $param = '')
{
If (empty($url) || empty($param)) {
              return false;
}
$postUrl = $url;
          $curlPost = $param;
​​​​$ch = curl_init(); //Initialize curl
​​​​ curl_setopt($ch, CURLOPT_URL, $postUrl); //Catch the specified web page
​​​​ curl_setopt($ch, CURLOPT_HEADER, 0); //Set header
​​​​ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Require the result to be a string and output it to the screen
​​​​ curl_setopt($ch, CURLOPT_POST, 1); //post submission method
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
​​​​ $data = curl_exec($ch); //Run curl
         curl_close($ch);
         return $data;
}
/**
*Send get request
* @param string $url
* @return bool|mixed
​​*/
Function request_get($url = '')
{
            if (empty($url)) {
              return false;
}
          $ch = curl_init();
​​​​ curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          $data = curl_exec($ch);
         curl_close($ch);
         return $data;
}
/**
* @param $appid
* @param $appsecret
* @return mixed
* Get token
​​*/
Protected function getToken($appid, $appsecret)
{
           if (S($appid)) {
               $access_token = S($appid);
         } else {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
$token = $this->request_get($url);
               $token = json_decode(stripslashes($token));
                $arr = json_decode(json_encode($token), true);
               $access_token = $arr['access_token'];
S($appid, $access_token, 720);
}
          return $access_token;
}
/**
*Send customized template message
* @param $touser
* @param $template_id
* @param $url
* @param $data
* @param string $topcolor
* @return bool
​​*/
Public function doSend($touser, $template_id, $url, $data, $topcolor = '#7B68EE')
{
         /*
* data=>array(
                'first'=>array('value'=>urlencode("Hello, you have purchased successfully"),'color'=>"#743A3A"),
                'name'=>array('value'=>urlencode("Product Information: Micro Times Movie Tickets"),'color'=>'#EEEEEE'),
                'remark'=>array('value'=>urlencode('Permanently valid! The password is: 1231313'),'color'=>'#FFFFFF'),
)
*/
         $template = array(
             'touser' => $touser,
              'template_id' => $template_id,
             'url' => $url,
              'topcolor' => $topcolor,
             'data' => $data
);
          $json_template = json_encode($template);
           $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $this->accessToken;
           $dataRes = $this->request_post($url, urldecode($json_template));
If ($dataRes['errcode'] == 0) {
             return true;
         } else {
              return false;
}
}
}

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/963832.htmlTechArticlephp method to implement sending WeChat template message, php letter template message This article describes how php implements sending WeChat template message method. Share it with everyone for your reference. The details are as follows: The...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn