PHP implementation of the method of sending micro-message template messages, PHP letter template Message _php Tutorial

Source: Internet
Author: User
Tags urlencode

PHP implementation of the method of sending template messages, PHP letter template message


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:
Copy CodeThe code is 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 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/963832.html www.bkjia.com true http://www.bkjia.com/PHPjc/963832.html techarticle PHP Implementation of the method of sending template messages, PHP letter template Message This article describes the PHP implementation of the method of sending template messages. Share to everyone for your reference. Specific as follows: the ...

  • Related Article

    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.