This article illustrates the method of implementing the message of micro-letter template by PHP. Share to everyone for your reference. Specifically as follows:
This method is based on thinkphp implementation, the specific OrderPush.class.php files are as follows:
Copy Code code as follows:
<?php
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 the specified Web page
curl_setopt ($ch, Curlopt_header, 0); Set Header
curl_setopt ($ch, Curlopt_returntransfer, 1); Requires the result to be a string and output 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 a custom 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 (
' =>array ' =>urlencode ("Hello, you have purchased Success"), ' Color ' => ' #743A3A '),
' Name ' =>array (' Product information: =>urlencode 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 will help you with your PHP program design.