Public Account development-public platform message reply instance, public instance
This article describes the message reply class of the public platform developed by the public account. 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.
Copy codeThe 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 = "<xml>
<ToUserName> <! [CDATA [% s]> </ToUserName>
<FromUserName> <! [CDATA [% s]> </FromUserName>
<CreateTime> % s </CreateTime>
<MsgType> <! [CDATA [text]> </MsgType>
<Content> <! [CDATA [% s]> </Content>
<FuncFlag> % d </FuncFlag>
</Xml> ";
$ 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 = "<xml> ";
$ XmlImage. = "<ToUserName> <! [CDATA [% s]> </ToUserName> ";
$ XmlImage. = "<FromUserName> <! [CDATA [% s]> </FromUserName> ";
$ XmlImage. = "<CreateTime> % s </CreateTime> ";
$ XmlImage. = "<MsgType> <! [CDATA [image]> </MsgType> ";
$ XmlImage. = "<Image> <MediaId> <! [CDATA [% s]> </MediaId> </Image> ";
$ XmlImage. = "</xml> ";
$ 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 = "<xml>
<ToUserName> <! [CDATA [{$ object-> FromUserName}]> </ToUserName>
<FromUserName> <! [CDATA [{$ object-> ToUserName}]> </FromUserName>
<CreateTime >{$ CreateTime} </CreateTime>
<MsgType> <! [CDATA [news]> </MsgType>
<Content> <! [CDATA [% s]> </Content>
<ArticleCount> % s </ArticleCount> <Articles> ";
$ NewTplItem = "<item>
<Title> <! [CDATA [% s]> </Title>
<Description> <! [CDATA [% s]> </Description>
<PicUrl> <! [CDATA [% s]> </PicUrl>
<Url> <! [CDATA [% s]> </Url>
</Item> ";
$ NewTplFoot = "</Articles>
<FuncFlag> % s </FuncFlag>
</Xml> ";
$ 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 = "<xml>
<ToUserName> <! [CDATA [% s]> </ToUserName>
<FromUserName> <! [CDATA [% s]> </FromUserName>
<CreateTime> % s </CreateTime>
<MsgType> <! [CDATA [music]> </MsgType>
<Music>
<Title> <! [CDATA [% s]> </Title>
<Description> <! [CDATA [% s]> </Description>
<MusicUrl> <! [CDATA [% s]> </MusicUrl>
<HQMusicUrl> <! [CDATA [% s]> </HQMusicUrl>
</Music>
</Xml> ";
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.