How to implement the method of sending micro-letter template messages by PHP _php tips

Source: Internet
Author: User
Tags mixed urlencode

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.