WeChat public platform message reply class example for WeChat public account development, public example_PHP tutorial

WBOY
Release: 2016-07-13 10:14:12
Original
749 people have browsed it

WeChat public platform message reply class example for WeChat public account development, public example

The example in this article describes the WeChat public platform message reply class developed by WeChat public account. Share it with everyone for your reference. The details are as follows:

I have seen a lot of WeChat public account development codes on the Internet. In fact, they all have the same meaning but are modified with reference to the official demo file. I will share one with you here.

Copy code The code is as follows:

/**
* WeChat public platform message reply class
*
*
​*/
class BBCweixin{
 
 private $APPID="******";
 private $APPSECRET="******";
 /*
  *文本消息回复
  *@param array object
  *@param string content
  *@return string
  */
 public function resText($object,$content,$flag=0){
  $xmlText="
                 
                 
                  %s
                 
                 
                  %d
                 
";
     $resultStr=sprintf($xmlText,$object->FromUserName,$object->ToUserName,time(),$content,$flag);
  echo $resultStr;exit();
 }
 /*
  *图片消息回复
  *@param array object
  *@param string url
  *@return string
  */
 public function resImage($object,$media_id){
  $xmlImage="";
  $xmlImage.="";
  $xmlImage.="";
  $xmlImage.="%s";
  $xmlImage.="";
  $xmlImage.="";
  $xmlImage.="
";
  $resultStr=sprintf($xmlImage,$object->FromUserName,$object->ToUserName,time(),$media_id);
  echo $resultStr;exit();
 }
 /*
  *图文消息回复
  *@param array object
  *@param array newsData 二维数组 必须包含[Title][Description][PicUrl][Url]字段
  *@return string
  */
 public function resNews($object,$newsData=array()){
     $CreateTime=time();
     $FuncFlag=0;
     $newTplHeader="
        FromUserName}]]>
        ToUserName}]]>
        {$CreateTime}
       
       
        %s";
     $newTplItem="
      <![CDATA[%s]]>
     
     
     
     
";
     $newTplFoot="

      %s
";
$Content='';
$itemsCount=count($newsData);
$itemsCount=$itemsCount<10?$itemsCount:10;//WeChat public platform can reply to up to 10 messages with pictures and texts 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] field
*@return string
*/
public function resMusic($object,$musicContent=array()){
$xmlMusic="
                                                                                                                                                                                                                           
                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                          %s
                                                                                                                                                                                                                             
                                                                                                                   <![CDATA[%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();
}
/*
*Interface for uploading multimedia files
*@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 to 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;
 }
 
 /***************************************************** **********
*
* 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 to also apply to keys
* @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 (compatible with Chinese)
* @param array $array 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);
 }
 /*
  *创建菜单
  *
  */
 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);
 }
 /*
  *客服消息回复
  *@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;
  }
  /*
 
   *获取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 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);
  }
 //根据经纬度计算距离和方向
 function getRadian($d){
  return $d * M_PI / 180;
 }
 
 function getDistance ($lat1, $lng1, $lat2, $lng2){
  $EARTH_RADIUS=6378.137;//地球半径
  $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;
 }
}
?>

希望本文所述对大家基于PHP的微信公众号开发有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/911900.htmlTechArticle微信公众号开发之微信公共平台消息回复类实例,公众实例 本文实例讲述了微信公众号开发之微信公共平台消息回复类。分享给大家供大家...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template