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

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

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

  1. <?php
  2. /**
  3. * 微信公共平台消息回复类
  4. *
  5. *
  6. */
  7. class BBCweixin{
  8. private $APP;
  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="<xml>
  18. <ToUserName><![CDATA[%s]]></ToUserName>
  19. <FromUserName><![CDATA[%s]]></FromUserName>
  20. <CreateTime>%s</CreateTime>
  21. <MsgType><![CDATA[text]]></MsgType>
  22. <Content><![CDATA[%s]]></Content>
  23. <FuncFlag>%d</FuncFlag>
  24. </xml>";
  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="<xml>";
  36. $xmlImage.="<ToUserName><![CDATA[%s]]></ToUserName>";
  37. $xmlImage.="<FromUserName><![CDATA[%s]]></FromUserName>";
  38. $xmlImage.="<CreateTime>%s</CreateTime>";
  39. $xmlImage.="<MsgType><![CDATA[image]]></MsgType>";
  40. $xmlImage.="<Image><MediaId><![CDATA[%s]]></MediaId></Image>";
  41. $xmlImage.="</xml>";
  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="<xml>
  55. <ToUserName><![CDATA[{$object->FromUserName}]]></ToUserName>
  56. <FromUserName><![CDATA[{$object->ToUserName}]]></FromUserName>
  57. <CreateTime>{$CreateTime}</CreateTime>
  58. <MsgType><![CDATA[news]]></MsgType>
  59. <Content><![CDATA[%s]]></Content>
  60. <ArticleCount>%s</ArticleCount><Articles>";
  61. $newTplItem="<item>
  62. <Title><![CDATA[%s]]></Title>
  63. <Description><![CDATA[%s]]></Description>
  64. <PicUrl><![CDATA[%s]]></PicUrl>
  65. <Url><![CDATA[%s]]></Url>
  66. </item>";
  67. $newTplFoot="</Articles>
  68. <FuncFlag>%s</FuncFlag>
  69. </xml>";
  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. *@param array object
  87. *@param array musicContent 二维数组 包含[Title][Description][MusicUrl][HQMusicUrl]字段
  88. *@return string
  89. */
  90. public function resMusic($object,$musicContent=array()){
  91. $xmlMusic="<xml>
  92. <ToUserName><![CDATA[%s]]></ToUserName>
  93. <FromUserName><![CDATA[%s]]></FromUserName>
  94. <CreateTime>%s</CreateTime>
  95. <MsgType><![CDATA[music]]></MsgType>
  96. <Music>
  97. <Title><![CDATA[%s]]></Title>
  98. <Description><![CDATA[%s]]></Description>
  99. <MusicUrl><![CDATA[%s]]></MusicUrl>
  100. <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
  101. </Music>
  102. </xml>";
  103. if(emptyempty($musicContent[0]['HQMusicUrl'])){
  104. $musicContent[0]['HQMusicUrl']=$musicContent[0]['MusicUrl'];
  105. }
  106. $resultStr=sprintf($xmlMusic,$object->FromUserName,$object->ToUserName,time(),$musicContent[0]['Title'],$musicContent[0]['Description'],$musicContent[0]['MusicUrl'],$musicContent[0]['HQMusicUrl']);
  107. echo $resultStr;exit();
  108. }
  109. /*
  110. *上传多媒体文件接口
  111. *@param
  112. *@param array mediaArr filename、filelength、content-type
  113. *@return object
  114. */
  115. public function uploadMedia($accessToken,$type='image',$mediaArr){
  116. $url="http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$accessToken."&type=".$type;
  117. $doPost=self::curlPost($mediaArr,$url);
  118. return $doPost;
  119. }
  120. /*
  121. *GPS,谷歌坐标转换成百度坐标
  122. *@param lnt
  123. *@param lat
  124. *@return array
  125. */
  126. public function mapApi($lng,$lat,$type){
  127. $map=array();
  128. if($type=='gps'){
  129. $url="http://map.yanue.net/gpsApi.php?lat=".$lat."&lng=".$lng;
  130. $res=json_decode(file_get_contents($url));
  131. $map['lng']=$res->baidu->lng;
  132. $map['lat']=$res->baidu->lat;
  133. }
  134. if($type=='google'){
  135. $url="http://api.map.baidu.com/ag/coord/convert?from=2&to=4&mode=1&x=".$lng."&y=".$lat;
  136. $res=json_decode(file_get_contents($url));
  137. $map['lng']=base64_decode($res[0]->x);
  138. $map['lat']=base64_decode($res[0]->y);
  139. }
  140. return $map;
  141. }
  142. /**************************************************************
  143. *
  144. * 使用特定function对数组中所有元素做处理
  145. * @param string &$array 要处理的字符串
  146. * @param string $function 要执行的函数
  147. * @return boolean $apply_to_keys_also 是否也应用到key上
  148. * @access public
  149. *
  150. *************************************************************/
  151. public function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
  152. {
  153. static $recursive_counter = 0;
  154. if (++$recursive_counter > 1000) {
  155. die('possible deep recursion attack');
  156. }
  157. foreach ($array as $key => $value) {
  158. if (is_array($value)) {
  159. self::arrayRecursive($array[$key], $function, $apply_to_keys_also);
  160. } else {
  161. $array[$key] = $function($value);
  162. }
  163. if ($apply_to_keys_also && is_string($key)) {
  164. $new_key = $function($key);
  165. if ($new_key != $key) {
  166. $array[$new_key] = $array[$key];
  167. unset($array[$key]);
  168. }
  169. }
  170. }
  171. $recursive_counter--;
  172. }
  173. /**************************************************************
  174. *
  175. * 将数组转换为JSON字符串(兼容中文)
  176. * @param array $array 要转换的数组
  177. * @return string 转换得到的json字符串
  178. * @access public
  179. *
  180. *************************************************************/
  181. public function JSON($array) {
  182. self::arrayRecursive($array, 'urlencode', true);
  183. $json = json_encode($array);
  184. return urldecode($json);
  185. }
  186. /*
  187. *创建菜单
  188. *
  189. */
  190. public function creatMenu($shop_id,$data){
  191. $jsonArray=self::JSON($data);
  192. $AccessToken=self::accessToken($weiXin[0]['key'],$weiXin[0]['secret']);
  193. $MENU_URL="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$AccessToken;
  194. return self::curlPost($jsonArray,$MENU_URL);
  195. }
  196. /*
  197. *客服消息回复
  198. *@param array jsonArray Array {"touser":"OPENID","msgtype":"text","text":{"content":"Hello World"}}
  199. *@return string
  200. */
  201. public function customService($jsonArray,$hash){
  202. if(emptyempty($jsonArray)){
  203. return false;
  204. }
  205. $db=M();
  206. $sql="select * from bbc_wechats where hash='".$hash."'";
  207. $weChast=$db->query($sql);
  208. $AccessToken=self::accessToken($weChast[0]['key'],$weChast[0]['secret']);
  209. $TokenUrl="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$AccessToken;
  210. $CustomRes=self::curlPost($jsonArray,$TokenUrl);
  211. return $CustomRes;
  212. }
  213. /*
  214. *获取access_token
  215. *@return objectStr
  216. */
  217. public function accessToken($appid,$secret){
  218. $access_token=BBCcache::getCache('accesstoken'.$appid);
  219. if($access_token){
  220. $AccessTokenRet=$access_token;
  221. }else{
  222. $TookenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&app;
  223. $AccessTokenRes=@file_get_contents($TookenUrl);
  224. $AccessToken=json_decode($AccessTokenRes);
  225. $AccessTokenRet=$AccessToken->access_token;
  226. BBCcache::setCache('accesstoken'.$appid,$AccessToken->access_token,3600);
  227. }
  228. return $AccessTokenRet;
  229. }
  230. /*
  231. *向远程接口POST数据
  232. *@data Array {"touser":"OPENID","msgtype":"text","text":{"content":"Hello World"}}
  233. *@return objectArray
  234. */
  235. public function curlPost($data,$url){
  236. $ch = curl_init();
  237. curl_setopt($ch, CURLOPT_URL, $url);
  238. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  239. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  240. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  241. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  242. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  243. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  244. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  245. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  246. $info = curl_exec($ch);
  247. if (curl_errno($ch)) {
  248. echo 'Errno'.curl_error($ch);
  249. }
  250. curl_close($ch);
  251. return json_decode($info);
  252. }
  253. //根据经纬度计算距离和方向
  254. function getRadian($d){
  255. return $d * M_PI / 180;
  256. }
  257. function getDistance ($lat1, $lng1, $lat2, $lng2){
  258. $EARTH_RADIUS=6378.137;//地球半径
  259. $lat1 =getRadian($lat1);
  260. $lat2 = getRadian($lat2);
  261. $a = $lat1 - $lat2;
  262. $b = getRadian($lng1) - getRadian($lng2);
  263. $v = 2 * asin(sqrt(pow(sin($a/2),2) + cos($lat1) * cos($lat2) * pow(sin($b/2),2)));//开源软件:phpfensi.com
  264. $v = round($EARTH_RADIUS * $v * 10000) / 10000;
  265. return $v;
  266. }
  267. }
  268. ?>