Micro-credit public number development's micro-trust platform message reply class instance _php skill

Source: Internet
Author: User
Tags cdata cos openid pow sin sprintf

This paper describes the micro-credit public platform message reply class of micro-credit development. Share to everyone for your reference. Specifically as follows:

Micro-Credit development code I saw a lot on the internet, in fact, are all Datong small righteousness is to refer to the official demo file to modify, here to share one.

Copy Code code as follows:

<?php
/**
* Micro-Credit public platform message reply class
*
*
*/
Class bbcweixin{

Private $APPID = "Hu Jintao";
Private $APPSECRET = "Hu Jintao";
/*
* 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 ();
}
/*
* Picture 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 Two-dimensional array must contain [Title][description][picurl][url] fields
* @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: Message from 10;//public platform a maximum of 10 records
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 Musiccontent two-dimensional array contains [Title][description][musicurl][hqmusicurl] fields
* @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>
</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 ();
}
/*
* Upload Multimedia file interface
* @param
* @param array Mediaarr filename, filelength, Content-type
* @return Object
*/
Public Function Uploadmedia ($accessToken, $type = ' image ', $MEDIAARR) {
$url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=". $accessToken. " &type= ". $type;
$doPost =self::curlpost ($MEDIAARR, $url);
return $doPost;
}
/*
*gps, Google coordinates converted into Baidu 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;
}

/**************************************************************
*
* Handle all elements in the array with a specific function
* @param string & $array strings to be processed
* @param string $function the function to execute
* @return Boolean $apply _to_keys_also 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)
* @param array $array arrays to convert
* @return string converted to a JSON string
* @access Public
*
*************************************************************/
Public Function JSON ($array) {
Self::arrayrecursive ($array, ' UrlEncode ', true);
$json = Json_encode ($array);
Return UrlDecode ($json);
}
/*
* Create 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 message reply
* @param array Jsonarray array {"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 (' Accesstoken '. $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 (' Accesstoken '. $appid, $AccessToken->access_token,3600);
}
return $AccessTokenRet;
}
/*
* Post data to 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);
}
Calculation of distance and direction according to latitude and longitude
function Getradian ($d) {
return $d * M_PI/180;
}

function Getdistance ($lat 1, $lng 1, $lat 2, $lng 2) {
$EARTH _radius=6378.137;//Earth Radius
$lat 1 =getradian ($lat 1);
$lat 2 = Getradian ($lat 2);

$a = $lat 1-$lat 2;
$b = Getradian ($lng 1)-Getradian ($lng 2);

$v = 2 * ASIN (SQRT (POW) (sin ($a/2), 2) + cos ($lat 1) * cos ($lat 2) * POW (sin ($b/2), 2));

$v = Round ($EARTH _radius * $v * 10000)/10000;

return $v;
}
}
?>

I hope this article will help you with the development of micro-public number based on PHP.

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.