qywechat.class.php 58 KB

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