WeChat public development WeChat common platform message reply class instance, public instance _php tutorial

Source: Internet
Author: User
Tags cos openid sprintf

Public Platform for public development message reply class instance, public instance


In this paper, the Public platform message reply class of public number development is described. Share to everyone for your reference. Specific as follows:

Public number development code I saw on the internet there are many, in fact, are all Datong small righteousness is to refer to the official demo file for modification, here to share one.

Copy CodeThe code is as follows:
<?php
/**
* Public platform message reply class
*
*
*/
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 ();
}
/*
* Picture 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 Two-dimensional array must contain [Title][description][picurl][url] fields
* @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;//public Platform message reply up to 10 messages 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 musiccontent Two-dimensional array containing [title][description][musicurl][hqmusicurl] fields
* @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 ();
}
/*
* 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;
}

/**************************************************************
*
* Processing of all elements in an array using 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 Array to JSON string (Chinese compatible)
* @param array $array to convert
* JSON string to be converted @return 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 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);
}
Calculate distance and direction based on 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;
}
}
?>

It is hoped that this article will help you to develop PHP-based public numbers.

http://www.bkjia.com/PHPjc/911900.html www.bkjia.com true http://www.bkjia.com/PHPjc/911900.html techarticle Public Platform for public development information reply class instance, public example this paper narrates the public platform message reply class of public number development. Share to everyone ...

  • 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.