This article mainly introduces the message reply class of the public platform developed by the public account, provides its complete example, and provides annotations to facilitate understanding and application, it is of great reference value for the development of public accounts. if you need it, you can refer to the example in this article to describe the message reply class of public platforms developed by public accounts. Share it with you for your reference. The details are as follows:
I have seen a lot of public account development code on the internet. In fact, all of them are Datong Xiaoyi. they all refer to the official demo file for modification. here I will share with you one.
The code is as follows:
<? Php
/**
* Public platform Message Reply
*
*
*/
Class BBCweixin {
Private $ APPID = "******";
Private $ APPSECRET = "******";
/*
* Text message reply
* @ Param array object
* @ Param string content
* @ Return string
*/
Public function resText ($ object, $ content, $ flag = 0 ){
$ XmlText ="
%s
%s
% S
text
%s
% D
";
$ ResultStr = sprintf ($ xmlText, $ object-> FromUserName, $ object-> ToUserName, time (), $ content, $ flag );
Echo $ resultStr; exit ();
}
/*
* Image Message Reply
* @ Param array object
* @ Param string url
* @ Return string
*/
Public function resImage ($ object, $ media_id ){
$ XmlImage =" ";
$ XmlImage. =" %s ";
$ XmlImage. =" %s ";
$ XmlImage. =" % S ";
$ XmlImage. =" image ";
$ XmlImage. =" %s ";
$ XmlImage. =" ";
$ ResultStr = sprintf ($ xmlImage, $ object-> FromUserName, $ object-> ToUserName, time (), $ media_id );
Echo $ resultStr; exit ();
}
/*
* Text message reply
* @ Param array object
* @ Param array newsData the two-dimensional array must contain the [Title] [Description] [PicUrl] [Url] Field
* @ Return string
*/
Public function resNews ($ object, $ newsData = array ()){
$ CreateTime = time ();
$ FuncFlag = 0;
$ NewTplHeader ="
{$object->FromUserName}
{$object->ToUserName}
{$ CreateTime}
news
%s
% S";
$ NewTplItem ="
<! [CDATA [% s]>
%s
%s
%s
";
$ NewTplFoot ="
% S
";
$ Content = '';
$ ItemsCount = count ($ newsData );
$ ItemsCount = $ itemsCount <10? $ ItemsCount: 10; // A maximum of 10 messages can be replied to by text on the public platform at a time.
If ($ itemsCount ){
Foreach ($ newsData as $ key => $ item ){
If ($ key <= 9 ){
$ Content. = sprintf ($ newTplItem, $ item ['title'], $ item ['description'], $ item ['picurl'], $ item ['URL']);
}
}
}
$ Header = sprintf ($ newTplHeader, 0, $ itemsCount );
$ Footer = sprintf ($ newTplFoot, $ FuncFlag );
Echo $ header. $ Content. $ footer; exit ();
}
/*
* Music message Reply
* @ Param array object
* @ Param array the musicContent two-dimensional array contains the [Title] [Description] [MusicUrl] [HQMusicUrl] Field
* @ Return string
*/
Public function resMusic ($ object, $ musicContent = array ()){
$ XmlMusic ="
%s
%s
% S
music
<! [CDATA [% s]>
%s
%s
%s
";
If (empty ($ musicContent [0] ['hqmusicurl']) {
$ MusicContent [0] ['hqmusicurl'] = $ musicContent [0] ['musicurl'];
}
$ ResultStr = sprintf ($ xmlMusic, $ object-> FromUserName, $ object-> ToUserName, time (), $ musicContent [0] ['title'], $ musicContent [0] ['description'], $ musicContent [0] ['musicurl'], $ musicContent [0] ['hqmusicurl']);
Echo $ resultStr; exit ();
}
/*
* Multimedia file upload interface
* @ Param
* @ Param array medialist R filename, filelength, and content-type
* @ Return object
*/
Public function uploadMedia ($ accessToken, $ type = 'image', $ mediater ){
$ Url = "http://file.api.weixin.qq.com/cgi-bin/media/upload? Access_token = ". $ accessToken." & type = ". $ type;
$ DoPost = self: curlPost ($ medialist R, $ url );
Return $ doPost;
}
/*
* GPS: Google coordinates to hundreds of coordinates
* @ Param lnt
* @ Param lat
* @ Return array
*/
Public function mapApi ($ lng, $ lat, $ type ){
$ Map = array ();
If ($ type = 'GPS '){
$ Url = "http://map.yanue.net/gpsApi.php? Lat = ". $ lat." & lng = ". $ lng;
$ Res = json_decode (file_get_contents ($ url ));
$ Map ['lng '] = $ res-> baidu-> lng;
$ Map ['lat'] = $ res-> baidu-> lat;
}
If ($ type = 'Google '){
$ Url = "http://api.map.baidu.com/ag/coord/convert? From = 2 & to = 4 & mode = 1 & x = ". $ lng." & y = ". $ lat;
$ Res = json_decode (file_get_contents ($ url ));
$ Map ['lng '] = base64_decode ($ res [0]-> x );
$ Map ['lat'] = base64_decode ($ res [0]-> y );
}
Return $ map;
}
/*************************************** ***********************
*
* Use a specific function to process all elements in the array
* @ Param string & $ array the string to be processed
* @ Param string $ function the function to be executed
* @ Return boolean $ apply_to_keys_also whether it is also applied to the key
* @ Access public
*
**************************************** *********************/
Public function arrayRecursive (& $ array, $ function, $ apply_to_keys_also = false)
{
Static $ recursive_counter = 0;
If (++ $ recursive_counter> 1000 ){
Die ('possible deep recursion attack ');
}
Foreach ($ array as $ key => $ value ){
If (is_array ($ value )){
Self: arrayRecursive ($ array [$ key], $ function, $ apply_to_keys_also );
} Else {
$ Array [$ key] = $ function ($ value );
}
If ($ apply_to_keys_also & is_string ($ key )){
$ New_key = $ function ($ key );
If ($ new_key! = $ Key ){
$ Array [$ new_key] = $ array [$ key];
Unset ($ array [$ key]);
}
}
}
$ Recursive_counter --;
}
/*************************************** ***********************
*
* Convert an array to a JSON string (compatible with Chinese characters)
* @ Param array $ array the array to be converted
* @ Return string the converted json string
* @ Access public
*
**************************************** *********************/
Public function JSON ($ array ){
Self: arrayRecursive ($ array, 'urlencode', true );
$ Json = json_encode ($ array );
Return urldecode ($ json );
}
/*
* Create a menu
*
*/
Public function creatMenu ($ shop_id, $ data ){
$ JsonArray = self: JSON ($ data );
$ AccessToken = self: accessToken ($ weiXin [0] ['key'], $ weiXin [0] ['secret']);
$ MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create? Access_token = ". $ AccessToken;
Return self: curlPost ($ jsonArray, $ MENU_URL );
}
/*
* Customer service Reply
* @ Param array jsonArray {"touser": "OPENID", "msgtype": "text", "text": {"content": "Hello World "}}
* @ Return string
*/
Public function customService ($ jsonArray, $ hash ){
If (empty ($ jsonArray )){
Return false;
}
$ Db = M ();
$ SQL = "select * from bbc_wechats where hash = '". $ hash ."'";
$ WeChast = $ db-> query ($ SQL );
$ AccessToken = self: accessToken ($ weChast [0] ['key'], $ weChast [0] ['secret']);
$ TokenUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send? Access_token = ". $ AccessToken;
$ CustomRes = self: curlPost ($ jsonArray, $ TokenUrl );
Return $ CustomRes;
}
/*
* Get access_token
* @ Return objectStr
*/
Public function accessToken ($ appid, $ secret ){
$ Access_token = BBCcache: getCache ('accesstransferen'. $ appid );
If ($ access_token ){
$ AccessTokenRet = $ access_token;
} Else {
$ TookenUrl = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = {$ appid} & secret = {$ secret }";
$ AccessTokenRes = @ file_get_contents ($ TookenUrl );
$ AccessToken = json_decode ($ AccessTokenRes );
$ AccessTokenRet = $ AccessToken-> access_token;
BBCcache: setCache ('accesstransferen'. $ appid, $ accesstoken-> access_token, 3600 );
}
Return $ AccessTokenRet;
}
/*
* POST data to a remote interface
* @ Data Array {"touser": "OPENID", "msgtype": "text", "text": {"content": "Hello World "}}
* @ Return objectArray
*/
Public function curlPost ($ data, $ url ){
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "POST ");
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, FALSE );
Curl_setopt ($ ch, CURLOPT_USERAGENT, 'mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0 )');
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );
Curl_setopt ($ ch, CURLOPT_AUTOREFERER, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
$ Info = curl_exec ($ ch );
If (curl_errno ($ ch )){
Echo 'errno'. curl_error ($ ch );
}
Curl_close ($ ch );
Return json_decode ($ info );
}
// Calculate the distance and direction based on the longitude and latitude
Function getRadian ($ d ){
Return $ d * M_PI/180;
}
Function getDistance ($ lat1, $ lng1, $ lat2, $ lng2 ){
$ EARTH_RADIUS = 6378.137; // Earth radius
$ Lat1 = getRadian ($ lat1 );
$ Lat2 = getRadian ($ lat2 );
$ A = $ lat1-$ lat2;
$ B = getRadian ($ lng1)-getRadian ($ lng2 );
$ V = 2 * asin (sqrt (pow (sin ($ a/2), 2) + cos ($ lat1) * cos ($ lat2) * pow (sin ($ B/2), 2 )));
$ V = round ($ EARTH_RADIUS * $ v * 10000)/10000;
Return $ v;
}
}
?>
I hope this article will help you develop a public account based on PHP.