Home >Backend Development >PHP Tutorial >PHP implements the method of sending WeChat template messages, PHP letter template messages_PHP tutorial
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.