php专区

 首页 > php专区 > PHP应用 > php类库 > 微信公众号开发之微信公共平台消息回复类

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

分享到:
【字体:
导读:
          微信公众号开发代码我在网上看到了有不少,其实都是大同小义了都是参考官方给出的demo文件进行修改的,下面给各位分享一个. 初次接触的时候写的一些,有点乱也没去整理最近都不想工...

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

初次接触的时候写的一些,有点乱…也没去整理…最近都不想工作了,各种烦,午饭也没吃,就是想表达一下我过的不好,–请忽略个人情绪往下看,代码如下:

  1. /** 
  2.  * 微信公共平台消息回复类 
  3.  * 
  4.  * 
  5.  */ 
  6. class BBCweixin{ 
  7.  
  8.  private $APPID="******"
  9.  private $APPSECRET="******"
  10.  /* 
  11.   *文本消息回复 
  12.   *@param array object 
  13.   *@param string content 
  14.   *@return string 
  15.   */ 
  16.  public function resText($object,$content,$flag=0){ 
  17.   $xmlText=" 
  18.                    
  19.                    
  20.                   %s 
  21.                    
  22.                    
  23.                   %d 
  24.                   "; 
  25.      $resultStr=sprintf($xmlText,$object->FromUserName,$object->ToUserName,time(),$content,$flag); 
  26.   echo $resultStr;exit(); 
  27.  } 
  28.  /* 
  29.   *图片消息回复 
  30.   *@param array object 
  31.   *@param string url 
  32.   *@return string 
  33.   */ 
  34.  public function resImage($object,$media_id){ 
  35.   $xmlImage=""
  36.   $xmlImage.=""
  37.   $xmlImage.=""
  38.   $xmlImage.="%s"
  39.   $xmlImage.=""
  40.   $xmlImage.=""
  41.   $xmlImage.=""
  42.   $resultStr=sprintf($xmlImage,$object->FromUserName,$object->ToUserName,time(),$media_id); 
  43.   echo $resultStr;exit(); 
  44.  } 
  45.  /* 
  46.   *图文消息回复 
  47.   *@param array object 
  48.   *@param array newsData 二维数组 必须包含[Title][Description][PicUrl][Url]字段 
  49.   *@return string 
  50.   */ 
  51.  public function resNews($object,$newsData=array()){ 
  52.      $CreateTime=time(); 
  53.      $FuncFlag=0; 
  54.      $newTplHeader=" 
  55.         $object->FromUserName}]]> 
  56.         $object->ToUserName}]]> 
  57.         {$CreateTime} 
  58.          
  59.          
  60.         %s"; 
  61.      $newTplItem=" 
  62.       <![CDATA[%s]]> 
  63.        
  64.        
  65.        
  66.       "; 
  67.      $newTplFoot=" 
  68.       %s 
  69.       "; 
  70.      $Content=''
  71.      $itemsCount=count($newsData); 
  72.      $itemsCount=$itemsCount<10?$itemsCount:10;//微信公众平台图文回复的消息一次最多10条 
  73.      if($itemsCount){ 
  74.       foreach($newsData as $key=>$item){ 
  75.        if($key<=9){ 
  76.       $Content.=sprintf($newTplItem,$item['Title'],$item['Description'],$item['PicUrl'],$item['Url']); 
  77.     } 
  78.       } 
  79.   } 
  80.      $header=sprintf($newTplHeader,0,$itemsCount); 
  81.      $footer=sprintf($newTplFoot,$FuncFlag); 
  82.      echo $header.$Content.$footer;exit(); 
  83.  } 
  84.  
  85.  /* 
  86.   *音乐消息回复 
  87.   *@param array object 
  88.   *@param array musicContent 二维数组 包含[Title][Description][MusicUrl][HQMusicUrl]字段 
  89.   *@return string 
  90.   */ 
  91.  public function resMusic($object,$musicContent=array()){ 
  92.    $xmlMusic=" 
  93.                      
  94.                      
  95.                     %s 
  96.                      
  97.                      
  98.      <![CDATA[%s]]> 
  99.                      
  100.                      
  101.                      
  102.                      
  103.                     "; 
  104.   if(emptyempty($musicContent[0]['HQMusicUrl'])){ 
  105.    $musicContent[0]['HQMusicUrl']=$musicContent[0]['MusicUrl']; 
  106.   } 
  107.   $resultStr=sprintf($xmlMusic,$object->FromUserName,$object->ToUserName,time(),$musicContent[0]['Title'],$musicContent[0]['Description'],$musicContent[0]['MusicUrl'],$musicContent[0]['HQMusicUrl']); 
  108.   echo $resultStr;exit(); 
  109.  } 
  110.  /* 
  111.   *上传多媒体文件接口 
  112.   *@param  
  113.   *@param array mediaArr filename、filelength、content-type 
  114.   *@return object 
  115.   */ 
  116.  public function uploadMedia($accessToken,$type='image',$mediaArr){ 
  117.   $url="http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$accessToken."&type=".$type
  118.   $doPost=self::curlPost($mediaArr,$url); 
  119.   return $doPost
  120.  } 
  121.  /* 
  122.   *GPS,谷歌坐标转换成百度坐标 
  123.   *@param lnt 
  124.   *@param lat 
  125.   *@return array 
  126.   */ 
  127.  public function mapApi($lng,$lat,$type){ 
  128.   $map=array(); 
  129.   if($type=='gps'){ 
  130.    $url="http://map.yanue.net/gpsApi.php?lat=".$lat."&lng=".$lng
  131.    $res=json_decode(file_get_contents($url)); 
  132.    $map['lng']=$res->baidu->lng; 
  133.    $map['lat']=$res->baidu->lat; 
  134.   } 
  135.   if($type=='google'){ 
  136.    $url="http://api.map.baidu.com/ag/coord/convert?from=2&to=4&mode=1&x=".$lng."&y=".$lat
  137.    $res=json_decode(file_get_contents($url)); 
  138.    $map['lng']=base64_decode($res[0]->x); 
  139.    $map['lat']=base64_decode($res[0]->y); 
  140.   } 
  141.   return $map
  142.  } 
  143.  
  144.  /************************************************************** 
  145.   * 
  146.   *  使用特定function对数组中所有元素做处理 
  147.   *  @param  string  &$array     要处理的字符串 
  148.   *  @param  string  $function   要执行的函数 
  149.   *  @return boolean $apply_to_keys_also     是否也应用到key上 
  150.   *  @access public 
  151.   * 
  152.   *************************************************************/ 
  153.  public function arrayRecursive(&$array$function$apply_to_keys_also = false) 
  154.  { 
  155.   static $recursive_counter = 0; 
  156.   if (++$recursive_counter > 1000) { 
  157.    die('possible deep recursion attack'); 
  158.   } 
  159.   foreach ($array as $key => $value) { 
  160.    if (is_array($value)) { 
  161.     self::arrayRecursive($array[$key], $function$apply_to_keys_also); 
  162.    } else { 
  163.     $array[$key] = $function($value); 
  164.    } 
  165.  
  166.    if ($apply_to_keys_also && is_string($key)) { 
  167.     $new_key = $function($key); 
  168.     if ($new_key != $key) { 
  169.      $array[$new_key] = $array[$key]; 
  170.      unset($array[$key]); 
  171.     } 
  172.    } 
  173.   } 
  174.   $recursive_counter--; 
  175.  } 
  176.  
  177.  /************************************************************** 
  178.   * 
  179.   *  将数组转换为JSON字符串(兼容中文) 
  180.   *  @param  array   $array      要转换的数组 
  181.   *  @return string      转换得到的json字符串 
  182.   *  @access public 
  183.   * 
  184.   *************************************************************/ 
  185.  public function JSON($array) { 
  186.   self::arrayRecursive($array'urlencode', true); 
  187.   $json = json_encode($array); 
  188.   return urldecode($json); 
  189.  } 
  190.  /* 
  191.   *创建菜单 
  192.   * 
  193.   */ 
  194.  public function creatMenu($shop_id,$data){ 
  195.   $jsonArray=self::JSON($data); 
  196.   $AccessToken=self::accessToken($weiXin[0]['key'],$weiXin[0]['secret']); 
  197.   $MENU_URL="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$AccessToken
  198.   return self::curlPost($jsonArray,$MENU_URL); 
  199.  } 
  200.  /* 
  201.   *客服消息回复 
  202.   *@param array jsonArray Array {"touser":"OPENID","msgtype":"text","text":{"content":"Hello World"}} 
  203.   *@return string 
  204.   */ 
  205.  
  206.   public function customService($jsonArray,$hash){ 
  207.   if(emptyempty($jsonArray)){ 
  208.    return false;  
  209.   } 
  210.   $db=M(); 
  211.   $sql="select * from bbc_wechats where hash='".$hash."'"
  212.   $weChast=$db->query($sql); 
  213.   $AccessToken=self::accessToken($weChast[0]['key'],$weChast[0]['secret']); 
  214.   $TokenUrl="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$AccessToken
  215.      $CustomRes=self::curlPost($jsonArray,$TokenUrl); 
  216.   return $CustomRes
  217.   } 
  218.   /* 
  219.  
  220.    *获取access_token 
  221.    *@return objectStr 
  222.    */ 
  223.   public function accessToken($appid,$secret){  
  224.    $access_token=BBCcache::getCache('accesstoken'.$appid); 
  225.    if($access_token){ 
  226.     $AccessTokenRet=$access_token
  227.    }else
  228.     $TookenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}"
  229.     $AccessTokenRes=@file_get_contents($TookenUrl); 
  230.     $AccessToken=json_decode($AccessTokenRes); 
  231.     $AccessTokenRet=$AccessToken->access_token; 
  232.     BBCcache::setCache('accesstoken'.$appid,$AccessToken->access_token,3600); 
  233.    } 
  234.    return $AccessTokenRet
  235.   } 
  236.   /* 
  237.    *向远程接口POST数据 
  238.    *@data Array {"touser":"OPENID","msgtype":"text","text":{"content":"Hello World"}} 
  239.    *@return objectArray 
  240.    */ 
  241.   public function curlPost($data,$url){ 
  242.     $ch = curl_init(); 
  243.  
  244.    curl_setopt($ch, CURLOPT_URL, $url);  
  245.    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
  246.    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  
  247.    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
  248.    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); 
  249.    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
  250.    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);  
  251.    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
  252.    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  253.  
  254.    $info = curl_exec($ch); 
  255.  
  256.    if (curl_errno($ch)) { 
  257.     echo 'Errno'.curl_error($ch); 
  258.    } 
  259.  
  260.    curl_close($ch); 
  261.    return json_decode($info); 
  262.   } 
  263.  //根据经纬度计算距离和方向 
  264.  function getRadian($d){ 
  265.   return $d * M_PI / 180; 
  266.  } 
  267.  
  268.  function getDistance ($lat1$lng1$lat2$lng2){ 
  269.   $EARTH_RADIUS=6378.137;//地球半径 
  270.   $lat1 =getRadian($lat1); 
  271.   $lat2 = getRadian($lat2); 
  272.  
  273.   $a = $lat1 - $lat2
  274.   $b = getRadian($lng1) - getRadian($lng2); 
  275.  
  276.   $v = 2 * asin(sqrt(pow(sin($a/2),2) + cos($lat1) * cos($lat2) * pow(sin($b/2),2)));//开源软件:phpfensi.com 
  277.  
  278.   $v = round($EARTH_RADIUS * $v * 10000) / 10000; 
  279.  
  280.   return $v
  281.  } 
  282.  
  283.  
  284. ?>
  285.  
分享到:
PHP系统异常处理类程序
以前我们用过的异常处理函数都是单个的,下面我找到一个非常的不错的异常处理类系统,不但可以控制错误还能给出好的界面哦. PHP系统异常处理类程序代码如下:
实用简单的mysql数据库连接类
class DB  {   //database connection   var $con = FALSE;     function DB($MYSQL_HOST=MYSQL_HOST, $MYSQL_USER=MYSQL_USER, $MYSQL_PASS=MYSQL_PASS,$MYSQL_DB=MYSQL_DB)   {    $this->con = @mysql_connect($MYSQL_HOST, $M...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……