qywechat.class.php 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  1. <?php
  2. /**
  3. * 微信公众平台企业号PHP-SDK, 官方API类库
  4. * @author binsee <binsee@163.com>
  5. * @link https://github.com/binsee/wechat-php-sdk
  6. * @version 1.0
  7. * usage:
  8. * $options = array(
  9. * 'token'=>'tokenaccesskey', //填写应用接口的Token
  10. * 'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey
  11. * 'appid'=>'wxdk1234567890', //填写高级调用功能的app id
  12. * 'appsecret'=>'xxxxxxxxxxxxxxxxxxx', //填写高级调用功能的密钥
  13. * 'agentid'=>'1', //应用的id
  14. * 'debug'=>false, //调试开关
  15. * '_logcallback'=>'logg', //调试输出方法,需要有一个string类型的参数
  16. * );
  17. *
  18. */
  19. class Wechat
  20. {
  21. const MSGTYPE_TEXT = 'text';
  22. const MSGTYPE_IMAGE = 'image';
  23. const MSGTYPE_LOCATION = 'location';
  24. const MSGTYPE_LINK = 'link'; //暂不支持
  25. const MSGTYPE_EVENT = 'event';
  26. const MSGTYPE_MUSIC = 'music'; //暂不支持
  27. const MSGTYPE_NEWS = 'news';
  28. const MSGTYPE_VOICE = 'voice';
  29. const MSGTYPE_VIDEO = 'video';
  30. const API_URL_PREFIX = 'https://qyapi.weixin.qq.com/cgi-bin';
  31. const USER_CREATE_URL = '/user/create?';
  32. const USER_UPDATE_URL = '/user/update?';
  33. const USER_DELETE_URL = '/user/delete?';
  34. const USER_GET_URL = '/user/get?';
  35. const USER_LIST_URL = '/user/simplelist?';
  36. const USER_GETINFO_URL = '/user/getuserinfo?';
  37. const DEPARTMENT_CREATE_URL = '/department/create?';
  38. const DEPARTMENT_UPDATE_URL = '/department/update?';
  39. const DEPARTMENT_DELETE_URL = '/department/delete?';
  40. const DEPARTMENT_MOVE_URL = '/department/move?';
  41. const DEPARTMENT_LIST_URL = '/department/list?';
  42. const TAG_CREATE_URL = '/tag/create?';
  43. const TAG_UPDATE_URL = '/tag/update?';
  44. const TAG_DELETE_URL = '/tag/delete?';
  45. const TAG_GET_URL = '/tag/get?';
  46. const TAG_ADDUSER_URL = '/tag/addtagusers?';
  47. const TAG_DELUSER_URL = '/tag/deltagusers?';
  48. const TAG_LIST_URL = '/tag/list?';
  49. const MEDIA_UPLOAD_URL = '/media/upload?';
  50. const MEDIA_GET_URL = '/media/get?';
  51. const AUTHSUCC_URL = '/user/authsucc?';
  52. const MASS_SEND_URL = '/message/send?';
  53. const MENU_CREATE_URL = '/menu/create?';
  54. const MENU_GET_URL = '/menu/get?';
  55. const MENU_DELETE_URL = '/menu/delete?';
  56. const TOKEN_GET_URL = '/gettoken?';
  57. const OAUTH_PREFIX = 'https://open.weixin.qq.com/connect/oauth2';
  58. const OAUTH_AUTHORIZE_URL = '/authorize?';
  59. private $token;
  60. private $encodingAesKey;
  61. private $appid; //也就是企业号的CorpID
  62. private $appsecret;
  63. private $access_token;
  64. private $agentid; //应用id AgentID
  65. private $postxml;
  66. private $agentidxml; //接收的应用id AgentID
  67. private $_msg;
  68. private $_receive;
  69. private $_sendmsg; //主动发送消息的内容
  70. private $_text_filter = true;
  71. public $debug = false;
  72. public $errCode = 40001;
  73. public $errMsg = "no access";
  74. private $_logcallback;
  75. public function __construct($options)
  76. {
  77. $this->token = isset($options['token'])?$options['token']:'';
  78. $this->encodingAesKey = isset($options['encodingaeskey'])?$options['encodingaeskey']:'';
  79. $this->appid = isset($options['appid'])?$options['appid']:'';
  80. $this->appsecret = isset($options['appsecret'])?$options['appsecret']:'';
  81. $this->agentid = isset($options['agentid'])?$options['agentid']:'';
  82. $this->debug = isset($options['debug'])?$options['debug']:false;
  83. $this->_logcallback = isset($options['logcallback'])?$options['logcallback']:false;
  84. }
  85. private function log($log){
  86. if ($this->debug && function_exists($this->_logcallback)) {
  87. if (is_array($log)) $log = print_r($log,true);
  88. return call_user_func($this->_logcallback,$log);
  89. }
  90. }
  91. /**
  92. * 数据XML编码
  93. * @param mixed $data 数据
  94. * @return string
  95. */
  96. public static function data_to_xml($data) {
  97. $xml = '';
  98. foreach ($data as $key => $val) {
  99. is_numeric($key) && $key = "item id=\"$key\"";
  100. $xml .= "<$key>";
  101. $xml .= ( is_array($val) || is_object($val)) ? self::data_to_xml($val) : self::xmlSafeStr($val);
  102. list($key, ) = explode(' ', $key);
  103. $xml .= "</$key>";
  104. }
  105. return $xml;
  106. }
  107. public static function xmlSafeStr($str)
  108. {
  109. return '<![CDATA['.preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/",'',$str).']]>';
  110. }
  111. /**
  112. * XML编码
  113. * @param mixed $data 数据
  114. * @param string $root 根节点名
  115. * @param string $item 数字索引的子节点名
  116. * @param string $attr 根节点属性
  117. * @param string $id 数字索引子节点key转换的属性名
  118. * @param string $encoding 数据编码
  119. * @return string
  120. */
  121. public function xml_encode($data, $root='xml', $item='item', $attr='', $id='id', $encoding='utf-8') {
  122. if(is_array($attr)){
  123. $_attr = array();
  124. foreach ($attr as $key => $value) {
  125. $_attr[] = "{$key}=\"{$value}\"";
  126. }
  127. $attr = implode(' ', $_attr);
  128. }
  129. $attr = trim($attr);
  130. $attr = empty($attr) ? '' : " {$attr}";
  131. $xml = "<{$root}{$attr}>";
  132. $xml .= self::data_to_xml($data, $item, $id);
  133. $xml .= "</{$root}>";
  134. return $xml;
  135. }
  136. /**
  137. * 微信api不支持中文转义的json结构
  138. * @param array $arr
  139. */
  140. static function json_encode($arr) {
  141. $parts = array ();
  142. $is_list = false;
  143. //Find out if the given array is a numerical array
  144. $keys = array_keys ( $arr );
  145. $max_length = count ( $arr ) - 1;
  146. if (($keys [0] === 0) && ($keys [$max_length] === $max_length )) { //See if the first key is 0 and last key is length - 1
  147. $is_list = true;
  148. for($i = 0; $i < count ( $keys ); $i ++) { //See if each key correspondes to its position
  149. if ($i != $keys [$i]) { //A key fails at position check.
  150. $is_list = false; //It is an associative array.
  151. break;
  152. }
  153. }
  154. }
  155. foreach ( $arr as $key => $value ) {
  156. if (is_array ( $value )) { //Custom handling for arrays
  157. if ($is_list)
  158. $parts [] = self::json_encode ( $value ); /* :RECURSION: */
  159. else
  160. $parts [] = '"' . $key . '":' . self::json_encode ( $value ); /* :RECURSION: */
  161. } else {
  162. $str = '';
  163. if (! $is_list)
  164. $str = '"' . $key . '":';
  165. //Custom handling for multiple data types
  166. if (!is_string ( $value ) && is_numeric ( $value ) && $value<2000000000)
  167. $str .= $value; //Numbers
  168. elseif ($value === false)
  169. $str .= 'false'; //The booleans
  170. elseif ($value === true)
  171. $str .= 'true';
  172. else
  173. $str .= '"' . addslashes ( $value ) . '"'; //All other things
  174. // :TODO: Is there any more datatype we should be in the lookout for? (Object?)
  175. $parts [] = $str;
  176. }
  177. }
  178. $json = implode ( ',', $parts );
  179. if ($is_list)
  180. return '[' . $json . ']'; //Return numerical JSON
  181. return '{' . $json . '}'; //Return associative JSON
  182. }
  183. /**
  184. * 过滤文字回复\r\n换行符
  185. * @param string $text
  186. * @return string|mixed
  187. */
  188. private function _auto_text_filter($text) {
  189. if (!$this->_text_filter) return $text;
  190. return str_replace("\r\n", "\n", $text);
  191. }
  192. /**
  193. * GET 请求
  194. * @param string $url
  195. */
  196. private function http_get($url){
  197. $oCurl = curl_init();
  198. if(stripos($url,"https://")!==FALSE){
  199. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  200. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
  201. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  202. }
  203. curl_setopt($oCurl, CURLOPT_URL, $url);
  204. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
  205. $sContent = curl_exec($oCurl);
  206. $aStatus = curl_getinfo($oCurl);
  207. curl_close($oCurl);
  208. if(intval($aStatus["http_code"])==200){
  209. return $sContent;
  210. }else{
  211. return false;
  212. }
  213. }
  214. /**
  215. * POST 请求
  216. * @param string $url
  217. * @param array $param
  218. * @param boolean $post_file 是否文件上传
  219. * @return string content
  220. */
  221. private function http_post($url,$param,$post_file=false){
  222. $oCurl = curl_init();
  223. if(stripos($url,"https://")!==FALSE){
  224. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  225. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  226. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  227. }
  228. if (is_string($param) || $post_file) {
  229. $strPOST = $param;
  230. } else {
  231. $aPOST = array();
  232. foreach($param as $key=>$val){
  233. $aPOST[] = $key."=".urlencode($val);
  234. }
  235. $strPOST = join("&", $aPOST);
  236. }
  237. curl_setopt($oCurl, CURLOPT_URL, $url);
  238. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
  239. curl_setopt($oCurl, CURLOPT_POST,true);
  240. curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
  241. $sContent = curl_exec($oCurl);
  242. $aStatus = curl_getinfo($oCurl);
  243. curl_close($oCurl);
  244. if(intval($aStatus["http_code"])==200){
  245. return $sContent;
  246. }else{
  247. return false;
  248. }
  249. }
  250. /**
  251. * For weixin server validation
  252. */
  253. private function checkSignature($str)
  254. {
  255. $signature = isset($_GET["msg_signature"])?$_GET["msg_signature"]:'';
  256. $timestamp = isset($_GET["timestamp"])?$_GET["timestamp"]:'';
  257. $nonce = isset($_GET["nonce"])?$_GET["nonce"]:'';
  258. $tmpArr = array($str,$this->token, $timestamp, $nonce);//比普通公众平台多了一个加密的密文
  259. sort($tmpArr, SORT_STRING);
  260. $tmpStr = implode($tmpArr);
  261. $shaStr = sha1($tmpStr);
  262. if( $shaStr == $signature ){
  263. return true;
  264. }else{
  265. return false;
  266. }
  267. }
  268. /**
  269. * 微信验证,包括post来的xml解密
  270. * @param bool $return 是否返回
  271. */
  272. public function valid($return=false)
  273. {
  274. $encryptStr="";
  275. if ($_SERVER['REQUEST_METHOD'] == "POST") {
  276. $postStr = file_get_contents("php://input");
  277. $array = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  278. $this->log($postStr);
  279. if (isset($array['Encrypt'])) {
  280. $encryptStr = $array['Encrypt'];
  281. $this->agentidxml = isset($array['AgentID']) ? $array['AgentID']: '';
  282. }
  283. } else {
  284. $encryptStr = isset($_GET["echostr"]) ? $_GET["echostr"]: '';
  285. }
  286. if ($encryptStr) {
  287. $ret=$this->checkSignature($encryptStr);
  288. }
  289. if (!isset($ret) || !$ret) {
  290. if (!$return) {
  291. die('no access');
  292. } else {
  293. return false;
  294. }
  295. }
  296. $pc = new Prpcrypt($this->encodingAesKey);
  297. $array = $pc->decrypt($encryptStr,$this->appid);
  298. if (!isset($array[0]) || ($array[0] != 0)) {
  299. if (!$return) {
  300. die('解密失败!');
  301. } else {
  302. return false;
  303. }
  304. }
  305. if ($_SERVER['REQUEST_METHOD'] == "POST") {
  306. $this->postxml = $array[1];
  307. //$this->log($array[1]);
  308. return ($this->postxml!="");
  309. } else {
  310. $echoStr = $array[1];
  311. if ($return) {
  312. return $echoStr;
  313. } else {
  314. die($echoStr);
  315. }
  316. }
  317. return false;
  318. }
  319. /**
  320. * 获取微信服务器发来的信息
  321. */
  322. public function getRev()
  323. {
  324. if ($this->_receive) return $this;
  325. $postStr = $this->postxml;
  326. $this->log($postStr);
  327. if (!empty($postStr)) {
  328. $this->_receive = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  329. if (!isset($this->_receive['AgentID'])) {
  330. $this->_receive['AgentID']=$this->agentidxml; //当前接收消息的应用id
  331. }
  332. }
  333. return $this;
  334. }
  335. /**
  336. * 获取微信服务器发来的信息
  337. */
  338. public function getRevData()
  339. {
  340. return $this->_receive;
  341. }
  342. /**
  343. * 获取微信服务器发来的原始加密信息
  344. */
  345. public function getRevPostXml()
  346. {
  347. return $this->postxml;
  348. }
  349. /**
  350. * 获取消息发送者
  351. */
  352. public function getRevFrom() {
  353. if (isset($this->_receive['FromUserName']))
  354. return $this->_receive['FromUserName'];
  355. else
  356. return false;
  357. }
  358. /**
  359. * 获取消息接受者
  360. */
  361. public function getRevTo() {
  362. if (isset($this->_receive['ToUserName']))
  363. return $this->_receive['ToUserName'];
  364. else
  365. return false;
  366. }
  367. /**
  368. * 获取接收消息的应用id
  369. */
  370. public function getRevAgentID() {
  371. if (isset($this->_receive['AgentID']))
  372. return $this->_receive['AgentID'];
  373. else
  374. return false;
  375. }
  376. /**
  377. * 获取接收消息的类型
  378. */
  379. public function getRevType() {
  380. if (isset($this->_receive['MsgType']))
  381. return $this->_receive['MsgType'];
  382. else
  383. return false;
  384. }
  385. /**
  386. * 获取消息ID
  387. */
  388. public function getRevID() {
  389. if (isset($this->_receive['MsgId']))
  390. return $this->_receive['MsgId'];
  391. else
  392. return false;
  393. }
  394. /**
  395. * 获取消息发送时间
  396. */
  397. public function getRevCtime() {
  398. if (isset($this->_receive['CreateTime']))
  399. return $this->_receive['CreateTime'];
  400. else
  401. return false;
  402. }
  403. /**
  404. * 获取接收消息内容正文
  405. */
  406. public function getRevContent(){
  407. if (isset($this->_receive['Content']))
  408. return $this->_receive['Content'];
  409. else
  410. return false;
  411. }
  412. /**
  413. * 获取接收消息图片
  414. */
  415. public function getRevPic(){
  416. if (isset($this->_receive['PicUrl']))
  417. return array(
  418. 'mediaid'=>$this->_receive['MediaId'],
  419. 'picurl'=>(string)$this->_receive['PicUrl'], //防止picurl为空导致解析出错
  420. );
  421. else
  422. return false;
  423. }
  424. /**
  425. * 获取接收地理位置
  426. */
  427. public function getRevGeo(){
  428. if (isset($this->_receive['Location_X'])){
  429. return array(
  430. 'x'=>$this->_receive['Location_X'],
  431. 'y'=>$this->_receive['Location_Y'],
  432. 'scale'=>(string)$this->_receive['Scale'],
  433. 'label'=>(string)$this->_receive['Label']
  434. );
  435. } else
  436. return false;
  437. }
  438. /**
  439. * 获取上报地理位置事件
  440. */
  441. public function getRevEventGeo(){
  442. if (isset($this->_receive['Latitude'])){
  443. return array(
  444. 'x'=>$this->_receive['Latitude'],
  445. 'y'=>$this->_receive['Longitude'],
  446. 'precision'=>$this->_receive['Precision'],
  447. );
  448. } else
  449. return false;
  450. }
  451. /**
  452. * 获取接收事件推送
  453. */
  454. public function getRevEvent(){
  455. if (isset($this->_receive['Event'])){
  456. $array['event'] = $this->_receive['Event'];
  457. }
  458. if (isset($this->_receive['EventKey'])){
  459. $array['key'] = $this->_receive['EventKey'];
  460. }
  461. if (isset($array) && count($array) > 0) {
  462. return $array;
  463. } else {
  464. return false;
  465. }
  466. }
  467. /**
  468. * 获取自定义菜单的扫码推事件信息
  469. *
  470. * 事件类型为以下两种时则调用此方法有效
  471. * Event 事件类型,scancode_push
  472. * Event 事件类型,scancode_waitmsg
  473. *
  474. * @return: array | false
  475. * array (
  476. * 'ScanType'=>'qrcode',
  477. * 'ScanResult'=>'123123'
  478. * )
  479. */
  480. public function getRevScanInfo(){
  481. if (isset($this->_receive['ScanCodeInfo'])){
  482. if (!is_array($this->_receive['SendPicsInfo'])) {
  483. $array=(array)$this->_receive['ScanCodeInfo'];
  484. $this->_receive['ScanCodeInfo']=$array;
  485. }else {
  486. $array=$this->_receive['ScanCodeInfo'];
  487. }
  488. }
  489. if (isset($array) && count($array) > 0) {
  490. return $array;
  491. } else {
  492. return false;
  493. }
  494. }
  495. /**
  496. * 获取自定义菜单的图片发送事件信息
  497. *
  498. * 事件类型为以下三种时则调用此方法有效
  499. * Event 事件类型,pic_sysphoto 弹出系统拍照发图的事件推送
  500. * Event 事件类型,pic_photo_or_album 弹出拍照或者相册发图的事件推送
  501. * Event 事件类型,pic_weixin 弹出微信相册发图器的事件推送
  502. *
  503. * @return: array | false
  504. * array (
  505. * 'Count' => '2',
  506. * 'PicList' =>array (
  507. * 'item' =>array (
  508. * 0 =>array ('PicMd5Sum' => 'aaae42617cf2a14342d96005af53624c'),
  509. * 1 =>array ('PicMd5Sum' => '149bd39e296860a2adc2f1bb81616ff8'),
  510. * ),
  511. * ),
  512. * )
  513. *
  514. */
  515. public function getRevSendPicsInfo(){
  516. if (isset($this->_receive['SendPicsInfo'])){
  517. if (!is_array($this->_receive['SendPicsInfo'])) {
  518. $array=(array)$this->_receive['SendPicsInfo'];
  519. if (isset($array['PicList'])){
  520. $array['PicList']=(array)$array['PicList'];
  521. $item=$array['PicList']['item'];
  522. $array['PicList']['item']=array();
  523. foreach ( $item as $key => $value ){
  524. $array['PicList']['item'][$key]=(array)$value;
  525. }
  526. }
  527. $this->_receive['SendPicsInfo']=$array;
  528. } else {
  529. $array=$this->_receive['SendPicsInfo'];
  530. }
  531. }
  532. if (isset($array) && count($array) > 0) {
  533. return $array;
  534. } else {
  535. return false;
  536. }
  537. }
  538. /**
  539. * 获取自定义菜单的地理位置选择器事件推送
  540. *
  541. * 事件类型为以下时则可以调用此方法有效
  542. * Event 事件类型,location_select 弹出系统拍照发图的事件推送
  543. *
  544. * @return: array | false
  545. * array (
  546. * 'Location_X' => '33.731655000061',
  547. * 'Location_Y' => '113.29955200008047',
  548. * 'Scale' => '16',
  549. * 'Label' => '某某市某某区某某路',
  550. * 'Poiname' => '',
  551. * )
  552. *
  553. */
  554. public function getRevSendGeoInfo(){
  555. if (isset($this->_receive['SendLocationInfo'])){
  556. if (!is_array($this->_receive['SendLocationInfo'])) {
  557. $array=(array)$this->_receive['SendLocationInfo'];
  558. if (empty($array['Poiname'])) {
  559. $array['Poiname']="";
  560. }
  561. if (empty($array['Label'])) {
  562. $array['Label']="";
  563. }
  564. $this->_receive['SendLocationInfo']=$array;
  565. } else {
  566. $array=$this->_receive['SendLocationInfo'];
  567. }
  568. }
  569. if (isset($array) && count($array) > 0) {
  570. return $array;
  571. } else {
  572. return false;
  573. }
  574. }
  575. /**
  576. * 获取接收语音推送
  577. */
  578. public function getRevVoice(){
  579. if (isset($this->_receive['MediaId'])){
  580. return array(
  581. 'mediaid'=>$this->_receive['MediaId'],
  582. 'format'=>$this->_receive['Format'],
  583. );
  584. } else
  585. return false;
  586. }
  587. /**
  588. * 获取接收视频推送
  589. */
  590. public function getRevVideo(){
  591. if (isset($this->_receive['MediaId'])){
  592. return array(
  593. 'mediaid'=>$this->_receive['MediaId'],
  594. 'thumbmediaid'=>$this->_receive['ThumbMediaId']
  595. );
  596. } else
  597. return false;
  598. }
  599. /**
  600. * 设置回复消息
  601. * Example: $obj->text('hello')->reply();
  602. * @param string $text
  603. */
  604. public function text($text='')
  605. {
  606. $msg = array(
  607. 'ToUserName' => $this->getRevFrom(),
  608. 'FromUserName'=>$this->getRevTo(),
  609. 'MsgType'=>self::MSGTYPE_TEXT,
  610. 'Content'=>$this->_auto_text_filter($text),
  611. 'CreateTime'=>time(),
  612. );
  613. $this->Message($msg);
  614. return $this;
  615. }
  616. /**
  617. * 设置回复消息
  618. * Example: $obj->image('media_id')->reply();
  619. * @param string $mediaid
  620. */
  621. public function image($mediaid='')
  622. {
  623. $msg = array(
  624. 'ToUserName' => $this->getRevFrom(),
  625. 'FromUserName'=>$this->getRevTo(),
  626. 'MsgType'=>self::MSGTYPE_IMAGE,
  627. 'Image'=>array('MediaId'=>$mediaid),
  628. 'CreateTime'=>time(),
  629. );
  630. $this->Message($msg);
  631. return $this;
  632. }
  633. /**
  634. * 设置回复消息
  635. * Example: $obj->voice('media_id')->reply();
  636. * @param string $mediaid
  637. */
  638. public function voice($mediaid='')
  639. {
  640. $msg = array(
  641. 'ToUserName' => $this->getRevFrom(),
  642. 'FromUserName'=>$this->getRevTo(),
  643. 'MsgType'=>self::MSGTYPE_IMAGE,
  644. 'Voice'=>array('MediaId'=>$mediaid),
  645. 'CreateTime'=>time(),
  646. );
  647. $this->Message($msg);
  648. return $this;
  649. }
  650. /**
  651. * 设置回复消息
  652. * Example: $obj->video('media_id','title','description')->reply();
  653. * @param string $mediaid
  654. */
  655. public function video($mediaid='',$title='',$description='')
  656. {
  657. $msg = array(
  658. 'ToUserName' => $this->getRevFrom(),
  659. 'FromUserName'=>$this->getRevTo(),
  660. 'MsgType'=>self::MSGTYPE_IMAGE,
  661. 'Video'=>array(
  662. 'MediaId'=>$mediaid,
  663. 'Title'=>$title,
  664. 'Description'=>$description
  665. ),
  666. 'CreateTime'=>time(),
  667. );
  668. $this->Message($msg);
  669. return $this;
  670. }
  671. /**
  672. * 设置回复图文
  673. * @param array $newsData
  674. * 数组结构:
  675. * array(
  676. * "0"=>array(
  677. * 'Title'=>'msg title',
  678. * 'Description'=>'summary text',
  679. * 'PicUrl'=>'http://www.domain.com/1.jpg',
  680. * 'Url'=>'http://www.domain.com/1.html'
  681. * ),
  682. * "1"=>....
  683. * )
  684. */
  685. public function news($newsData=array())
  686. {
  687. $count = count($newsData);
  688. $msg = array(
  689. 'ToUserName' => $this->getRevFrom(),
  690. 'FromUserName'=>$this->getRevTo(),
  691. 'MsgType'=>self::MSGTYPE_NEWS,
  692. 'CreateTime'=>time(),
  693. 'ArticleCount'=>$count,
  694. 'Articles'=>$newsData,
  695. );
  696. $this->Message($msg);
  697. return $this;
  698. }
  699. /**
  700. * 设置发送消息
  701. * @param array $msg 消息数组
  702. * @param bool $append 是否在原消息数组追加
  703. */
  704. public function Message($msg = '',$append = false){
  705. if (is_null($msg)) {
  706. $this->_msg =array();
  707. }elseif (is_array($msg)) {
  708. if ($append)
  709. $this->_msg = array_merge($this->_msg,$msg);
  710. else
  711. $this->_msg = $msg;
  712. return $this->_msg;
  713. } else {
  714. return $this->_msg;
  715. }
  716. }
  717. /**
  718. *
  719. * 回复微信服务器, 此函数支持链式操作
  720. * Example: $this->text('msg tips')->reply();
  721. * @param string $msg 要发送的信息, 默认取$this->_msg
  722. * @param bool $return 是否返回信息而不抛出到浏览器 默认:否
  723. */
  724. public function reply($msg=array(),$return = false)
  725. {
  726. if (empty($msg))
  727. $msg = $this->_msg;
  728. $xmldata= $this->xml_encode($msg);
  729. $this->log($xmldata);
  730. $pc = new Prpcrypt($this->encodingAesKey);
  731. $array = $pc->encrypt($xmldata, $this->appid);
  732. $ret = $array[0];
  733. if ($ret != 0) {
  734. $this->log('encrypt err!');
  735. return false;
  736. }
  737. $timestamp = time();
  738. $nonce = rand(77,999)*rand(605,888)*rand(11,99);
  739. $encrypt = $array[1];
  740. $tmpArr = array($this->token, $timestamp, $nonce,$encrypt);//比普通公众平台多了一个加密的密文
  741. sort($tmpArr, SORT_STRING);
  742. $signature = implode($tmpArr);
  743. $signature = sha1($signature);
  744. $smsg = $this->generate($encrypt, $signature, $timestamp, $nonce);
  745. $this->log($smsg);
  746. if ($return)
  747. return $smsg;
  748. elseif ($smsg){
  749. echo $smsg;
  750. return true;
  751. }else
  752. return false;
  753. }
  754. private function generate($encrypt, $signature, $timestamp, $nonce)
  755. {
  756. //格式化加密信息
  757. $format = "<xml>
  758. <Encrypt><![CDATA[%s]]></Encrypt>
  759. <MsgSignature><![CDATA[%s]]></MsgSignature>
  760. <TimeStamp>%s</TimeStamp>
  761. <Nonce><![CDATA[%s]]></Nonce>
  762. </xml>";
  763. return sprintf($format, $encrypt, $signature, $timestamp, $nonce);
  764. }
  765. /**
  766. * 通用auth验证方法
  767. * @param string $appid
  768. * @param string $appsecret
  769. * @param string $token 手动指定access_token,非必要情况不建议用
  770. */
  771. public function checkAuth($appid='',$appsecret='',$token=''){
  772. if (!$appid || !$appsecret) {
  773. $appid = $this->appid;
  774. $appsecret = $this->appsecret;
  775. }
  776. if ($token) { //手动指定token,优先使用
  777. $this->access_token=$token;
  778. return $this->access_token;
  779. }
  780. //TODO: get the cache access_token
  781. $result = $this->http_get(self::API_URL_PREFIX.self::TOKEN_GET_URL.'corpid='.$appid.'&corpsecret='.$appsecret);
  782. if ($result)
  783. {
  784. $json = json_decode($result,true);
  785. if (!$json || isset($json['errcode'])) {
  786. $this->errCode = $json['errcode'];
  787. $this->errMsg = $json['errmsg'];
  788. return false;
  789. }
  790. $this->access_token = $json['access_token'];
  791. $expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
  792. //TODO: cache access_token
  793. return $this->access_token;
  794. }
  795. return false;
  796. }
  797. /**
  798. * 删除验证数据
  799. * @param string $appid
  800. */
  801. public function resetAuth($appid=''){
  802. if (!$appid) $appid = $this->appid;
  803. $this->access_token = '';
  804. //TODO: remove cache
  805. return true;
  806. }
  807. /**
  808. * 创建菜单
  809. * @param array $data 菜单数组数据
  810. * example:
  811. * array (
  812. * 'button' => array (
  813. * 0 => array (
  814. * 'name' => '扫码',
  815. * 'sub_button' => array (
  816. * 0 => array (
  817. * 'type' => 'scancode_waitmsg',
  818. * 'name' => '扫码带提示',
  819. * 'key' => 'rselfmenu_0_0',
  820. * ),
  821. * 1 => array (
  822. * 'type' => 'scancode_push',
  823. * 'name' => '扫码推事件',
  824. * 'key' => 'rselfmenu_0_1',
  825. * ),
  826. * ),
  827. * ),
  828. * 1 => array (
  829. * 'name' => '发图',
  830. * 'sub_button' => array (
  831. * 0 => array (
  832. * 'type' => 'pic_sysphoto',
  833. * 'name' => '系统拍照发图',
  834. * 'key' => 'rselfmenu_1_0',
  835. * ),
  836. * 1 => array (
  837. * 'type' => 'pic_photo_or_album',
  838. * 'name' => '拍照或者相册发图',
  839. * 'key' => 'rselfmenu_1_1',
  840. * )
  841. * ),
  842. * ),
  843. * 2 => array (
  844. * 'type' => 'location_select',
  845. * 'name' => '发送位置',
  846. * 'key' => 'rselfmenu_2_0'
  847. * ),
  848. * ),
  849. * )
  850. * type可以选择为以下几种,会收到相应类型的事件推送。请注意,3到8的所有事件,仅支持微信iPhone5.4.1以上版本,
  851. * 和Android5.4以上版本的微信用户,旧版本微信用户点击后将没有回应,开发者也不能正常接收到事件推送。
  852. * 1、click:点击推事件
  853. * 2、view:跳转URL
  854. * 3、scancode_push:扫码推事件
  855. * 4、scancode_waitmsg:扫码推事件且弹出“消息接收中”提示框
  856. * 5、pic_sysphoto:弹出系统拍照发图
  857. * 6、pic_photo_or_album:弹出拍照或者相册发图
  858. * 7、pic_weixin:弹出微信相册发图器
  859. * 8、location_select:弹出地理位置选择器
  860. */
  861. public function createMenu($data,$agentid=''){
  862. if ($agentid=='') {
  863. $agentid=$this->agentid;
  864. }
  865. if (!$this->access_token && !$this->checkAuth()) return false;
  866. $result = $this->http_post(self::API_URL_PREFIX.self::MENU_CREATE_URL.'access_token='.$this->access_token.'&agentid='.$agentid,self::json_encode($data));
  867. if ($result)
  868. {
  869. $json = json_decode($result,true);
  870. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  871. $this->errCode = $json['errcode'];
  872. $this->errMsg = $json['errmsg'];
  873. return false;
  874. }
  875. return true;
  876. }
  877. return false;
  878. }
  879. /**
  880. * 获取菜单
  881. * @return array('menu'=>array(....s))
  882. */
  883. public function getMenu($agentid=''){
  884. if ($agentid=='') {
  885. $agentid=$this->agentid;
  886. }
  887. if (!$this->access_token && !$this->checkAuth()) return false;
  888. $result = $this->http_get(self::API_URL_PREFIX.self::MENU_GET_URL.'access_token='.$this->access_token.'&agentid='.$agentid);
  889. if ($result)
  890. {
  891. $json = json_decode($result,true);
  892. if (!$json || isset($json['errcode']) || $json['errcode']!=0) {
  893. $this->errCode = $json['errcode'];
  894. $this->errMsg = $json['errmsg'];
  895. return false;
  896. }
  897. return $json;
  898. }
  899. return false;
  900. }
  901. /**
  902. * 删除菜单
  903. * @return boolean
  904. */
  905. public function deleteMenu($agentid=''){
  906. if ($agentid=='') {
  907. $agentid=$this->agentid;
  908. }
  909. if (!$this->access_token && !$this->checkAuth()) return false;
  910. $result = $this->http_get(self::API_URL_PREFIX.self::MENU_DELETE_URL.'access_token='.$this->access_token.'&agentid='.$agentid);
  911. if ($result)
  912. {
  913. $json = json_decode($result,true);
  914. if (!$json || !empty($json['errcode'])) {
  915. $this->errCode = $json['errcode'];
  916. $this->errMsg = $json['errmsg'];
  917. return false;
  918. }
  919. return true;
  920. }
  921. return false;
  922. }
  923. /**
  924. * 上传多媒体文件 (只有三天的有效期,过期自动被删除)
  925. * 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
  926. * 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
  927. * @param array $data {"media":'@Path\filename.jpg'}
  928. * @param type 媒体文件类型:图片(image)、语音(voice)、视频(video),普通文件(file)
  929. * @return boolean|array
  930. * {
  931. * "type": "image",
  932. * "media_id": "0000001",
  933. * "created_at": "1380000000"
  934. * }
  935. */
  936. public function uploadMedia($data, $type){
  937. if (!$this->access_token && !$this->checkAuth()) return false;
  938. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOAD.'access_token='.$this->access_token.'&type='.$type,$data,true);
  939. if ($result)
  940. {
  941. $json = json_decode($result,true);
  942. if (!$json || !empty($json['errcode'])) {
  943. $this->errCode = $json['errcode'];
  944. $this->errMsg = $json['errmsg'];
  945. return false;
  946. }
  947. return $json;
  948. }
  949. return false;
  950. }
  951. /**
  952. * 根据媒体文件ID获取媒体文件
  953. * @param string $media_id 媒体文件id
  954. * @return raw data
  955. */
  956. public function getMedia($media_id){
  957. if (!$this->access_token && !$this->checkAuth()) return false;
  958. $result = $this->http_get(self::API_URL_PREFIX.self::MEDIA_GET_URL.'access_token='.$this->access_token.'&media_id='.$media_id);
  959. if ($result)
  960. {
  961. $json = json_decode($result,true);
  962. if (isset($json['errcode'])) {
  963. $this->errCode = $json['errcode'];
  964. $this->errMsg = $json['errmsg'];
  965. return false;
  966. }
  967. return $result;
  968. }
  969. return false;
  970. }
  971. /**
  972. * 创建部门
  973. * @param array $data 结构体为:
  974. * array (
  975. * "name" => "邮箱产品组", //部门名称
  976. * "parentid" => "1" //父部门id
  977. * "order" => "1", //(非必须)在父部门中的次序。从1开始,数字越大排序越靠后
  978. * )
  979. * @return boolean|array
  980. * 成功返回结果
  981. * {
  982. * "errcode": 0, //返回码
  983. * "errmsg": "created", //对返回码的文本描述内容
  984. * "id": 2 //创建的部门id。
  985. * }
  986. */
  987. public function createDepartment($data){
  988. if (!$this->access_token && !$this->checkAuth()) return false;
  989. $result = $this->http_post(self::API_URL_PREFIX.self::DEPARTMENT_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  990. if ($result)
  991. {
  992. $json = json_decode($result,true);
  993. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  994. $this->errCode = $json['errcode'];
  995. $this->errMsg = $json['errmsg'];
  996. return false;
  997. }
  998. return $json;
  999. }
  1000. return false;
  1001. }
  1002. /**
  1003. * 更新部门
  1004. * @param array $data 结构体为:
  1005. * array(
  1006. * "id" => "1" //(必须)部门id
  1007. * "name" => "邮箱产品组", //(非必须)部门名称
  1008. * "parentid" => "1", //(非必须)父亲部门id。根部门id为1
  1009. * "order" => "1", //(非必须)在父部门中的次序。从1开始,数字越大排序越靠后
  1010. * )
  1011. * @return boolean|array 成功返回结果
  1012. * {
  1013. * "errcode": 0, //返回码
  1014. * "errmsg": "updated" //对返回码的文本描述内容
  1015. * }
  1016. */
  1017. public function updateDepartment($data){
  1018. if (!$this->access_token && !$this->checkAuth()) return false;
  1019. $result = $this->http_post(self::API_URL_PREFIX.self::DEPARTMENT_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1020. if ($result)
  1021. {
  1022. $json = json_decode($result,true);
  1023. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1024. $this->errCode = $json['errcode'];
  1025. $this->errMsg = $json['errmsg'];
  1026. return false;
  1027. }
  1028. return $json;
  1029. }
  1030. return false;
  1031. }
  1032. /**
  1033. * 删除部门
  1034. * @param $id
  1035. * @return boolean|array 成功返回结果
  1036. * {
  1037. * "errcode": 0, //返回码
  1038. * "errmsg": "deleted" //对返回码的文本描述内容
  1039. * }
  1040. */
  1041. public function deleteDepartment($id){
  1042. if (!$this->access_token && !$this->checkAuth()) return false;
  1043. $result = $this->http_get(self::API_URL_PREFIX.self::DEPARTMENT_DELETE_URL.'access_token='.$this->access_token.'&id='.$id);
  1044. if ($result)
  1045. {
  1046. $json = json_decode($result,true);
  1047. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1048. $this->errCode = $json['errcode'];
  1049. $this->errMsg = $json['errmsg'];
  1050. return false;
  1051. }
  1052. return $json;
  1053. }
  1054. return false;
  1055. }
  1056. /**
  1057. * 移动部门
  1058. * @param $data
  1059. * array(
  1060. * "department_id" => "5", //所要移动的部门
  1061. * "to_parentid" => "2", //想移动到的父部门节点,根部门为1
  1062. * "to_position" => "1" //(非必须)想移动到的父部门下的位置,1表示最上方,往后位置为2,3,4,以此类推,默认为1
  1063. * )
  1064. * @return boolean|array 成功返回结果
  1065. * {
  1066. * "errcode": 0, //返回码
  1067. * "errmsg": "ok" //对返回码的文本描述内容
  1068. * }
  1069. */
  1070. public function moveDepartment($data){
  1071. if (!$this->access_token && !$this->checkAuth()) return false;
  1072. $result = $this->http_get(self::API_URL_PREFIX.self::DEPARTMENT_MOVE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1073. if ($result)
  1074. {
  1075. $json = json_decode($result,true);
  1076. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1077. $this->errCode = $json['errcode'];
  1078. $this->errMsg = $json['errmsg'];
  1079. return false;
  1080. }
  1081. return $json;
  1082. }
  1083. return false;
  1084. }
  1085. /**
  1086. * 获取部门列表
  1087. * @return boolean|array 成功返回结果
  1088. * {
  1089. * "errcode": 0,
  1090. * "errmsg": "ok",
  1091. * "department": [ //部门列表数据。以部门的order字段从小到大排列
  1092. * {
  1093. * "id": 1,
  1094. * "name": "广州研发中心",
  1095. * "parentid": 0
  1096. * },
  1097. * {
  1098. * "id": 2
  1099. * "name": "邮箱产品部",
  1100. * "parentid": 1
  1101. * }
  1102. * ]
  1103. * }
  1104. */
  1105. public function getDepartment(){
  1106. if (!$this->access_token && !$this->checkAuth()) return false;
  1107. $result = $this->http_get(self::API_URL_PREFIX.self::DEPARTMENT_LIST_URL.'access_token='.$this->access_token);
  1108. if ($result)
  1109. {
  1110. $json = json_decode($result,true);
  1111. if (!$json || !empty($json['errcode'])) {
  1112. $this->errCode = $json['errcode'];
  1113. $this->errMsg = $json['errmsg'];
  1114. return false;
  1115. }
  1116. return $json;
  1117. }
  1118. return false;
  1119. }
  1120. /**
  1121. * 创建成员
  1122. * @param array $data 结构体为:
  1123. * array(
  1124. * "userid" => "zhangsan",
  1125. * "name" => "张三",
  1126. * "department" => [1, 2],
  1127. * "position" => "产品经理",
  1128. * "mobile" => "15913215421",
  1129. * "gender" => 1, //性别。gender=0表示男,=1表示女
  1130. * "tel" => "62394",
  1131. * "email" => "zhangsan@gzdev.com",
  1132. * "weixinid" => "zhangsan4dev"
  1133. * )
  1134. * @return boolean|array
  1135. * 成功返回结果
  1136. * {
  1137. * "errcode": 0, //返回码
  1138. * "errmsg": "created", //对返回码的文本描述内容
  1139. * }
  1140. */
  1141. public function createUser($data){
  1142. if (!$this->access_token && !$this->checkAuth()) return false;
  1143. $result = $this->http_post(self::API_URL_PREFIX.self::USER_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1144. if ($result)
  1145. {
  1146. $json = json_decode($result,true);
  1147. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1148. $this->errCode = $json['errcode'];
  1149. $this->errMsg = $json['errmsg'];
  1150. return false;
  1151. }
  1152. return $json;
  1153. }
  1154. return false;
  1155. }
  1156. /**
  1157. * 更新成员
  1158. * @param array $data 结构体为:
  1159. * array(
  1160. * "userid" => "zhangsan",
  1161. * "name" => "张三",
  1162. * "department" => [1, 2],
  1163. * "position" => "产品经理",
  1164. * "mobile" => "15913215421",
  1165. * "gender" => 1, //性别。gender=0表示男,=1表示女
  1166. * "tel" => "62394",
  1167. * "email" => "zhangsan@gzdev.com",
  1168. * "weixinid" => "zhangsan4dev"
  1169. * )
  1170. * @return boolean|array 成功返回结果
  1171. * {
  1172. * "errcode": 0, //返回码
  1173. * "errmsg": "updated" //对返回码的文本描述内容
  1174. * }
  1175. */
  1176. public function updateUser($data){
  1177. if (!$this->access_token && !$this->checkAuth()) return false;
  1178. $result = $this->http_post(self::API_URL_PREFIX.self::USER_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1179. if ($result)
  1180. {
  1181. $json = json_decode($result,true);
  1182. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1183. $this->errCode = $json['errcode'];
  1184. $this->errMsg = $json['errmsg'];
  1185. return false;
  1186. }
  1187. return $json;
  1188. }
  1189. return false;
  1190. }
  1191. /**
  1192. * 删除成员
  1193. * @param $userid 员工UserID。对应管理端的帐号
  1194. * @return boolean|array 成功返回结果
  1195. * {
  1196. * "errcode": 0, //返回码
  1197. * "errmsg": "deleted" //对返回码的文本描述内容
  1198. * }
  1199. */
  1200. public function deleteUser($userid){
  1201. if (!$this->access_token && !$this->checkAuth()) return false;
  1202. $result = $this->http_get(self::API_URL_PREFIX.self::USER_DELETE_URL.'access_token='.$this->access_token.'&userid='.$userid);
  1203. if ($result)
  1204. {
  1205. $json = json_decode($result,true);
  1206. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1207. $this->errCode = $json['errcode'];
  1208. $this->errMsg = $json['errmsg'];
  1209. return false;
  1210. }
  1211. return $json;
  1212. }
  1213. return false;
  1214. }
  1215. /**
  1216. * 获取成员信息
  1217. * @param $userid 员工UserID。对应管理端的帐号
  1218. * @return boolean|array 成功返回结果
  1219. * {
  1220. * "errcode": 0,
  1221. * "errmsg": "ok",
  1222. * "userid": "zhangsan",
  1223. * "name": "李四",
  1224. * "department": [1, 2],
  1225. * "position": "后台工程师",
  1226. * "mobile": "15913215421",
  1227. * "gender": 1, //性别。gender=0表示男,=1表示女
  1228. * "tel": "62394",
  1229. * "email": "zhangsan@gzdev.com",
  1230. * "weixinid": "lisifordev", //微信号
  1231. * "avatar": "http://wx.qlogo.cn/mmopen/ajNVdqHZLLA3W..../0", //头像url。注:如果要获取小图将url最后的"/0"改成"/64"即可
  1232. * "status": 1 //关注状态: 1=已关注,2=已冻结,4=未关注
  1233. * }
  1234. */
  1235. public function getUserInfo($userid){
  1236. if (!$this->access_token && !$this->checkAuth()) return false;
  1237. $result = $this->http_get(self::API_URL_PREFIX.self::USER_GET_URL.'access_token='.$this->access_token.'&userid='.$userid);
  1238. if ($result)
  1239. {
  1240. $json = json_decode($result,true);
  1241. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1242. $this->errCode = $json['errcode'];
  1243. $this->errMsg = $json['errmsg'];
  1244. return false;
  1245. }
  1246. return $json;
  1247. }
  1248. return false;
  1249. }
  1250. /**
  1251. * 获取部门成员
  1252. * @param $department_id 部门id
  1253. * @param $fetch_child 1/0:是否递归获取子部门下面的成员
  1254. * @param $status 0获取全部员工,1获取已关注成员列表,2获取禁用成员列表,4获取未关注成员列表。status可叠加
  1255. * @return boolean|array 成功返回结果
  1256. * {
  1257. * "errcode": 0,
  1258. * "errmsg": "ok",
  1259. * "userlist": [
  1260. * {
  1261. * "userid": "zhangsan",
  1262. * "name": "李四"
  1263. * }
  1264. * ]
  1265. * }
  1266. */
  1267. public function getUserList($department_id,$fetch_child=0,$status=0){
  1268. if (!$this->access_token && !$this->checkAuth()) return false;
  1269. $result = $this->http_get(self::API_URL_PREFIX.self::USER_LIST_URL.'access_token='.$this->access_token
  1270. .'&department_id='.$department_id.'&fetch_child='.$fetch_child.'&status='.$status);
  1271. if ($result)
  1272. {
  1273. $json = json_decode($result,true);
  1274. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1275. $this->errCode = $json['errcode'];
  1276. $this->errMsg = $json['errmsg'];
  1277. return false;
  1278. }
  1279. return $json;
  1280. }
  1281. return false;
  1282. }
  1283. /**
  1284. * 根据code获取成员信息
  1285. * 通过Oauth2.0或者设置了二次验证时获取的code,用于换取成员的UserId和DeviceId
  1286. *
  1287. * @param $code Oauth2.0或者二次验证时返回的code值
  1288. * @param $agentid 跳转链接时所在的企业应用ID,未填则默认为当前配置的应用id
  1289. * @return boolean|array 成功返回数组
  1290. * array(
  1291. * 'UserId' => 'USERID', //员工UserID
  1292. * 'DeviceId' => 'DEVICEID' //手机设备号(由微信在安装时随机生成)
  1293. * )
  1294. */
  1295. public function getUserId($code,$agentid=0){
  1296. if (!$agentid) $agentid=$this->agentid;
  1297. if (!$this->access_token && !$this->checkAuth()) return false;
  1298. $result = $this->http_get(self::API_URL_PREFIX.self::USER_GETINFO_URL.'access_token='.$this->access_token.'&code='.$code.'&agentid'.$agentid);
  1299. if ($result)
  1300. {
  1301. $json = json_decode($result,true);
  1302. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1303. $this->errCode = $json['errcode'];
  1304. $this->errMsg = $json['errmsg'];
  1305. return false;
  1306. }
  1307. return $json;
  1308. }
  1309. return false;
  1310. }
  1311. /**
  1312. * 创建标签
  1313. * @param array $data 结构体为:
  1314. * array(
  1315. * "tagname" => "UI"
  1316. * )
  1317. * @return boolean|array
  1318. * 成功返回结果
  1319. * {
  1320. * "errcode": 0, //返回码
  1321. * "errmsg": "created", //对返回码的文本描述内容
  1322. * "tagid": "1"
  1323. * }
  1324. */
  1325. public function createTag($data){
  1326. if (!$this->access_token && !$this->checkAuth()) return false;
  1327. $result = $this->http_post(self::API_URL_PREFIX.self::TAG_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1328. if ($result)
  1329. {
  1330. $json = json_decode($result,true);
  1331. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1332. $this->errCode = $json['errcode'];
  1333. $this->errMsg = $json['errmsg'];
  1334. return false;
  1335. }
  1336. return $json;
  1337. }
  1338. return false;
  1339. }
  1340. /**
  1341. * 更新标签
  1342. * @param array $data 结构体为:
  1343. * array(
  1344. * "tagid" => "1",
  1345. * "tagname" => "UI design"
  1346. * )
  1347. * @return boolean|array 成功返回结果
  1348. * {
  1349. * "errcode": 0, //返回码
  1350. * "errmsg": "updated" //对返回码的文本描述内容
  1351. * }
  1352. */
  1353. public function updateTag($data){
  1354. if (!$this->access_token && !$this->checkAuth()) return false;
  1355. $result = $this->http_post(self::API_URL_PREFIX.self::TAG_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1356. if ($result)
  1357. {
  1358. $json = json_decode($result,true);
  1359. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1360. $this->errCode = $json['errcode'];
  1361. $this->errMsg = $json['errmsg'];
  1362. return false;
  1363. }
  1364. return $json;
  1365. }
  1366. return false;
  1367. }
  1368. /**
  1369. * 删除标签
  1370. * @param $tagid 标签TagID
  1371. * @return boolean|array 成功返回结果
  1372. * {
  1373. * "errcode": 0, //返回码
  1374. * "errmsg": "deleted" //对返回码的文本描述内容
  1375. * }
  1376. */
  1377. public function deleteTag($tagid){
  1378. if (!$this->access_token && !$this->checkAuth()) return false;
  1379. $result = $this->http_get(self::API_URL_PREFIX.self::TAG_DELETE_URL.'access_token='.$this->access_token.'&tagid='.$tagid);
  1380. if ($result)
  1381. {
  1382. $json = json_decode($result,true);
  1383. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1384. $this->errCode = $json['errcode'];
  1385. $this->errMsg = $json['errmsg'];
  1386. return false;
  1387. }
  1388. return $json;
  1389. }
  1390. return false;
  1391. }
  1392. /**
  1393. * 获取标签成员
  1394. * @param $tagid 标签TagID
  1395. * @return boolean|array 成功返回结果
  1396. * {
  1397. * "errcode": 0,
  1398. * "errmsg": "ok",
  1399. * "userlist": [
  1400. * {
  1401. * "userid": "zhangsan",
  1402. * "name": "李四"
  1403. * }
  1404. * ]
  1405. * }
  1406. */
  1407. public function getTag($tagid){
  1408. if (!$this->access_token && !$this->checkAuth()) return false;
  1409. $result = $this->http_get(self::API_URL_PREFIX.self::TAG_GET_URL.'access_token='.$this->access_token.'&tagid='.$tagid);
  1410. if ($result)
  1411. {
  1412. $json = json_decode($result,true);
  1413. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1414. $this->errCode = $json['errcode'];
  1415. $this->errMsg = $json['errmsg'];
  1416. return false;
  1417. }
  1418. return $json;
  1419. }
  1420. return false;
  1421. }
  1422. /**
  1423. * 增加标签成员
  1424. * @param array $data 结构体为:
  1425. * array (
  1426. * "tagid" => "1",
  1427. * "userlist" => array( //企业员工ID列表
  1428. * "user1",
  1429. * "user2"
  1430. * )
  1431. * )
  1432. * @return boolean|array
  1433. * 成功返回结果
  1434. * {
  1435. * "errcode": 0, //返回码
  1436. * "errmsg": "ok", //对返回码的文本描述内容
  1437. * "invalidlist":"usr1|usr2|usr" //若部分userid非法,则会有此段。不在权限内的员工ID列表,以“|”分隔
  1438. * }
  1439. */
  1440. public function addTagUser($data){
  1441. if (!$this->access_token && !$this->checkAuth()) return false;
  1442. $result = $this->http_post(self::API_URL_PREFIX.self::TAG_ADDUSER_URL.'access_token='.$this->access_token,self::json_encode($data));
  1443. if ($result)
  1444. {
  1445. $json = json_decode($result,true);
  1446. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1447. $this->errCode = $json['errcode'];
  1448. $this->errMsg = $json['errmsg'];
  1449. return false;
  1450. }
  1451. return $json;
  1452. }
  1453. return false;
  1454. }
  1455. /**
  1456. * 删除标签成员
  1457. * @param array $data 结构体为:
  1458. * array (
  1459. * "tagid" => "1",
  1460. * "userlist" => array( //企业员工ID列表
  1461. * "user1",
  1462. * "user2"
  1463. * )
  1464. * )
  1465. * @return boolean|array
  1466. * 成功返回结果
  1467. * {
  1468. * "errcode": 0, //返回码
  1469. * "errmsg": "deleted", //对返回码的文本描述内容
  1470. * "invalidlist":"usr1|usr2|usr" //若部分userid非法,则会有此段。不在权限内的员工ID列表,以“|”分隔
  1471. * }
  1472. */
  1473. public function delTagUser($data){
  1474. if (!$this->access_token && !$this->checkAuth()) return false;
  1475. $result = $this->http_post(self::API_URL_PREFIX.self::TAG_DELUSER_URL.'access_token='.$this->access_token,self::json_encode($data));
  1476. if ($result)
  1477. {
  1478. $json = json_decode($result,true);
  1479. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1480. $this->errCode = $json['errcode'];
  1481. $this->errMsg = $json['errmsg'];
  1482. return false;
  1483. }
  1484. return $json;
  1485. }
  1486. return false;
  1487. }
  1488. /**
  1489. * 获取标签列表
  1490. * @return boolean|array 成功返回数组结果,这里附上json样例
  1491. * {
  1492. * "errcode": 0,
  1493. * "errmsg": "ok",
  1494. * "taglist":[
  1495. * {"tagid":1,"tagname":"a"},
  1496. * {"tagid":2,"tagname":"b"}
  1497. * ]
  1498. * }
  1499. */
  1500. public function getTagList(){
  1501. if (!$this->access_token && !$this->checkAuth()) return false;
  1502. $result = $this->http_get(self::API_URL_PREFIX.self::TAG_LIST_URL.'access_token='.$this->access_token);
  1503. if ($result)
  1504. {
  1505. $json = json_decode($result,true);
  1506. if (!$json || !empty($json['errcode'])) {
  1507. $this->errCode = $json['errcode'];
  1508. $this->errMsg = $json['errmsg'];
  1509. return false;
  1510. }
  1511. return $json;
  1512. }
  1513. return false;
  1514. }
  1515. /**
  1516. * 主动发送信息接口
  1517. * @param array $data 结构体为:
  1518. * array(
  1519. * "touser" => "UserID1|UserID2|UserID3",
  1520. * "toparty" => "PartyID1|PartyID2 ",
  1521. * "totag" => "TagID1|TagID2 ",
  1522. * "safe":"0" //是否为保密消息,对于news无效
  1523. * "agentid" => "001", //应用id
  1524. * "msgtype" => "text", //根据信息类型,选择下面对应的信息结构体
  1525. *
  1526. * "text" => array(
  1527. * "content" => "Holiday Request For Pony(http://xxxxx)"
  1528. * ),
  1529. *
  1530. * "image" => array(
  1531. * "media_id" => "MEDIA_ID"
  1532. * ),
  1533. *
  1534. * "voice" => array(
  1535. * "media_id" => "MEDIA_ID"
  1536. * ),
  1537. *
  1538. * " video" => array(
  1539. * "media_id" => "MEDIA_ID",
  1540. * "title" => "Title",
  1541. * "description" => "Description"
  1542. * ),
  1543. *
  1544. * "file" => array(
  1545. * "media_id" => "MEDIA_ID"
  1546. * ),
  1547. *
  1548. * "news" => array( //不支持保密
  1549. * "articles" => array( //articles 图文消息,一个图文消息支持1到10个图文
  1550. * array(
  1551. * "title" => "Title", //标题
  1552. * "description" => "Description", //描述
  1553. * "url" => "URL", //点击后跳转的链接。可根据url里面带的code参数校验员工的真实身份。
  1554. * "picurl" => "PIC_URL", //图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图640*320,
  1555. * //小图80*80。如不填,在客户端不显示图片
  1556. * ),
  1557. * )
  1558. * ),
  1559. *
  1560. * "mpnews" => array(
  1561. * "articles" => array( //articles 图文消息,一个图文消息支持1到10个图文
  1562. * array(
  1563. * "title" => "Title", //图文消息的标题
  1564. * "thumb_media_id" => "id", //图文消息缩略图的media_id
  1565. * "author" => "Author", //图文消息的作者(可空)
  1566. * "content_source_url" => "URL", //图文消息点击“阅读原文”之后的页面链接(可空)
  1567. * "content" => "Content" //图文消息的内容,支持html标签
  1568. * "digest" => "Digest description", //图文消息的描述
  1569. * "show_cover_pic" => "0" //是否显示封面,1为显示,0为不显示(可空)
  1570. * ),
  1571. * )
  1572. * )
  1573. * )
  1574. * 请查看官方开发文档中的 发送消息 -> 消息类型及数据格式
  1575. *
  1576. * @return boolean|array
  1577. * 如果对应用或收件人、部门、标签任何一个无权限,则本次发送失败;
  1578. * 如果收件人、部门或标签不存在,发送仍然执行,但返回无效的部分。
  1579. * {
  1580. * "errcode": 0,
  1581. * "errmsg": "ok",
  1582. * "invaliduser": "UserID1",
  1583. * "invalidparty":"PartyID1",
  1584. * "invalidtag":"TagID1"
  1585. * }
  1586. */
  1587. public function sendMessage($data){
  1588. if (!$this->access_token && !$this->checkAuth()) return false;
  1589. $result = $this->http_post(self::API_URL_PREFIX.self::MASS_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
  1590. if ($result)
  1591. {
  1592. $json = json_decode($result,true);
  1593. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1594. $this->errCode = $json['errcode'];
  1595. $this->errMsg = $json['errmsg'];
  1596. return false;
  1597. }
  1598. return $json;
  1599. }
  1600. return false;
  1601. }
  1602. /**
  1603. * 二次验证
  1604. * 企业在开启二次验证时,必须填写企业二次验证页面的url。
  1605. * 当员工绑定通讯录中的帐号后,会收到一条图文消息,
  1606. * 引导员工到企业的验证页面验证身份,企业在员工验证成功后,
  1607. * 调用如下接口即可让员工关注成功。
  1608. *
  1609. * @param $userid
  1610. * @return boolean|array 成功返回结果
  1611. * {
  1612. * "errcode": 0, //返回码
  1613. * "errmsg": "ok" //对返回码的文本描述内容
  1614. * }
  1615. */
  1616. public function authSucc($userid){
  1617. if (!$this->access_token && !$this->checkAuth()) return false;
  1618. $result = $this->http_get(self::API_URL_PREFIX.self::AUTHSUCC_URL.'access_token='.$this->access_token.'&userid='.$userid);
  1619. if ($result)
  1620. {
  1621. $json = json_decode($result,true);
  1622. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1623. $this->errCode = $json['errcode'];
  1624. $this->errMsg = $json['errmsg'];
  1625. return false;
  1626. }
  1627. return $json;
  1628. }
  1629. return false;
  1630. }
  1631. /**
  1632. * oauth 授权跳转接口
  1633. * @param string $callback 回调URI
  1634. * @param string $state 重定向后会带上state参数,企业可以填写a-zA-Z0-9的参数值
  1635. * @return string
  1636. */
  1637. public function getOauthRedirect($callback,$state='STATE',$scope='snsapi_base'){
  1638. return self::OAUTH_PREFIX.self::OAUTH_AUTHORIZE_URL.'appid='.$this->appid.'&redirect_uri='.urlencode($callback).'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';
  1639. }
  1640. }
  1641. /**
  1642. * PKCS7Encoder class
  1643. *
  1644. * 提供基于PKCS7算法的加解密接口.
  1645. */
  1646. class PKCS7Encoder
  1647. {
  1648. public static $block_size = 32;
  1649. /**
  1650. * 对需要加密的明文进行填充补位
  1651. * @param $text 需要进行填充补位操作的明文
  1652. * @return 补齐明文字符串
  1653. */
  1654. function encode($text)
  1655. {
  1656. $block_size = PKCS7Encoder::$block_size;
  1657. $text_length = strlen($text);
  1658. //计算需要填充的位数
  1659. $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
  1660. if ($amount_to_pad == 0) {
  1661. $amount_to_pad = PKCS7Encoder::block_size;
  1662. }
  1663. //获得补位所用的字符
  1664. $pad_chr = chr($amount_to_pad);
  1665. $tmp = "";
  1666. for ($index = 0; $index < $amount_to_pad; $index++) {
  1667. $tmp .= $pad_chr;
  1668. }
  1669. return $text . $tmp;
  1670. }
  1671. /**
  1672. * 对解密后的明文进行补位删除
  1673. * @param decrypted 解密后的明文
  1674. * @return 删除填充补位后的明文
  1675. */
  1676. function decode($text)
  1677. {
  1678. $pad = ord(substr($text, -1));
  1679. if ($pad < 1 || $pad > PKCS7Encoder::$block_size) {
  1680. $pad = 0;
  1681. }
  1682. return substr($text, 0, (strlen($text) - $pad));
  1683. }
  1684. }
  1685. /**
  1686. * Prpcrypt class
  1687. *
  1688. * 提供接收和推送给公众平台消息的加解密接口.
  1689. */
  1690. class Prpcrypt
  1691. {
  1692. public $key;
  1693. function Prpcrypt($k)
  1694. {
  1695. $this->key = base64_decode($k . "=");
  1696. }
  1697. /**
  1698. * 对明文进行加密
  1699. * @param string $text 需要加密的明文
  1700. * @return string 加密后的密文
  1701. */
  1702. public function encrypt($text, $appid)
  1703. {
  1704. try {
  1705. //获得16位随机字符串,填充到明文之前
  1706. $random = $this->getRandomStr();//"aaaabbbbccccdddd";
  1707. $text = $random . pack("N", strlen($text)) . $text . $appid;
  1708. // 网络字节序
  1709. $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  1710. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  1711. $iv = substr($this->key, 0, 16);
  1712. //使用自定义的填充方式对明文进行补位填充
  1713. $pkc_encoder = new PKCS7Encoder;
  1714. $text = $pkc_encoder->encode($text);
  1715. mcrypt_generic_init($module, $this->key, $iv);
  1716. //加密
  1717. $encrypted = mcrypt_generic($module, $text);
  1718. mcrypt_generic_deinit($module);
  1719. mcrypt_module_close($module);
  1720. // print(base64_encode($encrypted));
  1721. //使用BASE64对加密后的字符串进行编码
  1722. return array(ErrorCode::$OK, base64_encode($encrypted));
  1723. } catch (Exception $e) {
  1724. //print $e;
  1725. return array(ErrorCode::$EncryptAESError, null);
  1726. }
  1727. }
  1728. /**
  1729. * 对密文进行解密
  1730. * @param string $encrypted 需要解密的密文
  1731. * @return string 解密得到的明文
  1732. */
  1733. public function decrypt($encrypted, $appid)
  1734. {
  1735. try {
  1736. //使用BASE64对需要解密的字符串进行解码
  1737. $ciphertext_dec = base64_decode($encrypted);
  1738. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  1739. $iv = substr($this->key, 0, 16);
  1740. mcrypt_generic_init($module, $this->key, $iv);
  1741. //解密
  1742. $decrypted = mdecrypt_generic($module, $ciphertext_dec);
  1743. mcrypt_generic_deinit($module);
  1744. mcrypt_module_close($module);
  1745. } catch (Exception $e) {
  1746. return array(ErrorCode::$DecryptAESError, null);
  1747. }
  1748. try {
  1749. //去除补位字符
  1750. $pkc_encoder = new PKCS7Encoder;
  1751. $result = $pkc_encoder->decode($decrypted);
  1752. //去除16位随机字符串,网络字节序和AppId
  1753. if (strlen($result) < 16)
  1754. return "";
  1755. $content = substr($result, 16, strlen($result));
  1756. $len_list = unpack("N", substr($content, 0, 4));
  1757. $xml_len = $len_list[1];
  1758. $xml_content = substr($content, 4, $xml_len);
  1759. $from_appid = substr($content, $xml_len + 4);
  1760. } catch (Exception $e) {
  1761. //print $e;
  1762. return array(ErrorCode::$IllegalBuffer, null);
  1763. }
  1764. if ($from_appid != $appid)
  1765. return array(ErrorCode::$ValidateAppidError, null);
  1766. return array(0, $xml_content);
  1767. }
  1768. /**
  1769. * 随机生成16位字符串
  1770. * @return string 生成的字符串
  1771. */
  1772. function getRandomStr()
  1773. {
  1774. $str = "";
  1775. $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  1776. $max = strlen($str_pol) - 1;
  1777. for ($i = 0; $i < 16; $i++) {
  1778. $str .= $str_pol[mt_rand(0, $max)];
  1779. }
  1780. return $str;
  1781. }
  1782. }
  1783. /**
  1784. * error code
  1785. * 仅用作类内部使用,不用于官方API接口的errCode码
  1786. */
  1787. class ErrorCode
  1788. {
  1789. public static $OK = 0;
  1790. public static $ValidateSignatureError = 40001;
  1791. public static $ParseXmlError = 40002;
  1792. public static $ComputeSignatureError = 40003;
  1793. public static $IllegalAesKey = 40004;
  1794. public static $ValidateAppidError = 40005;
  1795. public static $EncryptAESError = 40006;
  1796. public static $DecryptAESError = 40007;
  1797. public static $IllegalBuffer = 40008;
  1798. public static $EncodeBase64Error = 40009;
  1799. public static $DecodeBase64Error = 40010;
  1800. public static $GenReturnXmlError = 40011;
  1801. public static $errCode=array(
  1802. '0'=>'无问题',
  1803. '40001'=>'签名验证错误',
  1804. '40002'=>'xml解析失败',
  1805. '40003'=>'sha加密生成签名失败',
  1806. '40004'=>'encodingAesKey 非法',
  1807. '40005'=>'appid 校验错误',
  1808. '40006'=>'aes 加密失败',
  1809. '40007'=>'aes 解密失败',
  1810. '40008'=>'解密后得到的buffer非法',
  1811. '40009'=>'base64加密失败',
  1812. '40010'=>'base64解密失败',
  1813. '40011'=>'生成xml失败',
  1814. );
  1815. public static function getErrText($err) {
  1816. if (isset(self::$errCode[$err])) {
  1817. return self::$errCode[$err];
  1818. }else {
  1819. return false;
  1820. };
  1821. }
  1822. }