Home > Backend Development > PHP Tutorial > How to implement sending WeChat template messages in PHP_PHP tutorial

How to implement sending WeChat template messages in PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:05:11
Original
1144 people have browsed it

How to implement sending WeChat template messages in PHP

This article mainly introduces the method of sending WeChat template messages in PHP. It analyzes the operation of curl and custom template messages in PHP with examples. Implementation skills have certain reference value. Friends in need can refer to it

The example in this article describes the method of sending WeChat template messages in PHP. 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:

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 messages
* @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/963966.htmlTechArticleHow to send WeChat template messages in php. This article mainly introduces the method of sending WeChat template messages in php, with examples. Analyzed the implementation skills of PHP operating curl and custom template messages...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template