搜尋
首頁php教程php手册微信公众号开发之微信公共平台消息回复类实例

这篇文章主要介绍了微信公众号开发之微信公共平台消息回复类,给出了其完整实例,并附有注释说明便于理解与运用,对于微信公众号的开发来说非常具有参考借鉴价值,需

本文实例讲述了微信公众号开发之微信公共平台消息回复类。分享给大家供大家参考。具体如下:

微信公众号开发代码我在网上看到了有不少,其实都是大同小义了都是参考官方给出的demo文件进行修改的,,这里就给各位分享一个。

复制代码 代码如下:


/**
 * 微信公共平台消息回复类
 *
 *
 */
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="
     
     
     
     
     
";
     $newTplFoot="

      %s
     
";
     $Content='';
     $itemsCount=count($newsData);
     $itemsCount=$itemsCount      if($itemsCount){
      foreach($newsData as $key=>$item){
       if($key       $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();
 }
 
 /*
  *音乐消息回复
  *@param array object
  *@param array musicContent 二维数组 包含[Title][Description][MusicUrl][HQMusicUrl]字段
  *@return string
  */
 public function resMusic($object,$musicContent=array()){
   $xmlMusic="
                   
                   
                    %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();
 }
 /*
  *上传多媒体文件接口
  *@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,谷歌坐标转换成百度坐标
  *@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;
 }
 
 /**************************************************************
  *
  *  使用特定function对数组中所有元素做处理
  *  @param  string  &$array     要处理的字符串
  *  @param  string  $function   要执行的函数
  *  @return boolean $apply_to_keys_also     是否也应用到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--;
 }
 
 /**************************************************************
  *
  *  将数组转换为JSON字符串(兼容中文)
  *  @param  array   $array      要转换的数组
  *  @return string      转换得到的json字符串
  *  @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的微信公众号开发有所帮助。

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),