qywechat.class.php 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  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. curl_setopt($oCurl, CURLOPT_SSLVERSION, 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, 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. * 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
  926. * @param array $data {"media":'@Path\filename.jpg'}
  927. * @param type 媒体文件类型:图片(image)、语音(voice)、视频(video),普通文件(file)
  928. * @return boolean|array
  929. * {
  930. * "type": "image",
  931. * "media_id": "0000001",
  932. * "created_at": "1380000000"
  933. * }
  934. */
  935. public function uploadMedia($data, $type){
  936. if (!$this->access_token && !$this->checkAuth()) return false;
  937. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOAD.'access_token='.$this->access_token.'&type='.$type,$data,true);
  938. if ($result)
  939. {
  940. $json = json_decode($result,true);
  941. if (!$json || !empty($json['errcode'])) {
  942. $this->errCode = $json['errcode'];
  943. $this->errMsg = $json['errmsg'];
  944. return false;
  945. }
  946. return $json;
  947. }
  948. return false;
  949. }
  950. /**
  951. * 根据媒体文件ID获取媒体文件
  952. * @param string $media_id 媒体文件id
  953. * @return raw data
  954. */
  955. public function getMedia($media_id){
  956. if (!$this->access_token && !$this->checkAuth()) return false;
  957. $result = $this->http_get(self::API_URL_PREFIX.self::MEDIA_GET_URL.'access_token='.$this->access_token.'&media_id='.$media_id);
  958. if ($result)
  959. {
  960. $json = json_decode($result,true);
  961. if (isset($json['errcode'])) {
  962. $this->errCode = $json['errcode'];
  963. $this->errMsg = $json['errmsg'];
  964. return false;
  965. }
  966. return $result;
  967. }
  968. return false;
  969. }
  970. /**
  971. * 创建部门
  972. * @param array $data 结构体为:
  973. * array (
  974. * "name" => "邮箱产品组", //部门名称
  975. * "parentid" => "1" //父部门id
  976. * "order" => "1", //(非必须)在父部门中的次序。从1开始,数字越大排序越靠后
  977. * )
  978. * @return boolean|array
  979. * 成功返回结果
  980. * {
  981. * "errcode": 0, //返回码
  982. * "errmsg": "created", //对返回码的文本描述内容
  983. * "id": 2 //创建的部门id。
  984. * }
  985. */
  986. public function createDepartment($data){
  987. if (!$this->access_token && !$this->checkAuth()) return false;
  988. $result = $this->http_post(self::API_URL_PREFIX.self::DEPARTMENT_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  989. if ($result)
  990. {
  991. $json = json_decode($result,true);
  992. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  993. $this->errCode = $json['errcode'];
  994. $this->errMsg = $json['errmsg'];
  995. return false;
  996. }
  997. return $json;
  998. }
  999. return false;
  1000. }
  1001. /**
  1002. * 更新部门
  1003. * @param array $data 结构体为:
  1004. * array(
  1005. * "id" => "1" //(必须)部门id
  1006. * "name" => "邮箱产品组", //(非必须)部门名称
  1007. * "parentid" => "1", //(非必须)父亲部门id。根部门id为1
  1008. * "order" => "1", //(非必须)在父部门中的次序。从1开始,数字越大排序越靠后
  1009. * )
  1010. * @return boolean|array 成功返回结果
  1011. * {
  1012. * "errcode": 0, //返回码
  1013. * "errmsg": "updated" //对返回码的文本描述内容
  1014. * }
  1015. */
  1016. public function updateDepartment($data){
  1017. if (!$this->access_token && !$this->checkAuth()) return false;
  1018. $result = $this->http_post(self::API_URL_PREFIX.self::DEPARTMENT_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1019. if ($result)
  1020. {
  1021. $json = json_decode($result,true);
  1022. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1023. $this->errCode = $json['errcode'];
  1024. $this->errMsg = $json['errmsg'];
  1025. return false;
  1026. }
  1027. return $json;
  1028. }
  1029. return false;
  1030. }
  1031. /**
  1032. * 删除部门
  1033. * @param $id
  1034. * @return boolean|array 成功返回结果
  1035. * {
  1036. * "errcode": 0, //返回码
  1037. * "errmsg": "deleted" //对返回码的文本描述内容
  1038. * }
  1039. */
  1040. public function deleteDepartment($id){
  1041. if (!$this->access_token && !$this->checkAuth()) return false;
  1042. $result = $this->http_get(self::API_URL_PREFIX.self::DEPARTMENT_DELETE_URL.'access_token='.$this->access_token.'&id='.$id);
  1043. if ($result)
  1044. {
  1045. $json = json_decode($result,true);
  1046. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1047. $this->errCode = $json['errcode'];
  1048. $this->errMsg = $json['errmsg'];
  1049. return false;
  1050. }
  1051. return $json;
  1052. }
  1053. return false;
  1054. }
  1055. /**
  1056. * 移动部门
  1057. * @param $data
  1058. * array(
  1059. * "department_id" => "5", //所要移动的部门
  1060. * "to_parentid" => "2", //想移动到的父部门节点,根部门为1
  1061. * "to_position" => "1" //(非必须)想移动到的父部门下的位置,1表示最上方,往后位置为2,3,4,以此类推,默认为1
  1062. * )
  1063. * @return boolean|array 成功返回结果
  1064. * {
  1065. * "errcode": 0, //返回码
  1066. * "errmsg": "ok" //对返回码的文本描述内容
  1067. * }
  1068. */
  1069. public function moveDepartment($data){
  1070. if (!$this->access_token && !$this->checkAuth()) return false;
  1071. $result = $this->http_get(self::API_URL_PREFIX.self::DEPARTMENT_MOVE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1072. if ($result)
  1073. {
  1074. $json = json_decode($result,true);
  1075. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1076. $this->errCode = $json['errcode'];
  1077. $this->errMsg = $json['errmsg'];
  1078. return false;
  1079. }
  1080. return $json;
  1081. }
  1082. return false;
  1083. }
  1084. /**
  1085. * 获取部门列表
  1086. * @return boolean|array 成功返回结果
  1087. * {
  1088. * "errcode": 0,
  1089. * "errmsg": "ok",
  1090. * "department": [ //部门列表数据。以部门的order字段从小到大排列
  1091. * {
  1092. * "id": 1,
  1093. * "name": "广州研发中心",
  1094. * "parentid": 0
  1095. * },
  1096. * {
  1097. * "id": 2
  1098. * "name": "邮箱产品部",
  1099. * "parentid": 1
  1100. * }
  1101. * ]
  1102. * }
  1103. */
  1104. public function getDepartment(){
  1105. if (!$this->access_token && !$this->checkAuth()) return false;
  1106. $result = $this->http_get(self::API_URL_PREFIX.self::DEPARTMENT_LIST_URL.'access_token='.$this->access_token);
  1107. if ($result)
  1108. {
  1109. $json = json_decode($result,true);
  1110. if (!$json || !empty($json['errcode'])) {
  1111. $this->errCode = $json['errcode'];
  1112. $this->errMsg = $json['errmsg'];
  1113. return false;
  1114. }
  1115. return $json;
  1116. }
  1117. return false;
  1118. }
  1119. /**
  1120. * 创建成员
  1121. * @param array $data 结构体为:
  1122. * array(
  1123. * "userid" => "zhangsan",
  1124. * "name" => "张三",
  1125. * "department" => [1, 2],
  1126. * "position" => "产品经理",
  1127. * "mobile" => "15913215421",
  1128. * "gender" => 1, //性别。gender=0表示男,=1表示女
  1129. * "tel" => "62394",
  1130. * "email" => "zhangsan@gzdev.com",
  1131. * "weixinid" => "zhangsan4dev"
  1132. * )
  1133. * @return boolean|array
  1134. * 成功返回结果
  1135. * {
  1136. * "errcode": 0, //返回码
  1137. * "errmsg": "created", //对返回码的文本描述内容
  1138. * }
  1139. */
  1140. public function createUser($data){
  1141. if (!$this->access_token && !$this->checkAuth()) return false;
  1142. $result = $this->http_post(self::API_URL_PREFIX.self::USER_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1143. if ($result)
  1144. {
  1145. $json = json_decode($result,true);
  1146. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1147. $this->errCode = $json['errcode'];
  1148. $this->errMsg = $json['errmsg'];
  1149. return false;
  1150. }
  1151. return $json;
  1152. }
  1153. return false;
  1154. }
  1155. /**
  1156. * 更新成员
  1157. * @param array $data 结构体为:
  1158. * array(
  1159. * "userid" => "zhangsan",
  1160. * "name" => "张三",
  1161. * "department" => [1, 2],
  1162. * "position" => "产品经理",
  1163. * "mobile" => "15913215421",
  1164. * "gender" => 1, //性别。gender=0表示男,=1表示女
  1165. * "tel" => "62394",
  1166. * "email" => "zhangsan@gzdev.com",
  1167. * "weixinid" => "zhangsan4dev"
  1168. * )
  1169. * @return boolean|array 成功返回结果
  1170. * {
  1171. * "errcode": 0, //返回码
  1172. * "errmsg": "updated" //对返回码的文本描述内容
  1173. * }
  1174. */
  1175. public function updateUser($data){
  1176. if (!$this->access_token && !$this->checkAuth()) return false;
  1177. $result = $this->http_post(self::API_URL_PREFIX.self::USER_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1178. if ($result)
  1179. {
  1180. $json = json_decode($result,true);
  1181. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1182. $this->errCode = $json['errcode'];
  1183. $this->errMsg = $json['errmsg'];
  1184. return false;
  1185. }
  1186. return $json;
  1187. }
  1188. return false;
  1189. }
  1190. /**
  1191. * 删除成员
  1192. * @param $userid 员工UserID。对应管理端的帐号
  1193. * @return boolean|array 成功返回结果
  1194. * {
  1195. * "errcode": 0, //返回码
  1196. * "errmsg": "deleted" //对返回码的文本描述内容
  1197. * }
  1198. */
  1199. public function deleteUser($userid){
  1200. if (!$this->access_token && !$this->checkAuth()) return false;
  1201. $result = $this->http_get(self::API_URL_PREFIX.self::USER_DELETE_URL.'access_token='.$this->access_token.'&userid='.$userid);
  1202. if ($result)
  1203. {
  1204. $json = json_decode($result,true);
  1205. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1206. $this->errCode = $json['errcode'];
  1207. $this->errMsg = $json['errmsg'];
  1208. return false;
  1209. }
  1210. return $json;
  1211. }
  1212. return false;
  1213. }
  1214. /**
  1215. * 获取成员信息
  1216. * @param $userid 员工UserID。对应管理端的帐号
  1217. * @return boolean|array 成功返回结果
  1218. * {
  1219. * "errcode": 0,
  1220. * "errmsg": "ok",
  1221. * "userid": "zhangsan",
  1222. * "name": "李四",
  1223. * "department": [1, 2],
  1224. * "position": "后台工程师",
  1225. * "mobile": "15913215421",
  1226. * "gender": 1, //性别。gender=0表示男,=1表示女
  1227. * "tel": "62394",
  1228. * "email": "zhangsan@gzdev.com",
  1229. * "weixinid": "lisifordev", //微信号
  1230. * "avatar": "http://wx.qlogo.cn/mmopen/ajNVdqHZLLA3W..../0", //头像url。注:如果要获取小图将url最后的"/0"改成"/64"即可
  1231. * "status": 1 //关注状态: 1=已关注,2=已冻结,4=未关注
  1232. * }
  1233. */
  1234. public function getUserInfo($userid){
  1235. if (!$this->access_token && !$this->checkAuth()) return false;
  1236. $result = $this->http_get(self::API_URL_PREFIX.self::USER_GET_URL.'access_token='.$this->access_token.'&userid='.$userid);
  1237. if ($result)
  1238. {
  1239. $json = json_decode($result,true);
  1240. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1241. $this->errCode = $json['errcode'];
  1242. $this->errMsg = $json['errmsg'];
  1243. return false;
  1244. }
  1245. return $json;
  1246. }
  1247. return false;
  1248. }
  1249. /**
  1250. * 获取部门成员
  1251. * @param $department_id 部门id
  1252. * @param $fetch_child 1/0:是否递归获取子部门下面的成员
  1253. * @param $status 0获取全部员工,1获取已关注成员列表,2获取禁用成员列表,4获取未关注成员列表。status可叠加
  1254. * @return boolean|array 成功返回结果
  1255. * {
  1256. * "errcode": 0,
  1257. * "errmsg": "ok",
  1258. * "userlist": [
  1259. * {
  1260. * "userid": "zhangsan",
  1261. * "name": "李四"
  1262. * }
  1263. * ]
  1264. * }
  1265. */
  1266. public function getUserList($department_id,$fetch_child=0,$status=0){
  1267. if (!$this->access_token && !$this->checkAuth()) return false;
  1268. $result = $this->http_get(self::API_URL_PREFIX.self::USER_LIST_URL.'access_token='.$this->access_token
  1269. .'&department_id='.$department_id.'&fetch_child='.$fetch_child.'&status='.$status);
  1270. if ($result)
  1271. {
  1272. $json = json_decode($result,true);
  1273. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1274. $this->errCode = $json['errcode'];
  1275. $this->errMsg = $json['errmsg'];
  1276. return false;
  1277. }
  1278. return $json;
  1279. }
  1280. return false;
  1281. }
  1282. /**
  1283. * 根据code获取成员id
  1284. * 通过Oauth2.0或者设置了二次验证时获取的code,用于换取成员的userid
  1285. *
  1286. * @param $code Oauth2.0或者二次验证时返回的code值
  1287. * @param $agentid 跳转链接时所在的企业应用ID,未填则默认为当前配置的应用id
  1288. * @return boolean|string 成功返回userid
  1289. */
  1290. public function getUserId($code,$agentid=0){
  1291. if (!$agentid) $agentid=$this->agentid;
  1292. if (!$this->access_token && !$this->checkAuth()) return false;
  1293. $result = $this->http_get(self::API_URL_PREFIX.self::USER_GETINFO_URL.'access_token='.$this->access_token.'&code='.$code.'&agentid'.$agentid);
  1294. if ($result)
  1295. {
  1296. $json = json_decode($result,true);
  1297. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1298. $this->errCode = $json['errcode'];
  1299. $this->errMsg = $json['errmsg'];
  1300. return false;
  1301. }
  1302. return $json['UserId'];
  1303. }
  1304. return false;
  1305. }
  1306. /**
  1307. * 创建标签
  1308. * @param array $data 结构体为:
  1309. * array(
  1310. * "tagname" => "UI"
  1311. * )
  1312. * @return boolean|array
  1313. * 成功返回结果
  1314. * {
  1315. * "errcode": 0, //返回码
  1316. * "errmsg": "created", //对返回码的文本描述内容
  1317. * "tagid": "1"
  1318. * }
  1319. */
  1320. public function createTag($data){
  1321. if (!$this->access_token && !$this->checkAuth()) return false;
  1322. $result = $this->http_post(self::API_URL_PREFIX.self::TAG_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1323. if ($result)
  1324. {
  1325. $json = json_decode($result,true);
  1326. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1327. $this->errCode = $json['errcode'];
  1328. $this->errMsg = $json['errmsg'];
  1329. return false;
  1330. }
  1331. return $json;
  1332. }
  1333. return false;
  1334. }
  1335. /**
  1336. * 更新标签
  1337. * @param array $data 结构体为:
  1338. * array(
  1339. * "tagid" => "1",
  1340. * "tagname" => "UI design"
  1341. * )
  1342. * @return boolean|array 成功返回结果
  1343. * {
  1344. * "errcode": 0, //返回码
  1345. * "errmsg": "updated" //对返回码的文本描述内容
  1346. * }
  1347. */
  1348. public function updateTag($data){
  1349. if (!$this->access_token && !$this->checkAuth()) return false;
  1350. $result = $this->http_post(self::API_URL_PREFIX.self::TAG_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1351. if ($result)
  1352. {
  1353. $json = json_decode($result,true);
  1354. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1355. $this->errCode = $json['errcode'];
  1356. $this->errMsg = $json['errmsg'];
  1357. return false;
  1358. }
  1359. return $json;
  1360. }
  1361. return false;
  1362. }
  1363. /**
  1364. * 删除标签
  1365. * @param $tagid 标签TagID
  1366. * @return boolean|array 成功返回结果
  1367. * {
  1368. * "errcode": 0, //返回码
  1369. * "errmsg": "deleted" //对返回码的文本描述内容
  1370. * }
  1371. */
  1372. public function deleteTag($tagid){
  1373. if (!$this->access_token && !$this->checkAuth()) return false;
  1374. $result = $this->http_get(self::API_URL_PREFIX.self::TAG_DELETE_URL.'access_token='.$this->access_token.'&tagid='.$tagid);
  1375. if ($result)
  1376. {
  1377. $json = json_decode($result,true);
  1378. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1379. $this->errCode = $json['errcode'];
  1380. $this->errMsg = $json['errmsg'];
  1381. return false;
  1382. }
  1383. return $json;
  1384. }
  1385. return false;
  1386. }
  1387. /**
  1388. * 获取标签成员
  1389. * @param $tagid 标签TagID
  1390. * @return boolean|array 成功返回结果
  1391. * {
  1392. * "errcode": 0,
  1393. * "errmsg": "ok",
  1394. * "userlist": [
  1395. * {
  1396. * "userid": "zhangsan",
  1397. * "name": "李四"
  1398. * }
  1399. * ]
  1400. * }
  1401. */
  1402. public function getTag($tagid){
  1403. if (!$this->access_token && !$this->checkAuth()) return false;
  1404. $result = $this->http_get(self::API_URL_PREFIX.self::TAG_GET_URL.'access_token='.$this->access_token.'&tagid='.$tagid);
  1405. if ($result)
  1406. {
  1407. $json = json_decode($result,true);
  1408. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1409. $this->errCode = $json['errcode'];
  1410. $this->errMsg = $json['errmsg'];
  1411. return false;
  1412. }
  1413. return $json;
  1414. }
  1415. return false;
  1416. }
  1417. /**
  1418. * 增加标签成员
  1419. * @param array $data 结构体为:
  1420. * array (
  1421. * "tagid" => "1",
  1422. * "userlist" => array( //企业员工ID列表
  1423. * "user1",
  1424. * "user2"
  1425. * )
  1426. * )
  1427. * @return boolean|array
  1428. * 成功返回结果
  1429. * {
  1430. * "errcode": 0, //返回码
  1431. * "errmsg": "ok", //对返回码的文本描述内容
  1432. * "invalidlist":"usr1|usr2|usr" //若部分userid非法,则会有此段。不在权限内的员工ID列表,以“|”分隔
  1433. * }
  1434. */
  1435. public function addTagUser($data){
  1436. if (!$this->access_token && !$this->checkAuth()) return false;
  1437. $result = $this->http_post(self::API_URL_PREFIX.self::TAG_ADDUSER_URL.'access_token='.$this->access_token,self::json_encode($data));
  1438. if ($result)
  1439. {
  1440. $json = json_decode($result,true);
  1441. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1442. $this->errCode = $json['errcode'];
  1443. $this->errMsg = $json['errmsg'];
  1444. return false;
  1445. }
  1446. return $json;
  1447. }
  1448. return false;
  1449. }
  1450. /**
  1451. * 删除标签成员
  1452. * @param array $data 结构体为:
  1453. * array (
  1454. * "tagid" => "1",
  1455. * "userlist" => array( //企业员工ID列表
  1456. * "user1",
  1457. * "user2"
  1458. * )
  1459. * )
  1460. * @return boolean|array
  1461. * 成功返回结果
  1462. * {
  1463. * "errcode": 0, //返回码
  1464. * "errmsg": "deleted", //对返回码的文本描述内容
  1465. * "invalidlist":"usr1|usr2|usr" //若部分userid非法,则会有此段。不在权限内的员工ID列表,以“|”分隔
  1466. * }
  1467. */
  1468. public function delTagUser($data){
  1469. if (!$this->access_token && !$this->checkAuth()) return false;
  1470. $result = $this->http_post(self::API_URL_PREFIX.self::TAG_DELUSER_URL.'access_token='.$this->access_token,self::json_encode($data));
  1471. if ($result)
  1472. {
  1473. $json = json_decode($result,true);
  1474. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1475. $this->errCode = $json['errcode'];
  1476. $this->errMsg = $json['errmsg'];
  1477. return false;
  1478. }
  1479. return $json;
  1480. }
  1481. return false;
  1482. }
  1483. /**
  1484. * 获取标签列表
  1485. * @return boolean|array 成功返回数组结果,这里附上json样例
  1486. * {
  1487. * "errcode": 0,
  1488. * "errmsg": "ok",
  1489. * "taglist":[
  1490. * {"tagid":1,"tagname":"a"},
  1491. * {"tagid":2,"tagname":"b"}
  1492. * ]
  1493. * }
  1494. */
  1495. public function getTagList(){
  1496. if (!$this->access_token && !$this->checkAuth()) return false;
  1497. $result = $this->http_get(self::API_URL_PREFIX.self::TAG_LIST_URL.'access_token='.$this->access_token);
  1498. if ($result)
  1499. {
  1500. $json = json_decode($result,true);
  1501. if (!$json || !empty($json['errcode'])) {
  1502. $this->errCode = $json['errcode'];
  1503. $this->errMsg = $json['errmsg'];
  1504. return false;
  1505. }
  1506. return $json;
  1507. }
  1508. return false;
  1509. }
  1510. /**
  1511. * 主动发送信息接口
  1512. * @param array $data 结构体为:
  1513. * array(
  1514. * "touser" => "UserID1|UserID2|UserID3",
  1515. * "toparty" => "PartyID1|PartyID2 ",
  1516. * "totag" => "TagID1|TagID2 ",
  1517. * "safe":"0" //是否为保密消息,对于news无效
  1518. * "agentid" => "001", //应用id
  1519. * "msgtype" => "text", //根据信息类型,选择下面对应的信息结构体
  1520. *
  1521. * "text" => array(
  1522. * "content" => "Holiday Request For Pony(http://xxxxx)"
  1523. * ),
  1524. *
  1525. * "image" => array(
  1526. * "media_id" => "MEDIA_ID"
  1527. * ),
  1528. *
  1529. * "voice" => array(
  1530. * "media_id" => "MEDIA_ID"
  1531. * ),
  1532. *
  1533. * " video" => array(
  1534. * "media_id" => "MEDIA_ID",
  1535. * "title" => "Title",
  1536. * "description" => "Description"
  1537. * ),
  1538. *
  1539. * "file" => array(
  1540. * "media_id" => "MEDIA_ID"
  1541. * ),
  1542. *
  1543. * "news" => array( //不支持保密
  1544. * "articles" => array( //articles 图文消息,一个图文消息支持1到10个图文
  1545. * array(
  1546. * "title" => "Title", //标题
  1547. * "description" => "Description", //描述
  1548. * "url" => "URL", //点击后跳转的链接。可根据url里面带的code参数校验员工的真实身份。
  1549. * "picurl" => "PIC_URL", //图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图640*320,
  1550. * //小图80*80。如不填,在客户端不显示图片
  1551. * ),
  1552. * )
  1553. * ),
  1554. *
  1555. * "mpnews" => array(
  1556. * "articles" => array( //articles 图文消息,一个图文消息支持1到10个图文
  1557. * array(
  1558. * "title" => "Title", //图文消息的标题
  1559. * "thumb_media_id" => "id", //图文消息缩略图的media_id
  1560. * "author" => "Author", //图文消息的作者(可空)
  1561. * "content_source_url" => "URL", //图文消息点击“阅读原文”之后的页面链接(可空)
  1562. * "content" => "Content" //图文消息的内容,支持html标签
  1563. * "digest" => "Digest description", //图文消息的描述
  1564. * "show_cover_pic" => "0" //是否显示封面,1为显示,0为不显示(可空)
  1565. * ),
  1566. * )
  1567. * )
  1568. * )
  1569. * 请查看官方开发文档中的 发送消息 -> 消息类型及数据格式
  1570. *
  1571. * @return boolean|array
  1572. * 如果对应用或收件人、部门、标签任何一个无权限,则本次发送失败;
  1573. * 如果收件人、部门或标签不存在,发送仍然执行,但返回无效的部分。
  1574. * {
  1575. * "errcode": 0,
  1576. * "errmsg": "ok",
  1577. * "invaliduser": "UserID1",
  1578. * "invalidparty":"PartyID1",
  1579. * "invalidtag":"TagID1"
  1580. * }
  1581. */
  1582. public function sendMessage($data){
  1583. if (!$this->access_token && !$this->checkAuth()) return false;
  1584. $result = $this->http_post(self::API_URL_PREFIX.self::MASS_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
  1585. if ($result)
  1586. {
  1587. $json = json_decode($result,true);
  1588. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1589. $this->errCode = $json['errcode'];
  1590. $this->errMsg = $json['errmsg'];
  1591. return false;
  1592. }
  1593. return $json;
  1594. }
  1595. return false;
  1596. }
  1597. /**
  1598. * 二次验证
  1599. * 企业在开启二次验证时,必须填写企业二次验证页面的url。
  1600. * 当员工绑定通讯录中的帐号后,会收到一条图文消息,
  1601. * 引导员工到企业的验证页面验证身份,企业在员工验证成功后,
  1602. * 调用如下接口即可让员工关注成功。
  1603. *
  1604. * @param $userid
  1605. * @return boolean|array 成功返回结果
  1606. * {
  1607. * "errcode": 0, //返回码
  1608. * "errmsg": "ok" //对返回码的文本描述内容
  1609. * }
  1610. */
  1611. public function authSucc($userid){
  1612. if (!$this->access_token && !$this->checkAuth()) return false;
  1613. $result = $this->http_get(self::API_URL_PREFIX.self::AUTHSUCC_URL.'access_token='.$this->access_token.'&userid='.$userid);
  1614. if ($result)
  1615. {
  1616. $json = json_decode($result,true);
  1617. if (!$json || !empty($json['errcode']) || $json['errcode']!=0) {
  1618. $this->errCode = $json['errcode'];
  1619. $this->errMsg = $json['errmsg'];
  1620. return false;
  1621. }
  1622. return $json;
  1623. }
  1624. return false;
  1625. }
  1626. /**
  1627. * oauth 授权跳转接口
  1628. * @param string $callback 回调URI
  1629. * @param string $state 重定向后会带上state参数,企业可以填写a-zA-Z0-9的参数值
  1630. * @return string
  1631. */
  1632. public function getOauthRedirect($callback,$state='STATE',$scope='snsapi_base'){
  1633. return self::OAUTH_PREFIX.self::OAUTH_AUTHORIZE_URL.'appid='.$this->appid.'&redirect_uri='.urlencode($callback).'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';
  1634. }
  1635. }
  1636. /**
  1637. * PKCS7Encoder class
  1638. *
  1639. * 提供基于PKCS7算法的加解密接口.
  1640. */
  1641. class PKCS7Encoder
  1642. {
  1643. public static $block_size = 32;
  1644. /**
  1645. * 对需要加密的明文进行填充补位
  1646. * @param $text 需要进行填充补位操作的明文
  1647. * @return 补齐明文字符串
  1648. */
  1649. function encode($text)
  1650. {
  1651. $block_size = PKCS7Encoder::$block_size;
  1652. $text_length = strlen($text);
  1653. //计算需要填充的位数
  1654. $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
  1655. if ($amount_to_pad == 0) {
  1656. $amount_to_pad = PKCS7Encoder::block_size;
  1657. }
  1658. //获得补位所用的字符
  1659. $pad_chr = chr($amount_to_pad);
  1660. $tmp = "";
  1661. for ($index = 0; $index < $amount_to_pad; $index++) {
  1662. $tmp .= $pad_chr;
  1663. }
  1664. return $text . $tmp;
  1665. }
  1666. /**
  1667. * 对解密后的明文进行补位删除
  1668. * @param decrypted 解密后的明文
  1669. * @return 删除填充补位后的明文
  1670. */
  1671. function decode($text)
  1672. {
  1673. $pad = ord(substr($text, -1));
  1674. if ($pad < 1 || $pad > PKCS7Encoder::$block_size) {
  1675. $pad = 0;
  1676. }
  1677. return substr($text, 0, (strlen($text) - $pad));
  1678. }
  1679. }
  1680. /**
  1681. * Prpcrypt class
  1682. *
  1683. * 提供接收和推送给公众平台消息的加解密接口.
  1684. */
  1685. class Prpcrypt
  1686. {
  1687. public $key;
  1688. function Prpcrypt($k)
  1689. {
  1690. $this->key = base64_decode($k . "=");
  1691. }
  1692. /**
  1693. * 对明文进行加密
  1694. * @param string $text 需要加密的明文
  1695. * @return string 加密后的密文
  1696. */
  1697. public function encrypt($text, $appid)
  1698. {
  1699. try {
  1700. //获得16位随机字符串,填充到明文之前
  1701. $random = $this->getRandomStr();//"aaaabbbbccccdddd";
  1702. $text = $random . pack("N", strlen($text)) . $text . $appid;
  1703. // 网络字节序
  1704. $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  1705. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  1706. $iv = substr($this->key, 0, 16);
  1707. //使用自定义的填充方式对明文进行补位填充
  1708. $pkc_encoder = new PKCS7Encoder;
  1709. $text = $pkc_encoder->encode($text);
  1710. mcrypt_generic_init($module, $this->key, $iv);
  1711. //加密
  1712. $encrypted = mcrypt_generic($module, $text);
  1713. mcrypt_generic_deinit($module);
  1714. mcrypt_module_close($module);
  1715. // print(base64_encode($encrypted));
  1716. //使用BASE64对加密后的字符串进行编码
  1717. return array(ErrorCode::$OK, base64_encode($encrypted));
  1718. } catch (Exception $e) {
  1719. //print $e;
  1720. return array(ErrorCode::$EncryptAESError, null);
  1721. }
  1722. }
  1723. /**
  1724. * 对密文进行解密
  1725. * @param string $encrypted 需要解密的密文
  1726. * @return string 解密得到的明文
  1727. */
  1728. public function decrypt($encrypted, $appid)
  1729. {
  1730. try {
  1731. //使用BASE64对需要解密的字符串进行解码
  1732. $ciphertext_dec = base64_decode($encrypted);
  1733. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  1734. $iv = substr($this->key, 0, 16);
  1735. mcrypt_generic_init($module, $this->key, $iv);
  1736. //解密
  1737. $decrypted = mdecrypt_generic($module, $ciphertext_dec);
  1738. mcrypt_generic_deinit($module);
  1739. mcrypt_module_close($module);
  1740. } catch (Exception $e) {
  1741. return array(ErrorCode::$DecryptAESError, null);
  1742. }
  1743. try {
  1744. //去除补位字符
  1745. $pkc_encoder = new PKCS7Encoder;
  1746. $result = $pkc_encoder->decode($decrypted);
  1747. //去除16位随机字符串,网络字节序和AppId
  1748. if (strlen($result) < 16)
  1749. return "";
  1750. $content = substr($result, 16, strlen($result));
  1751. $len_list = unpack("N", substr($content, 0, 4));
  1752. $xml_len = $len_list[1];
  1753. $xml_content = substr($content, 4, $xml_len);
  1754. $from_appid = substr($content, $xml_len + 4);
  1755. } catch (Exception $e) {
  1756. //print $e;
  1757. return array(ErrorCode::$IllegalBuffer, null);
  1758. }
  1759. if ($from_appid != $appid)
  1760. return array(ErrorCode::$ValidateAppidError, null);
  1761. return array(0, $xml_content);
  1762. }
  1763. /**
  1764. * 随机生成16位字符串
  1765. * @return string 生成的字符串
  1766. */
  1767. function getRandomStr()
  1768. {
  1769. $str = "";
  1770. $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  1771. $max = strlen($str_pol) - 1;
  1772. for ($i = 0; $i < 16; $i++) {
  1773. $str .= $str_pol[mt_rand(0, $max)];
  1774. }
  1775. return $str;
  1776. }
  1777. }
  1778. /**
  1779. * error code
  1780. * 仅用作类内部使用,不用于官方API接口的errCode码
  1781. */
  1782. class ErrorCode
  1783. {
  1784. public static $OK = 0;
  1785. public static $ValidateSignatureError = 40001;
  1786. public static $ParseXmlError = 40002;
  1787. public static $ComputeSignatureError = 40003;
  1788. public static $IllegalAesKey = 40004;
  1789. public static $ValidateAppidError = 40005;
  1790. public static $EncryptAESError = 40006;
  1791. public static $DecryptAESError = 40007;
  1792. public static $IllegalBuffer = 40008;
  1793. public static $EncodeBase64Error = 40009;
  1794. public static $DecodeBase64Error = 40010;
  1795. public static $GenReturnXmlError = 40011;
  1796. public static $errCode=array(
  1797. '0'=>'无问题',
  1798. '40001'=>'签名验证错误',
  1799. '40002'=>'xml解析失败',
  1800. '40003'=>'sha加密生成签名失败',
  1801. '40004'=>'encodingAesKey 非法',
  1802. '40005'=>'appid 校验错误',
  1803. '40006'=>'aes 加密失败',
  1804. '40007'=>'aes 解密失败',
  1805. '40008'=>'解密后得到的buffer非法',
  1806. '40009'=>'base64加密失败',
  1807. '40010'=>'base64解密失败',
  1808. '40011'=>'生成xml失败',
  1809. );
  1810. public static function getErrText($err) {
  1811. if (isset(self::$errCode[$err])) {
  1812. return self::$errCode[$err];
  1813. }else {
  1814. return false;
  1815. };
  1816. }
  1817. }