qywechat.class.php 58 KB

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