您好,欢迎访问一九零五行业门户网

微信公众号开发之微信公共平台消息回复类实例

这篇文章主要介绍了微信公众号开发之微信公共平台消息回复类,给出了其完整实例,并附有注释说明便于理解与运用,对于微信公众号的开发来说非常具有参考借鉴价值,需
本文实例讲述了微信公众号开发之微信公共平台消息回复类。分享给大家供大家参考。具体如下:
微信公众号开发代码我在网上看到了有不少,其实都是大同小义了都是参考官方给出的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的微信公众号开发有所帮助。
其它类似信息

推荐信息