How PHP implements the sending of template messages
This article mainly introduces PHP implementation of the method of sending template messages, examples of PHP operation Curl and custom template message implementation skills, with a certain reference value, the need for friends can refer to the next
The example in this paper describes how PHP implements sending template messages. Share to everyone for your reference. Specific as follows:
This method is implemented based on thinkphp, and the specific OrderPush.class.php file is as follows:
The code is as follows:
namespace Org\weixin;
/**
* 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); Crawl specified Web page
curl_setopt ($ch, Curlopt_header, 0); Set Header
curl_setopt ($ch, Curlopt_returntransfer, 1); Request result As String and output to 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 a 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 custom 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 Success"), ' color ' = ' #743A3A '),
' Name ' =>array (' Value ' =>urlencode ("Product info: Micro-era movie ticket"), ' color ' = ' #EEEEEE '),
' Remark ' =>array (' Value ' =>urlencode (' permanently valid! Password: 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 is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/963966.html www.bkjia.com true http://www.bkjia.com/PHPjc/963966.html techarticle PHP Implementation method for sending template messages this article mainly introduces the PHP implementation of the method of sending template messages, examples of PHP operation Curl and custom template message implementation skills ...