Wechat.class.php 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427
  1. <?php
  2. /**
  3. * 微信公众平台PHP-SDK
  4. * @author dodgepudding@gmail.com
  5. * @link https://github.com/dodgepudding/wechat-php-sdk
  6. * @version 1.2
  7. * usage:
  8. * $options = array(
  9. * 'token'=>'tokenaccesskey', //填写你设定的key
  10. * 'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey
  11. * 'appid'=>'wxdk1234567890', //填写高级调用功能的app id
  12. * 'appsecret'=>'xxxxxxxxxxxxxxxxxxx', //填写高级调用功能的密钥
  13. * 'partnerid'=>'88888888', //财付通商户身份标识
  14. * 'partnerkey'=>'', //财付通商户权限密钥Key
  15. * 'paysignkey'=>'' //商户签名密钥Key
  16. * );
  17. * $weObj = new Wechat($options);
  18. * $weObj->valid();
  19. * $type = $weObj->getRev()->getRevType();
  20. * switch($type) {
  21. * case Wechat::MSGTYPE_TEXT:
  22. * $weObj->text("hello, I'm wechat")->reply();
  23. * exit;
  24. * break;
  25. * case Wechat::MSGTYPE_EVENT:
  26. * ....
  27. * break;
  28. * case Wechat::MSGTYPE_IMAGE:
  29. * ...
  30. * break;
  31. * default:
  32. * $weObj->text("help info")->reply();
  33. * }
  34. *
  35. * //获取菜单操作:
  36. * $menu = $weObj->getMenu();
  37. * //设置菜单
  38. * $newmenu = array(
  39. * "button"=>
  40. * array(
  41. * array('type'=>'click','name'=>'最新消息','key'=>'MENU_KEY_NEWS'),
  42. * array('type'=>'view','name'=>'我要搜索','url'=>'http://www.baidu.com'),
  43. * )
  44. * );
  45. * $result = $weObj->createMenu($newmenu);
  46. */
  47. class Wechat
  48. {
  49. const MSGTYPE_TEXT = 'text';
  50. const MSGTYPE_IMAGE = 'image';
  51. const MSGTYPE_LOCATION = 'location';
  52. const MSGTYPE_LINK = 'link';
  53. const MSGTYPE_EVENT = 'event';
  54. const MSGTYPE_MUSIC = 'music';
  55. const MSGTYPE_NEWS = 'news';
  56. const MSGTYPE_VOICE = 'voice';
  57. const MSGTYPE_VIDEO = 'video';
  58. const EVENT_SUBSCRIBE = 'subscribe'; //订阅
  59. const EVENT_UNSUBSCRIBE = 'unsubscribe'; //取消订阅
  60. const EVENT_SCAN = 'SCAN'; //扫描带参数二维码
  61. const EVENT_LOCATION = 'LOCATION'; //上报地理位置
  62. const EVENT_MENU_VIEW = 'VIEW'; //菜单 - 点击菜单跳转链接
  63. const EVENT_MENU_CLICK = 'CLICK'; //菜单 - 点击菜单拉取消息
  64. const EVENT_MENU_SCAN_PUSH = 'scancode_push'; //菜单 - 扫码推事件(客户端跳URL)
  65. const EVENT_MENU_SCAN_WAITMSG = 'scancode_waitmsg'; //菜单 - 扫码推事件(客户端不跳URL)
  66. const EVENT_MENU_PIC_SYS = 'pic_sysphoto'; //菜单 - 弹出系统拍照发图
  67. const EVENT_MENU_PIC_PHOTO = 'pic_photo_or_album'; //菜单 - 弹出拍照或者相册发图
  68. const EVENT_MENU_PIC_WEIXIN = 'pic_weixin'; //菜单 - 弹出微信相册发图器
  69. const EVENT_MENU_LOCATION = 'location_select'; //菜单 - 弹出地理位置选择器
  70. const EVENT_SEND_MASS = 'MASSSENDJOBFINISH'; //发送结果 - 高级群发完成
  71. const EVENT_SEND_TEMPLATE = 'TEMPLATESENDJOBFINISH';//发送结果 - 模板消息发送结果
  72. const EVENT_KF_SEESION_CREATE = 'kfcreatesession'; //多客服 - 接入会话
  73. const EVENT_KF_SEESION_CLOSE = 'kfclosesession'; //多客服 - 关闭会话
  74. const EVENT_KF_SEESION_SWITCH = 'kfswitchsession'; //多客服 - 转接会话
  75. const EVENT_CARD_PASS = 'card_pass_check'; //卡券 - 审核通过
  76. const EVENT_CARD_NOTPASS = 'card_not_pass_check'; //卡券 - 审核未通过
  77. const EVENT_CARD_USER_GET = 'user_get_card'; //卡券 - 用户领取卡券
  78. const EVENT_CARD_USER_DEL = 'user_del_card'; //卡券 - 用户删除卡券
  79. const API_URL_PREFIX = 'https://api.weixin.qq.com/cgi-bin';
  80. const AUTH_URL = '/token?grant_type=client_credential&';
  81. const MENU_CREATE_URL = '/menu/create?';
  82. const MENU_GET_URL = '/menu/get?';
  83. const MENU_DELETE_URL = '/menu/delete?';
  84. const GET_TICKET_URL = '/ticket/getticket?';
  85. const CALLBACKSERVER_GET_URL = '/getcallbackip?';
  86. const QRCODE_CREATE_URL='/qrcode/create?';
  87. const QR_SCENE = 0;
  88. const QR_LIMIT_SCENE = 1;
  89. const QRCODE_IMG_URL='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=';
  90. const SHORT_URL='/shorturl?';
  91. const USER_GET_URL='/user/get?';
  92. const USER_INFO_URL='/user/info?';
  93. const USER_UPDATEREMARK_URL='/user/info/updateremark?';
  94. const GROUP_GET_URL='/groups/get?';
  95. const USER_GROUP_URL='/groups/getid?';
  96. const GROUP_CREATE_URL='/groups/create?';
  97. const GROUP_UPDATE_URL='/groups/update?';
  98. const GROUP_MEMBER_UPDATE_URL='/groups/members/update?';
  99. const CUSTOM_SEND_URL='/message/custom/send?';
  100. const MEDIA_UPLOADNEWS_URL = '/media/uploadnews?';
  101. const MASS_SEND_URL = '/message/mass/send?';
  102. const TEMPLATE_SET_INDUSTRY_URL = '/message/template/api_set_industry?';
  103. const TEMPLATE_ADD_TPL_URL = '/message/template/api_add_template?';
  104. const TEMPLATE_SEND_URL = '/message/template/send?';
  105. const MASS_SEND_GROUP_URL = '/message/mass/sendall?';
  106. const MASS_DELETE_URL = '/message/mass/delete?';
  107. const MASS_PREVIEW_URL = '/message/mass/preview?';
  108. const MASS_QUERY_URL = '/message/mass/get?';
  109. const UPLOAD_MEDIA_URL = 'http://file.api.weixin.qq.com/cgi-bin';
  110. const MEDIA_UPLOAD = '/media/upload?';
  111. const MEDIA_GET_URL = '/media/get?';
  112. const MEDIA_VIDEO_UPLOAD = '/media/uploadvideo?';
  113. const OAUTH_PREFIX = 'https://open.weixin.qq.com/connect/oauth2';
  114. const OAUTH_AUTHORIZE_URL = '/authorize?';
  115. const API_BASE_URL_PREFIX = 'https://api.weixin.qq.com'; //以下API接口URL需要使用此前缀
  116. const OAUTH_TOKEN_URL = '/sns/oauth2/access_token?';
  117. const OAUTH_REFRESH_URL = '/sns/oauth2/refresh_token?';
  118. const OAUTH_USERINFO_URL = '/sns/oauth2/sns/userinfo?';
  119. const OAUTH_AUTH_URL = '/sns/auth?';
  120. const PAY_DELIVERNOTIFY = '/pay/delivernotify?';
  121. const PAY_ORDERQUERY = '/pay/orderquery?';
  122. ///多客服相关地址
  123. const CUSTOM_SERVICE_GET_RECORD = '/customservice/getrecord?';
  124. const CUSTOM_SERVICE_GET_KFLIST = '/customservice/getkflist?';
  125. const CUSTOM_SERVICE_GET_ONLINEKFLIST = '/customservice/getonlinekflist?';
  126. const CUSTOM_SEESSION_CREATE = '/customservice/kfsession/create?';
  127. const CUSTOM_SEESSION_CLOSE = '/customservice/kfsession/close?';
  128. const CUSTOM_SEESSION_SWITCH = '/customservice/kfsession/switch?';
  129. const CUSTOM_SEESSION_GET = '/customservice/kfsession/getsession?';
  130. const CUSTOM_SEESSION_GET_LIST = '/customservice/kfsession/getsessionlist?';
  131. const CUSTOM_SEESSION_GET_WAIT = '/customservice/kfsession/getwaitcase?';
  132. const CS_KF_ACCOUNT_ADD_URL = '/customservice/kfaccount/add?';
  133. const CS_KF_ACCOUNT_UPDATE_URL = '/customservice/kfaccount/update?';
  134. const CS_KF_ACCOUNT_DEL_URL = '/customservice/kfaccount/del?';
  135. const CS_KF_ACCOUNT_UPLOAD_HEADIMG_URL = '/customservice/kfaccount/uploadheadimg?';
  136. ///卡券相关地址
  137. const CARD_CREATE = '/card/create?';
  138. const CARD_DELETE = '/card/delete?';
  139. const CARD_UPDATE = '/card/update?';
  140. const CARD_GET = '/card/get?';
  141. const CARD_BATCHGET = '/card/batchget?';
  142. const CARD_MODIFY_STOCK = '/card/modifystock?';
  143. const CARD_LOCATION_BATCHADD = '/card/location/batchadd?';
  144. const CARD_LOCATION_BATCHGET = '/card/location/batchget?';
  145. const CARD_GETCOLORS = '/card/getcolors?';
  146. const CARD_QRCODE_CREATE = '/card/qrcode/create?';
  147. const CARD_CODE_CONSUME = '/card/code/consume?';
  148. const CARD_CODE_DECRYPT = '/card/code/decrypt?';
  149. const CARD_CODE_GET = '/card/code/get?';
  150. const CARD_CODE_UPDATE = '/card/code/update?';
  151. const CARD_CODE_UNAVAILABLE = '/card/code/unavailable?';
  152. const CARD_TESTWHILELIST_SET = '/card/testwhitelist/set?';
  153. const CARD_MEMBERCARD_ACTIVATE = '/card/membercard/activate?'; //激活会员卡
  154. const CARD_MEMBERCARD_UPDATEUSER = '/card/membercard/updateuser?'; //更新会员卡
  155. const CARD_MOVIETICKET_UPDATEUSER = '/card/movieticket/updateuser?'; //更新电影票(未加方法)
  156. const CARD_BOARDINGPASS_CHECKIN = '/card/boardingpass/checkin?'; //飞机票-在线选座(未加方法)
  157. const CARD_LUCKYMONEY_UPDATE = '/card/luckymoney/updateuserbalance?'; //更新红包金额
  158. const SEMANTIC_API_URL= '/semantic/semproxy/search?';
  159. private $token;
  160. private $encodingAesKey;
  161. private $encrypt_type;
  162. private $appid;
  163. private $appsecret;
  164. private $access_token;
  165. private $jsapi_ticket;
  166. private $user_token;
  167. private $partnerid;
  168. private $partnerkey;
  169. private $paysignkey;
  170. private $postxml;
  171. private $_msg;
  172. private $_funcflag = false;
  173. private $_receive;
  174. private $_text_filter = true;
  175. public $debug = false;
  176. public $errCode = 40001;
  177. public $errMsg = "no access";
  178. private $_logcallback;
  179. public function __construct($options)
  180. {
  181. $this->token = isset($options['token'])?$options['token']:'';
  182. $this->encodingAesKey = isset($options['encodingaeskey'])?$options['encodingaeskey']:'';
  183. $this->appid = isset($options['appid'])?$options['appid']:'';
  184. $this->appsecret = isset($options['appsecret'])?$options['appsecret']:'';
  185. $this->partnerid = isset($options['partnerid'])?$options['partnerid']:'';
  186. $this->partnerkey = isset($options['partnerkey'])?$options['partnerkey']:'';
  187. $this->paysignkey = isset($options['paysignkey'])?$options['paysignkey']:'';
  188. $this->debug = isset($options['debug'])?$options['debug']:false;
  189. $this->_logcallback = isset($options['logcallback'])?$options['logcallback']:false;
  190. }
  191. /**
  192. * For weixin server validation
  193. */
  194. private function checkSignature($str='')
  195. {
  196. $signature = isset($_GET["signature"])?$_GET["signature"]:'';
  197. $signature = isset($_GET["msg_signature"])?$_GET["msg_signature"]:$signature; //如果存在加密验证则用加密验证段
  198. $timestamp = isset($_GET["timestamp"])?$_GET["timestamp"]:'';
  199. $nonce = isset($_GET["nonce"])?$_GET["nonce"]:'';
  200. $token = $this->token;
  201. $tmpArr = array($token, $timestamp, $nonce,$str);
  202. sort($tmpArr, SORT_STRING);
  203. $tmpStr = implode( $tmpArr );
  204. $tmpStr = sha1( $tmpStr );
  205. if( $tmpStr == $signature ){
  206. return true;
  207. }else{
  208. return false;
  209. }
  210. }
  211. /**
  212. * For weixin server validation
  213. * @param bool $return 是否返回
  214. */
  215. public function valid($return=false)
  216. {
  217. $encryptStr="";
  218. if ($_SERVER['REQUEST_METHOD'] == "POST") {
  219. $postStr = file_get_contents("php://input");
  220. $array = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  221. $this->encrypt_type = isset($_GET["encrypt_type"]) ? $_GET["encrypt_type"]: '';
  222. if ($this->encrypt_type == 'aes') { //aes加密
  223. $this->log($postStr);
  224. $encryptStr = $array['Encrypt'];
  225. $pc = new Prpcrypt($this->encodingAesKey);
  226. $array = $pc->decrypt($encryptStr,$this->appid);
  227. if (!isset($array[0]) || ($array[0] != 0)) {
  228. if (!$return) {
  229. die('decrypt error!');
  230. } else {
  231. return false;
  232. }
  233. }
  234. $this->postxml = $array[1];
  235. if (!$this->appid)
  236. $this->appid = $array[2];//为了没有appid的订阅号。
  237. } else {
  238. $this->postxml = $postStr;
  239. }
  240. } elseif (isset($_GET["echostr"])) {
  241. $echoStr = $_GET["echostr"];
  242. if ($return) {
  243. if ($this->checkSignature())
  244. return $echoStr;
  245. else
  246. return false;
  247. } else {
  248. if ($this->checkSignature())
  249. die($echoStr);
  250. else
  251. die('no access');
  252. }
  253. }
  254. if (!$this->checkSignature($encryptStr)) {
  255. if ($return)
  256. return false;
  257. else
  258. die('no access');
  259. }
  260. return true;
  261. }
  262. /**
  263. * 设置发送消息
  264. * @param array $msg 消息数组
  265. * @param bool $append 是否在原消息数组追加
  266. */
  267. public function Message($msg = '',$append = false){
  268. if (is_null($msg)) {
  269. $this->_msg =array();
  270. }elseif (is_array($msg)) {
  271. if ($append)
  272. $this->_msg = array_merge($this->_msg,$msg);
  273. else
  274. $this->_msg = $msg;
  275. return $this->_msg;
  276. } else {
  277. return $this->_msg;
  278. }
  279. }
  280. /**
  281. * 设置消息的星标标志,官方已取消对此功能的支持
  282. */
  283. public function setFuncFlag($flag) {
  284. $this->_funcflag = $flag;
  285. return $this;
  286. }
  287. private function log($log){
  288. if ($this->debug ) {
  289. if (function_exists($this->_logcallback)) {
  290. if (is_array($log)) $log = print_r($log,true);
  291. return call_user_func($this->_logcallback,$log);
  292. }elseif (class_exists('Log')) {
  293. Log::write('wechat:'.$log, Log::DEBUG);
  294. }
  295. }
  296. return false;
  297. }
  298. /**
  299. * 获取微信服务器发来的信息
  300. */
  301. public function getRev()
  302. {
  303. if ($this->_receive) return $this;
  304. $postStr = !empty($this->postxml)?$this->postxml:file_get_contents("php://input");
  305. //兼顾使用明文又不想调用valid()方法的情况
  306. $this->log($postStr);
  307. if (!empty($postStr)) {
  308. $this->_receive = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  309. }
  310. return $this;
  311. }
  312. /**
  313. * 获取微信服务器发来的信息
  314. */
  315. public function getRevData()
  316. {
  317. return $this->_receive;
  318. }
  319. /**
  320. * 获取消息发送者
  321. */
  322. public function getRevFrom() {
  323. if (isset($this->_receive['FromUserName']))
  324. return $this->_receive['FromUserName'];
  325. else
  326. return false;
  327. }
  328. /**
  329. * 获取消息接受者
  330. */
  331. public function getRevTo() {
  332. if (isset($this->_receive['ToUserName']))
  333. return $this->_receive['ToUserName'];
  334. else
  335. return false;
  336. }
  337. /**
  338. * 获取接收消息的类型
  339. */
  340. public function getRevType() {
  341. if (isset($this->_receive['MsgType']))
  342. return $this->_receive['MsgType'];
  343. else
  344. return false;
  345. }
  346. /**
  347. * 获取消息ID
  348. */
  349. public function getRevID() {
  350. if (isset($this->_receive['MsgId']))
  351. return $this->_receive['MsgId'];
  352. else
  353. return false;
  354. }
  355. /**
  356. * 获取消息发送时间
  357. */
  358. public function getRevCtime() {
  359. if (isset($this->_receive['CreateTime']))
  360. return $this->_receive['CreateTime'];
  361. else
  362. return false;
  363. }
  364. /**
  365. * 获取接收消息内容正文
  366. */
  367. public function getRevContent(){
  368. if (isset($this->_receive['Content']))
  369. return $this->_receive['Content'];
  370. else if (isset($this->_receive['Recognition'])) //获取语音识别文字内容,需申请开通
  371. return $this->_receive['Recognition'];
  372. else
  373. return false;
  374. }
  375. /**
  376. * 获取接收消息图片
  377. */
  378. public function getRevPic(){
  379. if (isset($this->_receive['PicUrl']))
  380. return array(
  381. 'mediaid'=>$this->_receive['MediaId'],
  382. 'picurl'=>(string)$this->_receive['PicUrl'], //防止picurl为空导致解析出错
  383. );
  384. else
  385. return false;
  386. }
  387. /**
  388. * 获取接收消息链接
  389. */
  390. public function getRevLink(){
  391. if (isset($this->_receive['Url'])){
  392. return array(
  393. 'url'=>$this->_receive['Url'],
  394. 'title'=>$this->_receive['Title'],
  395. 'description'=>$this->_receive['Description']
  396. );
  397. } else
  398. return false;
  399. }
  400. /**
  401. * 获取接收地理位置
  402. */
  403. public function getRevGeo(){
  404. if (isset($this->_receive['Location_X'])){
  405. return array(
  406. 'x'=>$this->_receive['Location_X'],
  407. 'y'=>$this->_receive['Location_Y'],
  408. 'scale'=>$this->_receive['Scale'],
  409. 'label'=>$this->_receive['Label']
  410. );
  411. } else
  412. return false;
  413. }
  414. /**
  415. * 获取上报地理位置事件
  416. */
  417. public function getRevEventGeo(){
  418. if (isset($this->_receive['Latitude'])){
  419. return array(
  420. 'x'=>$this->_receive['Latitude'],
  421. 'y'=>$this->_receive['Longitude'],
  422. 'precision'=>$this->_receive['Precision'],
  423. );
  424. } else
  425. return false;
  426. }
  427. /**
  428. * 获取接收事件推送
  429. */
  430. public function getRevEvent(){
  431. if (isset($this->_receive['Event'])){
  432. $array['event'] = $this->_receive['Event'];
  433. }
  434. if (isset($this->_receive['EventKey'])){
  435. $array['key'] = $this->_receive['EventKey'];
  436. }
  437. if (isset($array) && count($array) > 0) {
  438. return $array;
  439. } else {
  440. return false;
  441. }
  442. }
  443. /**
  444. * 获取自定义菜单的扫码推事件信息
  445. *
  446. * 事件类型为以下两种时则调用此方法有效
  447. * Event 事件类型,scancode_push
  448. * Event 事件类型,scancode_waitmsg
  449. *
  450. * @return: array | false
  451. * array (
  452. * 'ScanType'=>'qrcode',
  453. * 'ScanResult'=>'123123'
  454. * )
  455. */
  456. public function getRevScanInfo(){
  457. if (isset($this->_receive['ScanCodeInfo'])){
  458. if (!is_array($this->_receive['SendPicsInfo'])) {
  459. $array=(array)$this->_receive['ScanCodeInfo'];
  460. $this->_receive['ScanCodeInfo']=$array;
  461. }else {
  462. $array=$this->_receive['ScanCodeInfo'];
  463. }
  464. }
  465. if (isset($array) && count($array) > 0) {
  466. return $array;
  467. } else {
  468. return false;
  469. }
  470. }
  471. /**
  472. * 获取自定义菜单的图片发送事件信息
  473. *
  474. * 事件类型为以下三种时则调用此方法有效
  475. * Event 事件类型,pic_sysphoto 弹出系统拍照发图的事件推送
  476. * Event 事件类型,pic_photo_or_album 弹出拍照或者相册发图的事件推送
  477. * Event 事件类型,pic_weixin 弹出微信相册发图器的事件推送
  478. *
  479. * @return: array | false
  480. * array (
  481. * 'Count' => '2',
  482. * 'PicList' =>array (
  483. * 'item' =>array (
  484. * 0 =>array ('PicMd5Sum' => 'aaae42617cf2a14342d96005af53624c'),
  485. * 1 =>array ('PicMd5Sum' => '149bd39e296860a2adc2f1bb81616ff8'),
  486. * ),
  487. * ),
  488. * )
  489. *
  490. */
  491. public function getRevSendPicsInfo(){
  492. if (isset($this->_receive['SendPicsInfo'])){
  493. if (!is_array($this->_receive['SendPicsInfo'])) {
  494. $array=(array)$this->_receive['SendPicsInfo'];
  495. if (isset($array['PicList'])){
  496. $array['PicList']=(array)$array['PicList'];
  497. $item=$array['PicList']['item'];
  498. $array['PicList']['item']=array();
  499. foreach ( $item as $key => $value ){
  500. $array['PicList']['item'][$key]=(array)$value;
  501. }
  502. }
  503. $this->_receive['SendPicsInfo']=$array;
  504. } else {
  505. $array=$this->_receive['SendPicsInfo'];
  506. }
  507. }
  508. if (isset($array) && count($array) > 0) {
  509. return $array;
  510. } else {
  511. return false;
  512. }
  513. }
  514. /**
  515. * 获取自定义菜单的地理位置选择器事件推送
  516. *
  517. * 事件类型为以下时则可以调用此方法有效
  518. * Event 事件类型,location_select 弹出地理位置选择器的事件推送
  519. *
  520. * @return: array | false
  521. * array (
  522. * 'Location_X' => '33.731655000061',
  523. * 'Location_Y' => '113.29955200008047',
  524. * 'Scale' => '16',
  525. * 'Label' => '某某市某某区某某路',
  526. * 'Poiname' => '',
  527. * )
  528. *
  529. */
  530. public function getRevSendGeoInfo(){
  531. if (isset($this->_receive['SendLocationInfo'])){
  532. if (!is_array($this->_receive['SendLocationInfo'])) {
  533. $array=(array)$this->_receive['SendLocationInfo'];
  534. if (empty($array['Poiname'])) {
  535. $array['Poiname']="";
  536. }
  537. if (empty($array['Label'])) {
  538. $array['Label']="";
  539. }
  540. $this->_receive['SendLocationInfo']=$array;
  541. } else {
  542. $array=$this->_receive['SendLocationInfo'];
  543. }
  544. }
  545. if (isset($array) && count($array) > 0) {
  546. return $array;
  547. } else {
  548. return false;
  549. }
  550. }
  551. /**
  552. * 获取接收语音推送
  553. */
  554. public function getRevVoice(){
  555. if (isset($this->_receive['MediaId'])){
  556. return array(
  557. 'mediaid'=>$this->_receive['MediaId'],
  558. 'format'=>$this->_receive['Format'],
  559. );
  560. } else
  561. return false;
  562. }
  563. /**
  564. * 获取接收视频推送
  565. */
  566. public function getRevVideo(){
  567. if (isset($this->_receive['MediaId'])){
  568. return array(
  569. 'mediaid'=>$this->_receive['MediaId'],
  570. 'thumbmediaid'=>$this->_receive['ThumbMediaId']
  571. );
  572. } else
  573. return false;
  574. }
  575. /**
  576. * 获取接收TICKET
  577. */
  578. public function getRevTicket(){
  579. if (isset($this->_receive['Ticket'])){
  580. return $this->_receive['Ticket'];
  581. } else
  582. return false;
  583. }
  584. /**
  585. * 获取二维码的场景值
  586. */
  587. public function getRevSceneId (){
  588. if (isset($this->_receive['EventKey'])){
  589. return str_replace('qrscene_','',$this->_receive['EventKey']);
  590. } else{
  591. return false;
  592. }
  593. }
  594. /**
  595. * 获取主动推送的消息ID
  596. * 经过验证,这个和普通的消息MsgId不一样
  597. * 当Event为 MASSSENDJOBFINISH 或 TEMPLATESENDJOBFINISH
  598. */
  599. public function getRevTplMsgID(){
  600. if (isset($this->_receive['MsgID'])){
  601. return $this->_receive['MsgID'];
  602. } else
  603. return false;
  604. }
  605. /**
  606. * 获取模板消息发送状态
  607. */
  608. public function getRevStatus(){
  609. if (isset($this->_receive['Status'])){
  610. return $this->_receive['Status'];
  611. } else
  612. return false;
  613. }
  614. /**
  615. * 获取群发或模板消息发送结果
  616. * 当Event为 MASSSENDJOBFINISH 或 TEMPLATESENDJOBFINISH,即高级群发/模板消息
  617. */
  618. public function getRevResult(){
  619. if (isset($this->_receive['Status'])) //发送是否成功,具体的返回值请参考 高级群发/模板消息 的事件推送说明
  620. $array['Status'] = $this->_receive['Status'];
  621. if (isset($this->_receive['MsgID'])) //发送的消息id
  622. $array['MsgID'] = $this->_receive['MsgID'];
  623. //以下仅当群发消息时才会有的事件内容
  624. if (isset($this->_receive['TotalCount'])) //分组或openid列表内粉丝数量
  625. $array['TotalCount'] = $this->_receive['TotalCount'];
  626. if (isset($this->_receive['FilterCount'])) //过滤(过滤是指特定地区、性别的过滤、用户设置拒收的过滤,用户接收已超4条的过滤)后,准备发送的粉丝数
  627. $array['FilterCount'] = $this->_receive['FilterCount'];
  628. if (isset($this->_receive['SentCount'])) //发送成功的粉丝数
  629. $array['SentCount'] = $this->_receive['SentCount'];
  630. if (isset($this->_receive['ErrorCount'])) //发送失败的粉丝数
  631. $array['ErrorCount'] = $this->_receive['ErrorCount'];
  632. if (isset($array) && count($array) > 0) {
  633. return $array;
  634. } else {
  635. return false;
  636. }
  637. }
  638. /**
  639. * 获取多客服会话状态推送事件 - 接入会话
  640. * 当Event为 kfcreatesession 即接入会话
  641. * @return string | boolean 返回分配到的客服
  642. */
  643. public function getRevKFCreate(){
  644. if (isset($this->_receive['KfAccount'])){
  645. return $this->_receive['KfAccount'];
  646. } else
  647. return false;
  648. }
  649. /**
  650. * 获取多客服会话状态推送事件 - 关闭会话
  651. * 当Event为 kfclosesession 即关闭会话
  652. * @return string | boolean 返回分配到的客服
  653. */
  654. public function getRevKFClose(){
  655. if (isset($this->_receive['KfAccount'])){
  656. return $this->_receive['KfAccount'];
  657. } else
  658. return false;
  659. }
  660. /**
  661. * 获取多客服会话状态推送事件 - 转接会话
  662. * 当Event为 kfswitchsession 即转接会话
  663. * @return array | boolean 返回分配到的客服
  664. * {
  665. * 'FromKfAccount' => '', //原接入客服
  666. * 'ToKfAccount' => '' //转接到客服
  667. * }
  668. */
  669. public function getRevKFSwitch(){
  670. if (isset($this->_receive['FromKfAccount'])) //原接入客服
  671. $array['FromKfAccount'] = $this->_receive['FromKfAccount'];
  672. if (isset($this->_receive['ToKfAccount'])) //转接到客服
  673. $array['ToKfAccount'] = $this->_receive['ToKfAccount'];
  674. if (isset($array) && count($array) > 0) {
  675. return $array;
  676. } else {
  677. return false;
  678. }
  679. }
  680. /**
  681. * 获取卡券事件推送 - 卡卷审核是否通过
  682. * 当Event为 card_pass_check(审核通过) 或 card_not_pass_check(未通过)
  683. * @return string|boolean 返回卡券ID
  684. */
  685. public function getRevCardPass(){
  686. if (isset($this->_receive['CardId']))
  687. return $this->_receive['CardId'];
  688. else
  689. return false;
  690. }
  691. /**
  692. * 获取卡券事件推送 - 领取卡券
  693. * 当Event为 user_get_card(用户领取卡券)
  694. * @return array|boolean
  695. */
  696. public function getRevCardGet(){
  697. if (isset($this->_receive['CardId'])) //卡券 ID
  698. $array['CardId'] = $this->_receive['CardId'];
  699. if (isset($this->_receive['IsGiveByFriend'])) //是否为转赠,1 代表是,0 代表否。
  700. $array['IsGiveByFriend'] = $this->_receive['IsGiveByFriend'];
  701. if (isset($this->_receive['UserCardCode']) && !empty($this->_receive['UserCardCode'])) //code 序列号。自定义 code 及非自定义 code的卡券被领取后都支持事件推送。
  702. $array['UserCardCode'] = $this->_receive['UserCardCode'];
  703. if (isset($array) && count($array) > 0) {
  704. return $array;
  705. } else {
  706. return false;
  707. }
  708. }
  709. /**
  710. * 获取卡券事件推送 - 删除卡券
  711. * 当Event为 user_del_card(用户删除卡券)
  712. * @return array|boolean
  713. */
  714. public function getRevCardDel(){
  715. if (isset($this->_receive['CardId'])) //卡券 ID
  716. $array['CardId'] = $this->_receive['CardId'];
  717. if (isset($this->_receive['UserCardCode']) && !empty($this->_receive['UserCardCode'])) //code 序列号。自定义 code 及非自定义 code的卡券被领取后都支持事件推送。
  718. $array['UserCardCode'] = $this->_receive['UserCardCode'];
  719. if (isset($array) && count($array) > 0) {
  720. return $array;
  721. } else {
  722. return false;
  723. }
  724. }
  725. public static function xmlSafeStr($str)
  726. {
  727. return '<![CDATA['.preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/",'',$str).']]>';
  728. }
  729. /**
  730. * 数据XML编码
  731. * @param mixed $data 数据
  732. * @return string
  733. */
  734. public static function data_to_xml($data) {
  735. $xml = '';
  736. foreach ($data as $key => $val) {
  737. is_numeric($key) && $key = "item id=\"$key\"";
  738. $xml .= "<$key>";
  739. $xml .= ( is_array($val) || is_object($val)) ? self::data_to_xml($val) : self::xmlSafeStr($val);
  740. list($key, ) = explode(' ', $key);
  741. $xml .= "</$key>";
  742. }
  743. return $xml;
  744. }
  745. /**
  746. * XML编码
  747. * @param mixed $data 数据
  748. * @param string $root 根节点名
  749. * @param string $item 数字索引的子节点名
  750. * @param string $attr 根节点属性
  751. * @param string $id 数字索引子节点key转换的属性名
  752. * @param string $encoding 数据编码
  753. * @return string
  754. */
  755. public function xml_encode($data, $root='xml', $item='item', $attr='', $id='id', $encoding='utf-8') {
  756. if(is_array($attr)){
  757. $_attr = array();
  758. foreach ($attr as $key => $value) {
  759. $_attr[] = "{$key}=\"{$value}\"";
  760. }
  761. $attr = implode(' ', $_attr);
  762. }
  763. $attr = trim($attr);
  764. $attr = empty($attr) ? '' : " {$attr}";
  765. $xml = "<{$root}{$attr}>";
  766. $xml .= self::data_to_xml($data, $item, $id);
  767. $xml .= "</{$root}>";
  768. return $xml;
  769. }
  770. /**
  771. * 过滤文字回复\r\n换行符
  772. * @param string $text
  773. * @return string|mixed
  774. */
  775. private function _auto_text_filter($text) {
  776. if (!$this->_text_filter) return $text;
  777. return str_replace("\r\n", "\n", $text);
  778. }
  779. /**
  780. * 设置回复消息
  781. * Example: $obj->text('hello')->reply();
  782. * @param string $text
  783. */
  784. public function text($text='')
  785. {
  786. $FuncFlag = $this->_funcflag ? 1 : 0;
  787. $msg = array(
  788. 'ToUserName' => $this->getRevFrom(),
  789. 'FromUserName'=>$this->getRevTo(),
  790. 'MsgType'=>self::MSGTYPE_TEXT,
  791. 'Content'=>$this->_auto_text_filter($text),
  792. 'CreateTime'=>time(),
  793. 'FuncFlag'=>$FuncFlag
  794. );
  795. $this->Message($msg);
  796. return $this;
  797. }
  798. /**
  799. * 设置回复消息
  800. * Example: $obj->image('media_id')->reply();
  801. * @param string $mediaid
  802. */
  803. public function image($mediaid='')
  804. {
  805. $FuncFlag = $this->_funcflag ? 1 : 0;
  806. $msg = array(
  807. 'ToUserName' => $this->getRevFrom(),
  808. 'FromUserName'=>$this->getRevTo(),
  809. 'MsgType'=>self::MSGTYPE_IMAGE,
  810. 'Image'=>array('MediaId'=>$mediaid),
  811. 'CreateTime'=>time(),
  812. 'FuncFlag'=>$FuncFlag
  813. );
  814. $this->Message($msg);
  815. return $this;
  816. }
  817. /**
  818. * 设置回复消息
  819. * Example: $obj->voice('media_id')->reply();
  820. * @param string $mediaid
  821. */
  822. public function voice($mediaid='')
  823. {
  824. $FuncFlag = $this->_funcflag ? 1 : 0;
  825. $msg = array(
  826. 'ToUserName' => $this->getRevFrom(),
  827. 'FromUserName'=>$this->getRevTo(),
  828. 'MsgType'=>self::MSGTYPE_VOICE,
  829. 'Voice'=>array('MediaId'=>$mediaid),
  830. 'CreateTime'=>time(),
  831. 'FuncFlag'=>$FuncFlag
  832. );
  833. $this->Message($msg);
  834. return $this;
  835. }
  836. /**
  837. * 设置回复消息
  838. * Example: $obj->video('media_id','title','description')->reply();
  839. * @param string $mediaid
  840. */
  841. public function video($mediaid='',$title='',$description='')
  842. {
  843. $FuncFlag = $this->_funcflag ? 1 : 0;
  844. $msg = array(
  845. 'ToUserName' => $this->getRevFrom(),
  846. 'FromUserName'=>$this->getRevTo(),
  847. 'MsgType'=>self::MSGTYPE_VIDEO,
  848. 'Video'=>array(
  849. 'MediaId'=>$mediaid,
  850. 'Title'=>$title,
  851. 'Description'=>$description
  852. ),
  853. 'CreateTime'=>time(),
  854. 'FuncFlag'=>$FuncFlag
  855. );
  856. $this->Message($msg);
  857. return $this;
  858. }
  859. /**
  860. * 设置回复音乐
  861. * @param string $title
  862. * @param string $desc
  863. * @param string $musicurl
  864. * @param string $hgmusicurl
  865. * @param string $thumbmediaid 音乐图片缩略图的媒体id,非必须
  866. */
  867. public function music($title,$desc,$musicurl,$hgmusicurl='',$thumbmediaid='') {
  868. $FuncFlag = $this->_funcflag ? 1 : 0;
  869. $msg = array(
  870. 'ToUserName' => $this->getRevFrom(),
  871. 'FromUserName'=>$this->getRevTo(),
  872. 'CreateTime'=>time(),
  873. 'MsgType'=>self::MSGTYPE_MUSIC,
  874. 'Music'=>array(
  875. 'Title'=>$title,
  876. 'Description'=>$desc,
  877. 'MusicUrl'=>$musicurl,
  878. 'HQMusicUrl'=>$hgmusicurl
  879. ),
  880. 'FuncFlag'=>$FuncFlag
  881. );
  882. if ($thumbmediaid) {
  883. $msg['Music']['ThumbMediaId'] = $thumbmediaid;
  884. }
  885. $this->Message($msg);
  886. return $this;
  887. }
  888. /**
  889. * 设置回复图文
  890. * @param array $newsData
  891. * 数组结构:
  892. * array(
  893. * "0"=>array(
  894. * 'Title'=>'msg title',
  895. * 'Description'=>'summary text',
  896. * 'PicUrl'=>'http://www.domain.com/1.jpg',
  897. * 'Url'=>'http://www.domain.com/1.html'
  898. * ),
  899. * "1"=>....
  900. * )
  901. */
  902. public function news($newsData=array())
  903. {
  904. $FuncFlag = $this->_funcflag ? 1 : 0;
  905. $count = count($newsData);
  906. $msg = array(
  907. 'ToUserName' => $this->getRevFrom(),
  908. 'FromUserName'=>$this->getRevTo(),
  909. 'MsgType'=>self::MSGTYPE_NEWS,
  910. 'CreateTime'=>time(),
  911. 'ArticleCount'=>$count,
  912. 'Articles'=>$newsData,
  913. 'FuncFlag'=>$FuncFlag
  914. );
  915. $this->Message($msg);
  916. return $this;
  917. }
  918. /**
  919. *
  920. * 回复微信服务器, 此函数支持链式操作
  921. * Example: $this->text('msg tips')->reply();
  922. * @param string $msg 要发送的信息, 默认取$this->_msg
  923. * @param bool $return 是否返回信息而不抛出到浏览器 默认:否
  924. */
  925. public function reply($msg=array(),$return = false)
  926. {
  927. if (empty($msg)) {
  928. if (empty($this->_msg)) //防止不先设置回复内容,直接调用reply方法导致异常
  929. return false;
  930. $msg = $this->_msg;
  931. }
  932. $xmldata= $this->xml_encode($msg);
  933. $this->log($xmldata);
  934. if ($this->encrypt_type == 'aes') { //如果来源消息为加密方式
  935. $pc = new Prpcrypt($this->encodingAesKey);
  936. $array = $pc->encrypt($xmldata, $this->appid);
  937. $ret = $array[0];
  938. if ($ret != 0) {
  939. $this->log('encrypt err!');
  940. return false;
  941. }
  942. $timestamp = time();
  943. $nonce = rand(77,999)*rand(605,888)*rand(11,99);
  944. $encrypt = $array[1];
  945. $tmpArr = array($this->token, $timestamp, $nonce,$encrypt);//比普通公众平台多了一个加密的密文
  946. sort($tmpArr, SORT_STRING);
  947. $signature = implode($tmpArr);
  948. $signature = sha1($signature);
  949. $xmldata = $this->generate($encrypt, $signature, $timestamp, $nonce);
  950. $this->log($xmldata);
  951. }
  952. if ($return)
  953. return $xmldata;
  954. else
  955. echo $xmldata;
  956. }
  957. /**
  958. * xml格式加密,仅请求为加密方式时再用
  959. */
  960. private function generate($encrypt, $signature, $timestamp, $nonce)
  961. {
  962. //格式化加密信息
  963. $format = "<xml>
  964. <Encrypt><![CDATA[%s]]></Encrypt>
  965. <MsgSignature><![CDATA[%s]]></MsgSignature>
  966. <TimeStamp>%s</TimeStamp>
  967. <Nonce><![CDATA[%s]]></Nonce>
  968. </xml>";
  969. return sprintf($format, $encrypt, $signature, $timestamp, $nonce);
  970. }
  971. /**
  972. * GET 请求
  973. * @param string $url
  974. */
  975. private function http_get($url){
  976. $oCurl = curl_init();
  977. if(stripos($url,"https://")!==FALSE){
  978. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  979. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
  980. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  981. }
  982. curl_setopt($oCurl, CURLOPT_URL, $url);
  983. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
  984. $sContent = curl_exec($oCurl);
  985. $aStatus = curl_getinfo($oCurl);
  986. curl_close($oCurl);
  987. if(intval($aStatus["http_code"])==200){
  988. return $sContent;
  989. }else{
  990. return false;
  991. }
  992. }
  993. /**
  994. * POST 请求
  995. * @param string $url
  996. * @param array $param
  997. * @param boolean $post_file 是否文件上传
  998. * @return string content
  999. */
  1000. private function http_post($url,$param,$post_file=false){
  1001. $oCurl = curl_init();
  1002. if(stripos($url,"https://")!==FALSE){
  1003. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  1004. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  1005. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  1006. }
  1007. if (is_string($param) || $post_file) {
  1008. $strPOST = $param;
  1009. } else {
  1010. $aPOST = array();
  1011. foreach($param as $key=>$val){
  1012. $aPOST[] = $key."=".urlencode($val);
  1013. }
  1014. $strPOST = join("&", $aPOST);
  1015. }
  1016. curl_setopt($oCurl, CURLOPT_URL, $url);
  1017. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
  1018. curl_setopt($oCurl, CURLOPT_POST,true);
  1019. curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
  1020. $sContent = curl_exec($oCurl);
  1021. $aStatus = curl_getinfo($oCurl);
  1022. curl_close($oCurl);
  1023. if(intval($aStatus["http_code"])==200){
  1024. return $sContent;
  1025. }else{
  1026. return false;
  1027. }
  1028. }
  1029. /**
  1030. * 通用auth验证方法,获取access_token
  1031. * @param string $appid
  1032. * @param string $appsecret
  1033. * @param string $token 手动指定access_token,非必要情况不建议用
  1034. */
  1035. public function checkAuth($appid='',$appsecret='',$token=''){
  1036. if (!$appid || !$appsecret) {
  1037. $appid = $this->appid;
  1038. $appsecret = $this->appsecret;
  1039. }
  1040. $authname = 'wechat_access_token'.$appid;
  1041. if ($token) { //手动指定token,优先使用
  1042. $this->access_token=$token;
  1043. return $this->access_token;
  1044. }
  1045. if ($rs = S($authname)) {
  1046. $this->access_token = $rs;
  1047. return $rs;
  1048. }
  1049. $result = $this->http_get(self::API_URL_PREFIX.self::AUTH_URL.'appid='.$appid.'&secret='.$appsecret);
  1050. if ($result)
  1051. {
  1052. $json = json_decode($result,true);
  1053. if (!$json || isset($json['errcode'])) {
  1054. $this->errCode = $json['errcode'];
  1055. $this->errMsg = $json['errmsg'];
  1056. return false;
  1057. }
  1058. $this->access_token = $json['access_token'];
  1059. $expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
  1060. S($authname,$this->access_token,$expire);
  1061. return $this->access_token;
  1062. }
  1063. return false;
  1064. }
  1065. /**
  1066. * 删除验证数据
  1067. * @param string $appid
  1068. */
  1069. public function resetAuth($appid=''){
  1070. if (!$appid) $appid = $this->appid;
  1071. $this->access_token = '';
  1072. $authname = 'wechat_access_token'.$appid;
  1073. S($authname,null);
  1074. return true;
  1075. }
  1076. /**
  1077. * 删除JSAPI授权TICKET
  1078. * @param string $appid 用于多个appid时使用
  1079. */
  1080. public function resetJsTicket($appid=''){
  1081. if (!$appid) $appid = $this->appid;
  1082. $this->jsapi_ticket = '';
  1083. $authname = 'wechat_jsapi_ticket'.$appid;
  1084. S($authname,null);
  1085. return true;
  1086. }
  1087. /**
  1088. * 获取JSAPI授权TICKET
  1089. * @param string $appid 用于多个appid时使用,可空
  1090. * @param string $jsapi_ticket 手动指定jsapi_ticket,非必要情况不建议用
  1091. */
  1092. public function getJsTicket($appid='',$jsapi_ticket=''){
  1093. if (!$this->access_token && !$this->checkAuth()) return false;
  1094. if ($jsapi_ticket) { //手动指定token,优先使用
  1095. $this->jsapi_ticket = $jsapi_ticket;
  1096. return $this->access_token;
  1097. }
  1098. $authname = 'wechat_jsapi_ticket'.$appid;
  1099. if ($rs = S($authname)) {
  1100. $this->jsapi_ticket = $rs;
  1101. return $rs;
  1102. }
  1103. $result = $this->http_get(self::API_URL_PREFIX.self::GET_TICKET_URL.'access_token='.$this->access_token.'&type=jsapi');
  1104. if ($result)
  1105. {
  1106. $json = json_decode($result,true);
  1107. if (!$json || !empty($json['errcode'])) {
  1108. $this->errCode = $json['errcode'];
  1109. $this->errMsg = $json['errmsg'];
  1110. return false;
  1111. }
  1112. $this->jsapi_ticket = $json['ticket'];
  1113. $expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
  1114. S($authname,$this->access_token,$expire);
  1115. return $this->jsapi_ticket;
  1116. }
  1117. return false;
  1118. }
  1119. /**
  1120. * 获取JsApi使用签名
  1121. * @param string $url 网页的URL,不包含#及其后面部分
  1122. * @param string $timeStamp 当前时间戳(需与JS输出的一致)
  1123. * @param string $nonceStr 随机串(需与JS输出的一致)
  1124. * @param string $appid 用于多个appid时使用,可空
  1125. * @return string 返回签名字串
  1126. */
  1127. public function getJsSign($url, $timeStamp, $nonceStr, $appid=''){
  1128. if (!$this->jsapi_ticket && !$this->getJsTicket($appid)) return false;
  1129. $ret = strpos($url,'#');
  1130. if ($ret)
  1131. $url = substr($url,0,$ret);
  1132. $url = trim($url);
  1133. if (empty($url))
  1134. return false;
  1135. $arrdata = array("timestamp" => $timeStamp, "noncestr" => $nonceStr, "url" => $url, "jsapi_ticket" => $this->jsapi_ticket);
  1136. return $this->getSignature($arrdata);
  1137. }
  1138. /**
  1139. * 微信api不支持中文转义的json结构
  1140. * @param array $arr
  1141. */
  1142. static function json_encode($arr) {
  1143. $parts = array ();
  1144. $is_list = false;
  1145. //Find out if the given array is a numerical array
  1146. $keys = array_keys ( $arr );
  1147. $max_length = count ( $arr ) - 1;
  1148. if (($keys [0] === 0) && ($keys [$max_length] === $max_length )) { //See if the first key is 0 and last key is length - 1
  1149. $is_list = true;
  1150. for($i = 0; $i < count ( $keys ); $i ++) { //See if each key correspondes to its position
  1151. if ($i != $keys [$i]) { //A key fails at position check.
  1152. $is_list = false; //It is an associative array.
  1153. break;
  1154. }
  1155. }
  1156. }
  1157. foreach ( $arr as $key => $value ) {
  1158. if (is_array ( $value )) { //Custom handling for arrays
  1159. if ($is_list)
  1160. $parts [] = self::json_encode ( $value ); /* :RECURSION: */
  1161. else
  1162. $parts [] = '"' . $key . '":' . self::json_encode ( $value ); /* :RECURSION: */
  1163. } else {
  1164. $str = '';
  1165. if (! $is_list)
  1166. $str = '"' . $key . '":';
  1167. //Custom handling for multiple data types
  1168. if (!is_string ( $value ) && is_numeric ( $value ) && $value<2000000000)
  1169. $str .= $value; //Numbers
  1170. elseif ($value === false)
  1171. $str .= 'false'; //The booleans
  1172. elseif ($value === true)
  1173. $str .= 'true';
  1174. else
  1175. $str .= '"' . addslashes ( $value ) . '"'; //All other things
  1176. // :TODO: Is there any more datatype we should be in the lookout for? (Object?)
  1177. $parts [] = $str;
  1178. }
  1179. }
  1180. $json = implode ( ',', $parts );
  1181. if ($is_list)
  1182. return '[' . $json . ']'; //Return numerical JSON
  1183. return '{' . $json . '}'; //Return associative JSON
  1184. }
  1185. /**
  1186. * 获取微信服务器IP地址列表
  1187. * @return array('127.0.0.1','127.0.0.1')
  1188. */
  1189. public function getServerIp(){
  1190. if (!$this->access_token && !$this->checkAuth()) return false;
  1191. $result = $this->http_get(self::API_URL_PREFIX.self::CALLBACKSERVER_GET_URL.'access_token='.$this->access_token);
  1192. if ($result)
  1193. {
  1194. $json = json_decode($result,true);
  1195. if (!$json || isset($json['errcode'])) {
  1196. $this->errCode = $json['errcode'];
  1197. $this->errMsg = $json['errmsg'];
  1198. return false;
  1199. }
  1200. return $json['ip_list'];
  1201. }
  1202. return false;
  1203. }
  1204. /**
  1205. * 创建菜单(认证后的订阅号可用)
  1206. * @param array $data 菜单数组数据
  1207. * example:
  1208. * array (
  1209. * 'button' => array (
  1210. * 0 => array (
  1211. * 'name' => '扫码',
  1212. * 'sub_button' => array (
  1213. * 0 => array (
  1214. * 'type' => 'scancode_waitmsg',
  1215. * 'name' => '扫码带提示',
  1216. * 'key' => 'rselfmenu_0_0',
  1217. * ),
  1218. * 1 => array (
  1219. * 'type' => 'scancode_push',
  1220. * 'name' => '扫码推事件',
  1221. * 'key' => 'rselfmenu_0_1',
  1222. * ),
  1223. * ),
  1224. * ),
  1225. * 1 => array (
  1226. * 'name' => '发图',
  1227. * 'sub_button' => array (
  1228. * 0 => array (
  1229. * 'type' => 'pic_sysphoto',
  1230. * 'name' => '系统拍照发图',
  1231. * 'key' => 'rselfmenu_1_0',
  1232. * ),
  1233. * 1 => array (
  1234. * 'type' => 'pic_photo_or_album',
  1235. * 'name' => '拍照或者相册发图',
  1236. * 'key' => 'rselfmenu_1_1',
  1237. * )
  1238. * ),
  1239. * ),
  1240. * 2 => array (
  1241. * 'type' => 'location_select',
  1242. * 'name' => '发送位置',
  1243. * 'key' => 'rselfmenu_2_0'
  1244. * ),
  1245. * ),
  1246. * )
  1247. * type可以选择为以下几种,其中5-8除了收到菜单事件以外,还会单独收到对应类型的信息。
  1248. * 1、click:点击推事件
  1249. * 2、view:跳转URL
  1250. * 3、scancode_push:扫码推事件
  1251. * 4、scancode_waitmsg:扫码推事件且弹出“消息接收中”提示框
  1252. * 5、pic_sysphoto:弹出系统拍照发图
  1253. * 6、pic_photo_or_album:弹出拍照或者相册发图
  1254. * 7、pic_weixin:弹出微信相册发图器
  1255. * 8、location_select:弹出地理位置选择器
  1256. */
  1257. public function createMenu($data){
  1258. if (!$this->access_token && !$this->checkAuth()) return false;
  1259. $result = $this->http_post(self::API_URL_PREFIX.self::MENU_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1260. if ($result)
  1261. {
  1262. $json = json_decode($result,true);
  1263. if (!$json || !empty($json['errcode'])) {
  1264. $this->errCode = $json['errcode'];
  1265. $this->errMsg = $json['errmsg'];
  1266. return false;
  1267. }
  1268. return true;
  1269. }
  1270. return false;
  1271. }
  1272. /**
  1273. * 获取菜单(认证后的订阅号可用)
  1274. * @return array('menu'=>array(....s))
  1275. */
  1276. public function getMenu(){
  1277. if (!$this->access_token && !$this->checkAuth()) return false;
  1278. $result = $this->http_get(self::API_URL_PREFIX.self::MENU_GET_URL.'access_token='.$this->access_token);
  1279. if ($result)
  1280. {
  1281. $json = json_decode($result,true);
  1282. if (!$json || isset($json['errcode'])) {
  1283. $this->errCode = $json['errcode'];
  1284. $this->errMsg = $json['errmsg'];
  1285. return false;
  1286. }
  1287. return $json;
  1288. }
  1289. return false;
  1290. }
  1291. /**
  1292. * 删除菜单(认证后的订阅号可用)
  1293. * @return boolean
  1294. */
  1295. public function deleteMenu(){
  1296. if (!$this->access_token && !$this->checkAuth()) return false;
  1297. $result = $this->http_get(self::API_URL_PREFIX.self::MENU_DELETE_URL.'access_token='.$this->access_token);
  1298. if ($result)
  1299. {
  1300. $json = json_decode($result,true);
  1301. if (!$json || !empty($json['errcode'])) {
  1302. $this->errCode = $json['errcode'];
  1303. $this->errMsg = $json['errmsg'];
  1304. return false;
  1305. }
  1306. return true;
  1307. }
  1308. return false;
  1309. }
  1310. /**
  1311. * 上传多媒体文件(认证后的订阅号可用)
  1312. * 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
  1313. * 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
  1314. * @param array $data {"media":'@Path\filename.jpg'}
  1315. * @param type 类型:图片:image 语音:voice 视频:video 缩略图:thumb
  1316. * @return boolean|array
  1317. */
  1318. public function uploadMedia($data, $type){
  1319. if (!$this->access_token && !$this->checkAuth()) return false;
  1320. $result = $this->http_post(self::UPLOAD_MEDIA_URL.self::MEDIA_UPLOAD.'access_token='.$this->access_token.'&type='.$type,$data,true);
  1321. if ($result)
  1322. {
  1323. $json = json_decode($result,true);
  1324. if (!$json || !empty($json['errcode'])) {
  1325. $this->errCode = $json['errcode'];
  1326. $this->errMsg = $json['errmsg'];
  1327. return false;
  1328. }
  1329. return $json;
  1330. }
  1331. return false;
  1332. }
  1333. /**
  1334. * 根据媒体文件ID获取媒体文件(认证后的订阅号可用)
  1335. * @param string $media_id 媒体文件id
  1336. * @return raw data
  1337. */
  1338. public function getMedia($media_id){
  1339. if (!$this->access_token && !$this->checkAuth()) return false;
  1340. $result = $this->http_get(self::UPLOAD_MEDIA_URL.self::MEDIA_GET_URL.'access_token='.$this->access_token.'&media_id='.$media_id);
  1341. if ($result)
  1342. {
  1343. $json = json_decode($result,true);
  1344. if (isset($json['errcode'])) {
  1345. $this->errCode = $json['errcode'];
  1346. $this->errMsg = $json['errmsg'];
  1347. return false;
  1348. }
  1349. return $result;
  1350. }
  1351. return false;
  1352. }
  1353. /**
  1354. * 上传图文消息素材(认证后的订阅号可用)
  1355. * @param array $data 消息结构{"articles":[{...}]}
  1356. * @return boolean|array
  1357. */
  1358. public function uploadArticles($data){
  1359. if (!$this->access_token && !$this->checkAuth()) return false;
  1360. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOADNEWS_URL.'access_token='.$this->access_token,self::json_encode($data));
  1361. if ($result)
  1362. {
  1363. $json = json_decode($result,true);
  1364. if (!$json || !empty($json['errcode'])) {
  1365. $this->errCode = $json['errcode'];
  1366. $this->errMsg = $json['errmsg'];
  1367. return false;
  1368. }
  1369. return $json;
  1370. }
  1371. return false;
  1372. }
  1373. /**
  1374. * 上传视频素材(认证后的订阅号可用)
  1375. * @param array $data 消息结构
  1376. * {
  1377. * "media_id"=>"", //通过上传媒体接口得到的MediaId
  1378. * "title"=>"TITLE", //视频标题
  1379. * "description"=>"Description" //视频描述
  1380. * }
  1381. * @return boolean|array
  1382. * {
  1383. * "type":"video",
  1384. * "media_id":"mediaid",
  1385. * "created_at":1398848981
  1386. * }
  1387. */
  1388. public function uploadMpVideo($data){
  1389. if (!$this->access_token && !$this->checkAuth()) return false;
  1390. $result = $this->http_post(self::UPLOAD_MEDIA_URL.self::MEDIA_VIDEO_UPLOAD.'access_token='.$this->access_token,self::json_encode($data));
  1391. if ($result)
  1392. {
  1393. $json = json_decode($result,true);
  1394. if (!$json || !empty($json['errcode'])) {
  1395. $this->errCode = $json['errcode'];
  1396. $this->errMsg = $json['errmsg'];
  1397. return false;
  1398. }
  1399. return $json;
  1400. }
  1401. return false;
  1402. }
  1403. /**
  1404. * 高级群发消息, 根据OpenID列表群发图文消息(订阅号不可用)
  1405. * 注意:视频需要在调用uploadMedia()方法后,再使用 uploadMpVideo() 方法生成,
  1406. * 然后获得的 mediaid 才能用于群发,且消息类型为 mpvideo 类型。
  1407. * @param array $data 消息结构
  1408. * {
  1409. * "touser"=>array(
  1410. * "OPENID1",
  1411. * "OPENID2"
  1412. * ),
  1413. * "msgtype"=>"mpvideo",
  1414. * // 在下面5种类型中选择对应的参数内容
  1415. * // mpnews | voice | image | mpvideo => array( "media_id"=>"MediaId")
  1416. * // text => array ( "content" => "hello")
  1417. * }
  1418. * @return boolean|array
  1419. */
  1420. public function sendMassMessage($data){
  1421. if (!$this->access_token && !$this->checkAuth()) return false;
  1422. $result = $this->http_post(self::API_URL_PREFIX.self::MASS_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
  1423. if ($result)
  1424. {
  1425. $json = json_decode($result,true);
  1426. if (!$json || !empty($json['errcode'])) {
  1427. $this->errCode = $json['errcode'];
  1428. $this->errMsg = $json['errmsg'];
  1429. return false;
  1430. }
  1431. return $json;
  1432. }
  1433. return false;
  1434. }
  1435. /**
  1436. * 高级群发消息, 根据群组id群发图文消息(认证后的订阅号可用)
  1437. * 注意:视频需要在调用uploadMedia()方法后,再使用 uploadMpVideo() 方法生成,
  1438. * 然后获得的 mediaid 才能用于群发,且消息类型为 mpvideo 类型。
  1439. * @param array $data 消息结构
  1440. * {
  1441. * "filter"=>array(
  1442. * "is_to_all"=>False, //是否群发给所有用户.True不用分组id,False需填写分组id
  1443. * "group_id"=>"2" //群发的分组id
  1444. * ),
  1445. * "msgtype"=>"mpvideo",
  1446. * // 在下面5种类型中选择对应的参数内容
  1447. * // mpnews | voice | image | mpvideo => array( "media_id"=>"MediaId")
  1448. * // text => array ( "content" => "hello")
  1449. * }
  1450. * @return boolean|array
  1451. */
  1452. public function sendGroupMassMessage($data){
  1453. if (!$this->access_token && !$this->checkAuth()) return false;
  1454. $result = $this->http_post(self::API_URL_PREFIX.self::MASS_SEND_GROUP_URL.'access_token='.$this->access_token,self::json_encode($data));
  1455. if ($result)
  1456. {
  1457. $json = json_decode($result,true);
  1458. if (!$json || !empty($json['errcode'])) {
  1459. $this->errCode = $json['errcode'];
  1460. $this->errMsg = $json['errmsg'];
  1461. return false;
  1462. }
  1463. return $json;
  1464. }
  1465. return false;
  1466. }
  1467. /**
  1468. * 高级群发消息, 删除群发图文消息(认证后的订阅号可用)
  1469. * @param int $msg_id 消息id
  1470. * @return boolean|array
  1471. */
  1472. public function deleteMassMessage($msg_id){
  1473. if (!$this->access_token && !$this->checkAuth()) return false;
  1474. $result = $this->http_post(self::API_URL_PREFIX.self::MASS_DELETE_URL.'access_token='.$this->access_token,self::json_encode(array('msg_id'=>$msg_id)));
  1475. if ($result)
  1476. {
  1477. $json = json_decode($result,true);
  1478. if (!$json || !empty($json['errcode'])) {
  1479. $this->errCode = $json['errcode'];
  1480. $this->errMsg = $json['errmsg'];
  1481. return false;
  1482. }
  1483. return true;
  1484. }
  1485. return false;
  1486. }
  1487. /**
  1488. * 高级群发消息, 预览群发消息(认证后的订阅号可用)
  1489. * 注意:视频需要在调用uploadMedia()方法后,再使用 uploadMpVideo() 方法生成,
  1490. * 然后获得的 mediaid 才能用于群发,且消息类型为 mpvideo 类型。
  1491. * @param array $data 消息结构
  1492. * {
  1493. * "touser"=>"OPENID",
  1494. * "msgtype"=>"mpvideo",
  1495. * // 在下面5种类型中选择对应的参数内容
  1496. * // mpnews | voice | image | mpvideo => array( "media_id"=>"MediaId")
  1497. * // text => array ( "content" => "hello")
  1498. * }
  1499. * @return boolean|array
  1500. */
  1501. public function previewMassMessage($data){
  1502. if (!$this->access_token && !$this->checkAuth()) return false;
  1503. $result = $this->http_post(self::API_URL_PREFIX.self::MASS_PREVIEW_URL.'access_token='.$this->access_token,self::json_encode($data));
  1504. if ($result)
  1505. {
  1506. $json = json_decode($result,true);
  1507. if (!$json || !empty($json['errcode'])) {
  1508. $this->errCode = $json['errcode'];
  1509. $this->errMsg = $json['errmsg'];
  1510. return false;
  1511. }
  1512. return $json;
  1513. }
  1514. return false;
  1515. }
  1516. /**
  1517. * 高级群发消息, 查询群发消息发送状态(认证后的订阅号可用)
  1518. * @param int $msg_id 消息id
  1519. * @return boolean|array
  1520. * {
  1521. * "msg_id":201053012, //群发消息后返回的消息id
  1522. * "msg_status":"SEND_SUCCESS" //消息发送后的状态,SENDING表示正在发送 SEND_SUCCESS表示发送成功
  1523. * }
  1524. */
  1525. public function queryMassMessage($msg_id){
  1526. if (!$this->access_token && !$this->checkAuth()) return false;
  1527. $result = $this->http_post(self::API_URL_PREFIX.self::MASS_QUERY_URL.'access_token='.$this->access_token,self::json_encode(array('msg_id'=>$msg_id)));
  1528. if ($result)
  1529. {
  1530. $json = json_decode($result,true);
  1531. if (!$json || !empty($json['errcode'])) {
  1532. $this->errCode = $json['errcode'];
  1533. $this->errMsg = $json['errmsg'];
  1534. return false;
  1535. }
  1536. return $json;
  1537. }
  1538. return false;
  1539. }
  1540. /**
  1541. * 创建二维码ticket
  1542. * @param int $scene_id 自定义追踪id
  1543. * @param int $type 0:临时二维码;1:永久二维码(此时expire参数无效)
  1544. * @param int $expire 临时二维码有效期,最大为1800秒
  1545. * @return array('ticket'=>'qrcode字串','expire_seconds'=>1800,'url'=>'二维码图片解析后的地址')
  1546. */
  1547. public function getQRCode($scene_id,$type=0,$expire=1800){
  1548. if (!$this->access_token && !$this->checkAuth()) return false;
  1549. $data = array(
  1550. 'action_name'=>$type?"QR_LIMIT_SCENE":"QR_SCENE",
  1551. 'expire_seconds'=>$expire,
  1552. 'action_info'=>array('scene'=>array('scene_id'=>$scene_id))
  1553. );
  1554. if ($type == 1) {
  1555. unset($data['expire_seconds']);
  1556. }
  1557. $result = $this->http_post(self::API_URL_PREFIX.self::QRCODE_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1558. if ($result)
  1559. {
  1560. $json = json_decode($result,true);
  1561. if (!$json || !empty($json['errcode'])) {
  1562. $this->errCode = $json['errcode'];
  1563. $this->errMsg = $json['errmsg'];
  1564. return false;
  1565. }
  1566. return $json;
  1567. }
  1568. return false;
  1569. }
  1570. /**
  1571. * 获取二维码图片
  1572. * @param string $ticket 传入由getQRCode方法生成的ticket参数
  1573. * @return string url 返回http地址
  1574. */
  1575. public function getQRUrl($ticket) {
  1576. return self::QRCODE_IMG_URL.$ticket;
  1577. }
  1578. /**
  1579. * 长链接转短链接接口
  1580. * @param string $long_url 传入要转换的长url
  1581. * @return boolean|string url 成功则返回转换后的短url
  1582. */
  1583. public function getShortUrl($long_url){
  1584. if (!$this->access_token && !$this->checkAuth()) return false;
  1585. $data = array(
  1586. 'action'=>'long2short',
  1587. 'long_url'=>$long_url
  1588. );
  1589. $result = $this->http_post(self::API_URL_PREFIX.self::SHORT_URL.'access_token='.$this->access_token,self::json_encode($data));
  1590. if ($result)
  1591. {
  1592. $json = json_decode($result,true);
  1593. if (!$json || !empty($json['errcode'])) {
  1594. $this->errCode = $json['errcode'];
  1595. $this->errMsg = $json['errmsg'];
  1596. return false;
  1597. }
  1598. return $json['short_url'];
  1599. }
  1600. return false;
  1601. }
  1602. /**
  1603. * 批量获取关注用户列表
  1604. * @param unknown $next_openid
  1605. */
  1606. public function getUserList($next_openid=''){
  1607. if (!$this->access_token && !$this->checkAuth()) return false;
  1608. $result = $this->http_get(self::API_URL_PREFIX.self::USER_GET_URL.'access_token='.$this->access_token.'&next_openid='.$next_openid);
  1609. if ($result)
  1610. {
  1611. $json = json_decode($result,true);
  1612. if (isset($json['errcode'])) {
  1613. $this->errCode = $json['errcode'];
  1614. $this->errMsg = $json['errmsg'];
  1615. return false;
  1616. }
  1617. return $json;
  1618. }
  1619. return false;
  1620. }
  1621. /**
  1622. * 获取关注者详细信息
  1623. * @param string $openid
  1624. * @return array {subscribe,openid,nickname,sex,city,province,country,language,headimgurl,subscribe_time,[unionid]}
  1625. * 注意:unionid字段 只有在用户将公众号绑定到微信开放平台账号后,才会出现。建议调用前用isset()检测一下
  1626. */
  1627. public function getUserInfo($openid){
  1628. if (!$this->access_token && !$this->checkAuth()) return false;
  1629. $result = $this->http_get(self::API_URL_PREFIX.self::USER_INFO_URL.'access_token='.$this->access_token.'&openid='.$openid);
  1630. if ($result)
  1631. {
  1632. $json = json_decode($result,true);
  1633. if (isset($json['errcode'])) {
  1634. $this->errCode = $json['errcode'];
  1635. $this->errMsg = $json['errmsg'];
  1636. return false;
  1637. }
  1638. return $json;
  1639. }
  1640. return false;
  1641. }
  1642. /**
  1643. * 设置用户备注名
  1644. * @param string $openid
  1645. * @param string $remark 备注名
  1646. * @return boolean|array
  1647. */
  1648. public function updateUserRemark($openid,$remark){
  1649. if (!$this->access_token && !$this->checkAuth()) return false;
  1650. $data = array(
  1651. 'openid'=>$openid,
  1652. 'remark'=>$remark
  1653. );
  1654. $result = $this->http_post(self::API_URL_PREFIX.self::USER_UPDATEREMARK_URL.'access_token='.$this->access_token,self::json_encode($data));
  1655. if ($result)
  1656. {
  1657. $json = json_decode($result,true);
  1658. if (!$json || !empty($json['errcode'])) {
  1659. $this->errCode = $json['errcode'];
  1660. $this->errMsg = $json['errmsg'];
  1661. return false;
  1662. }
  1663. return $json;
  1664. }
  1665. return false;
  1666. }
  1667. /**
  1668. * 获取用户分组列表
  1669. * @return boolean|array
  1670. */
  1671. public function getGroup(){
  1672. if (!$this->access_token && !$this->checkAuth()) return false;
  1673. $result = $this->http_get(self::API_URL_PREFIX.self::GROUP_GET_URL.'access_token='.$this->access_token);
  1674. if ($result)
  1675. {
  1676. $json = json_decode($result,true);
  1677. if (isset($json['errcode'])) {
  1678. $this->errCode = $json['errcode'];
  1679. $this->errMsg = $json['errmsg'];
  1680. return false;
  1681. }
  1682. return $json;
  1683. }
  1684. return false;
  1685. }
  1686. /**
  1687. * 获取用户所在分组
  1688. * @param string $openid
  1689. * @return boolean|int 成功则返回用户分组id
  1690. */
  1691. public function getUserGroup($openid){
  1692. if (!$this->access_token && !$this->checkAuth()) return false;
  1693. $data = array(
  1694. 'openid'=>$openid
  1695. );
  1696. $result = $this->http_post(self::API_URL_PREFIX.self::USER_GROUP_URL.'access_token='.$this->access_token,self::json_encode($data));
  1697. if ($result)
  1698. {
  1699. $json = json_decode($result,true);
  1700. if (!$json || !empty($json['errcode'])) {
  1701. $this->errCode = $json['errcode'];
  1702. $this->errMsg = $json['errmsg'];
  1703. return false;
  1704. } else
  1705. if (isset($json['groupid'])) return $json['groupid'];
  1706. }
  1707. return false;
  1708. }
  1709. /**
  1710. * 新增自定分组
  1711. * @param string $name 分组名称
  1712. * @return boolean|array
  1713. */
  1714. public function createGroup($name){
  1715. if (!$this->access_token && !$this->checkAuth()) return false;
  1716. $data = array(
  1717. 'group'=>array('name'=>$name)
  1718. );
  1719. $result = $this->http_post(self::API_URL_PREFIX.self::GROUP_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1720. if ($result)
  1721. {
  1722. $json = json_decode($result,true);
  1723. if (!$json || !empty($json['errcode'])) {
  1724. $this->errCode = $json['errcode'];
  1725. $this->errMsg = $json['errmsg'];
  1726. return false;
  1727. }
  1728. return $json;
  1729. }
  1730. return false;
  1731. }
  1732. /**
  1733. * 更改分组名称
  1734. * @param int $groupid 分组id
  1735. * @param string $name 分组名称
  1736. * @return boolean|array
  1737. */
  1738. public function updateGroup($groupid,$name){
  1739. if (!$this->access_token && !$this->checkAuth()) return false;
  1740. $data = array(
  1741. 'group'=>array('id'=>$groupid,'name'=>$name)
  1742. );
  1743. $result = $this->http_post(self::API_URL_PREFIX.self::GROUP_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1744. if ($result)
  1745. {
  1746. $json = json_decode($result,true);
  1747. if (!$json || !empty($json['errcode'])) {
  1748. $this->errCode = $json['errcode'];
  1749. $this->errMsg = $json['errmsg'];
  1750. return false;
  1751. }
  1752. return $json;
  1753. }
  1754. return false;
  1755. }
  1756. /**
  1757. * 移动用户分组
  1758. * @param int $groupid 分组id
  1759. * @param string $openid 用户openid
  1760. * @return boolean|array
  1761. */
  1762. public function updateGroupMembers($groupid,$openid){
  1763. if (!$this->access_token && !$this->checkAuth()) return false;
  1764. $data = array(
  1765. 'openid'=>$openid,
  1766. 'to_groupid'=>$groupid
  1767. );
  1768. $result = $this->http_post(self::API_URL_PREFIX.self::GROUP_MEMBER_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1769. if ($result)
  1770. {
  1771. $json = json_decode($result,true);
  1772. if (!$json || !empty($json['errcode'])) {
  1773. $this->errCode = $json['errcode'];
  1774. $this->errMsg = $json['errmsg'];
  1775. return false;
  1776. }
  1777. return $json;
  1778. }
  1779. return false;
  1780. }
  1781. /**
  1782. * 发送客服消息
  1783. * @param array $data 消息结构{"touser":"OPENID","msgtype":"news","news":{...}}
  1784. * @return boolean|array
  1785. */
  1786. public function sendCustomMessage($data){
  1787. if (!$this->access_token && !$this->checkAuth()) return false;
  1788. $result = $this->http_post(self::API_URL_PREFIX.self::CUSTOM_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
  1789. if ($result)
  1790. {
  1791. $json = json_decode($result,true);
  1792. if (!$json || !empty($json['errcode'])) {
  1793. $this->errCode = $json['errcode'];
  1794. $this->errMsg = $json['errmsg'];
  1795. return false;
  1796. }
  1797. return $json;
  1798. }
  1799. return false;
  1800. }
  1801. /**
  1802. * oauth 授权跳转接口
  1803. * @param string $callback 回调URI
  1804. * @return string
  1805. */
  1806. public function getOauthRedirect($callback,$state='',$scope='snsapi_userinfo'){
  1807. return self::OAUTH_PREFIX.self::OAUTH_AUTHORIZE_URL.'appid='.$this->appid.'&redirect_uri='.urlencode($callback).'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';
  1808. }
  1809. /**
  1810. * 通过code获取Access Token
  1811. * @return array {access_token,expires_in,refresh_token,openid,scope}
  1812. */
  1813. public function getOauthAccessToken(){
  1814. $code = isset($_GET['code'])?$_GET['code']:'';
  1815. if (!$code) return false;
  1816. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::OAUTH_TOKEN_URL.'appid='.$this->appid.'&secret='.$this->appsecret.'&code='.$code.'&grant_type=authorization_code');
  1817. if ($result)
  1818. {
  1819. $json = json_decode($result,true);
  1820. if (!$json || !empty($json['errcode'])) {
  1821. $this->errCode = $json['errcode'];
  1822. $this->errMsg = $json['errmsg'];
  1823. return false;
  1824. }
  1825. $this->user_token = $json['access_token'];
  1826. return $json;
  1827. }
  1828. return false;
  1829. }
  1830. /**
  1831. * 刷新access token并续期
  1832. * @param string $refresh_token
  1833. * @return boolean|mixed
  1834. */
  1835. public function getOauthRefreshToken($refresh_token){
  1836. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::OAUTH_REFRESH_URL.'appid='.$this->appid.'&grant_type=refresh_token&refresh_token='.$refresh_token);
  1837. if ($result)
  1838. {
  1839. $json = json_decode($result,true);
  1840. if (!$json || !empty($json['errcode'])) {
  1841. $this->errCode = $json['errcode'];
  1842. $this->errMsg = $json['errmsg'];
  1843. return false;
  1844. }
  1845. $this->user_token = $json['access_token'];
  1846. return $json;
  1847. }
  1848. return false;
  1849. }
  1850. /**
  1851. * 获取授权后的用户资料
  1852. * @param string $access_token
  1853. * @param string $openid
  1854. * @return array {openid,nickname,sex,province,city,country,headimgurl,privilege,[unionid]}
  1855. * 注意:unionid字段 只有在用户将公众号绑定到微信开放平台账号后,才会出现。建议调用前用isset()检测一下
  1856. */
  1857. public function getOauthUserinfo($access_token,$openid){
  1858. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::OAUTH_USERINFO_URL.'access_token='.$access_token.'&openid='.$openid);
  1859. if ($result)
  1860. {
  1861. $json = json_decode($result,true);
  1862. if (!$json || !empty($json['errcode'])) {
  1863. $this->errCode = $json['errcode'];
  1864. $this->errMsg = $json['errmsg'];
  1865. return false;
  1866. }
  1867. return $json;
  1868. }
  1869. return false;
  1870. }
  1871. /**
  1872. * 检验授权凭证是否有效
  1873. * @param string $access_token
  1874. * @param string $openid
  1875. * @return boolean 是否有效
  1876. */
  1877. public function getOauthAuth($access_token,$openid){
  1878. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::OAUTH_AUTH_URL.'access_token='.$access_token.'&openid='.$openid);
  1879. if ($result)
  1880. {
  1881. $json = json_decode($result,true);
  1882. if (!$json || !empty($json['errcode'])) {
  1883. $this->errCode = $json['errcode'];
  1884. $this->errMsg = $json['errmsg'];
  1885. return false;
  1886. } else
  1887. if ($json['errcode']==0) return true;
  1888. }
  1889. return false;
  1890. }
  1891. /**
  1892. * 获取签名
  1893. * @param array $arrdata 签名数组
  1894. * @param string $method 签名方法
  1895. * @return boolean|string 签名值
  1896. */
  1897. public function getSignature($arrdata,$method="sha1") {
  1898. if (!function_exists($method)) return false;
  1899. ksort($arrdata);
  1900. $paramstring = "";
  1901. foreach($arrdata as $key => $value)
  1902. {
  1903. if(strlen($paramstring) == 0)
  1904. $paramstring .= $key . "=" . $value;
  1905. else
  1906. $paramstring .= "&" . $key . "=" . $value;
  1907. }
  1908. $paySign = $method($paramstring);
  1909. return $paySign;
  1910. }
  1911. /**
  1912. * 生成随机字串
  1913. * @param number $length 长度,默认为16,最长为32字节
  1914. * @return string
  1915. */
  1916. public function generateNonceStr($length=16){
  1917. // 密码字符集,可任意添加你需要的字符
  1918. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  1919. $str = "";
  1920. for($i = 0; $i < $length; $i++)
  1921. {
  1922. $str .= $chars[mt_rand(0, strlen($chars) - 1)];
  1923. }
  1924. return $str;
  1925. }
  1926. /**
  1927. * 生成原生支付url
  1928. * @param number $productid 商品编号,最长为32字节
  1929. * @return string
  1930. */
  1931. public function createNativeUrl($productid){
  1932. $nativeObj["appid"] = $this->appid;
  1933. $nativeObj["appkey"] = $this->paysignkey;
  1934. $nativeObj["productid"] = urlencode($productid);
  1935. $nativeObj["timestamp"] = time();
  1936. $nativeObj["noncestr"] = $this->generateNonceStr();
  1937. $nativeObj["sign"] = $this->getSignature($nativeObj);
  1938. unset($nativeObj["appkey"]);
  1939. $bizString = "";
  1940. foreach($nativeObj as $key => $value)
  1941. {
  1942. if(strlen($bizString) == 0)
  1943. $bizString .= $key . "=" . $value;
  1944. else
  1945. $bizString .= "&" . $key . "=" . $value;
  1946. }
  1947. return "weixin://wxpay/bizpayurl?".$bizString;
  1948. //weixin://wxpay/bizpayurl?sign=XXXXX&appid=XXXXXX&productid=XXXXXX&timestamp=XXXXXX&noncestr=XXXXXX
  1949. }
  1950. /**
  1951. * 生成订单package字符串
  1952. * @param string $out_trade_no 必填,商户系统内部的订单号,32个字符内,确保在商户系统唯一
  1953. * @param string $body 必填,商品描述,128 字节以下
  1954. * @param int $total_fee 必填,订单总金额,单位为分
  1955. * @param string $notify_url 必填,支付完成通知回调接口,255 字节以内
  1956. * @param string $spbill_create_ip 必填,用户终端IP,IPV4字串,15字节内
  1957. * @param int $fee_type 必填,现金支付币种,默认1:人民币
  1958. * @param string $bank_type 必填,银行通道类型,默认WX
  1959. * @param string $input_charset 必填,传入参数字符编码,默认UTF-8,取值有UTF-8和GBK
  1960. * @param string $time_start 交易起始时间,订单生成时间,格式yyyyMMddHHmmss
  1961. * @param string $time_expire 交易结束时间,也是订单失效时间
  1962. * @param int $transport_fee 物流费用,单位为分
  1963. * @param int $product_fee 商品费用,单位为分,必须保证 transport_fee + product_fee=total_fee
  1964. * @param string $goods_tag 商品标记,优惠券时可能用到
  1965. * @param string $attach 附加数据,notify接口原样返回
  1966. * @return string
  1967. */
  1968. public function createPackage($out_trade_no,$body,$total_fee,$notify_url,$spbill_create_ip,$fee_type=1,$bank_type="WX",$input_charset="UTF-8",$time_start="",$time_expire="",$transport_fee="",$product_fee="",$goods_tag="",$attach=""){
  1969. $arrdata = array("bank_type" => $bank_type, "body" => $body, "partner" => $this->partnerid, "out_trade_no" => $out_trade_no, "total_fee" => $total_fee, "fee_type" => $fee_type, "notify_url" => $notify_url, "spbill_create_ip" => $spbill_create_ip, "input_charset" => $input_charset);
  1970. if ($time_start) $arrdata['time_start'] = $time_start;
  1971. if ($time_expire) $arrdata['time_expire'] = $time_expire;
  1972. if ($transport_fee) $arrdata['transport_fee'] = $transport_fee;
  1973. if ($product_fee) $arrdata['product_fee'] = $product_fee;
  1974. if ($goods_tag) $arrdata['goods_tag'] = $goods_tag;
  1975. if ($attach) $arrdata['attach'] = $attach;
  1976. ksort($arrdata);
  1977. $paramstring = "";
  1978. foreach($arrdata as $key => $value)
  1979. {
  1980. if(strlen($paramstring) == 0)
  1981. $paramstring .= $key . "=" . $value;
  1982. else
  1983. $paramstring .= "&" . $key . "=" . $value;
  1984. }
  1985. $stringSignTemp = $paramstring . "&key=" . $this->partnerkey;
  1986. $signValue = strtoupper(md5($stringSignTemp));
  1987. $package = http_build_query($arrdata) . "&sign=" . $signValue;
  1988. return $package;
  1989. }
  1990. /**
  1991. * 支付签名(paySign)生成方法
  1992. * @param string $package 订单详情字串
  1993. * @param string $timeStamp 当前时间戳(需与JS输出的一致)
  1994. * @param string $nonceStr 随机串(需与JS输出的一致)
  1995. * @return string 返回签名字串
  1996. */
  1997. public function getPaySign($package, $timeStamp, $nonceStr){
  1998. $arrdata = array("appid" => $this->appid, "timestamp" => $timeStamp, "noncestr" => $nonceStr, "package" => $package, "appkey" => $this->paysignkey);
  1999. $paySign = $this->getSignature($arrdata);
  2000. return $paySign;
  2001. }
  2002. /**
  2003. * 回调通知签名验证
  2004. * @param array $orderxml 返回的orderXml的数组表示,留空则自动从post数据获取
  2005. * @return boolean
  2006. */
  2007. public function checkOrderSignature($orderxml=''){
  2008. if (!$orderxml) {
  2009. $postStr = file_get_contents("php://input");
  2010. if (!empty($postStr)) {
  2011. $orderxml = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  2012. } else return false;
  2013. }
  2014. $arrdata = array('appid'=>$orderxml['AppId'],'appkey'=>$this->paysignkey,'timestamp'=>$orderxml['TimeStamp'],'noncestr'=>$orderxml['NonceStr'],'openid'=>$orderxml['OpenId'],'issubscribe'=>$orderxml['IsSubscribe']);
  2015. $paySign = $this->getSignature($arrdata);
  2016. if ($paySign!=$orderxml['AppSignature']) return false;
  2017. return true;
  2018. }
  2019. /**
  2020. * 发货通知
  2021. * @param string $openid 用户open_id
  2022. * @param string $transid 交易单号
  2023. * @param string $out_trade_no 第三方订单号
  2024. * @param int $status 0:发货失败;1:已发货
  2025. * @param string $msg 失败原因
  2026. * @return boolean|array
  2027. */
  2028. public function sendPayDeliverNotify($openid,$transid,$out_trade_no,$status=1,$msg='ok'){
  2029. if (!$this->access_token && !$this->checkAuth()) return false;
  2030. $postdata = array(
  2031. "appid"=>$this->appid,
  2032. "appkey"=>$this->paysignkey,
  2033. "openid"=>$openid,
  2034. "transid"=>strval($transid),
  2035. "out_trade_no"=>strval($out_trade_no),
  2036. "deliver_timestamp"=>strval(time()),
  2037. "deliver_status"=>strval($status),
  2038. "deliver_msg"=>$msg,
  2039. );
  2040. $postdata['app_signature'] = $this->getSignature($postdata);
  2041. $postdata['sign_method'] = 'sha1';
  2042. unset($postdata['appkey']);
  2043. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::PAY_DELIVERNOTIFY.'access_token='.$this->access_token,self::json_encode($postdata));
  2044. if ($result)
  2045. {
  2046. $json = json_decode($result,true);
  2047. if (!$json || !empty($json['errcode'])) {
  2048. $this->errCode = $json['errcode'];
  2049. $this->errMsg = $json['errmsg'];
  2050. return false;
  2051. }
  2052. return $json;
  2053. }
  2054. return false;
  2055. }
  2056. /**
  2057. * 查询订单信息
  2058. * @param string $out_trade_no 订单号
  2059. * @return boolean|array
  2060. */
  2061. public function getPayOrder($out_trade_no) {
  2062. if (!$this->access_token && !$this->checkAuth()) return false;
  2063. $sign = strtoupper(md5("out_trade_no=$out_trade_no&partner={$this->partnerid}&key={$this->partnerkey}"));
  2064. $postdata = array(
  2065. "appid"=>$this->appid,
  2066. "appkey"=>$this->paysignkey,
  2067. "package"=>"out_trade_no=$out_trade_no&partner={$this->partnerid}&sign=$sign",
  2068. "timestamp"=>strval(time()),
  2069. );
  2070. $postdata['app_signature'] = $this->getSignature($postdata);
  2071. $postdata['sign_method'] = 'sha1';
  2072. unset($postdata['appkey']);
  2073. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::PAY_ORDERQUERY.'access_token='.$this->access_token,self::json_encode($postdata));
  2074. if ($result)
  2075. {
  2076. $json = json_decode($result,true);
  2077. if (!$json || !empty($json['errcode'])) {
  2078. $this->errCode = $json['errcode'];
  2079. $this->errMsg = $json['errmsg'].json_encode($postdata);
  2080. return false;
  2081. }
  2082. return $json["order_info"];
  2083. }
  2084. return false;
  2085. }
  2086. /**
  2087. * 获取收货地址JS的签名
  2088. * @tutorial 参考weixin.js脚本的WeixinJS.editAddress方法调用
  2089. * @param string $appId
  2090. * @param string $url
  2091. * @param int $timeStamp
  2092. * @param string $nonceStr
  2093. * @param string $user_token
  2094. * @return Ambigous <boolean, string>
  2095. */
  2096. public function getAddrSign($url, $timeStamp, $nonceStr, $user_token=''){
  2097. if (!$user_token) $user_token = $this->user_token;
  2098. if (!$user_token) {
  2099. $this->errMsg = 'no user access token found!';
  2100. return false;
  2101. }
  2102. $url = htmlspecialchars_decode($url);
  2103. $arrdata = array(
  2104. 'appid'=>$this->appid,
  2105. 'url'=>$url,
  2106. 'timestamp'=>strval($timeStamp),
  2107. 'noncestr'=>$nonceStr,
  2108. 'accesstoken'=>$user_token
  2109. );
  2110. return $this->getSignature($arrdata);
  2111. }
  2112. /**
  2113. * 模板消息 设置所属行业
  2114. * @param int $id1 公众号模板消息所属行业编号,参看官方开发文档 行业代码
  2115. * @param int $id2 同$id1。但如果只有一个行业,此参数可省略
  2116. * @return boolean|array
  2117. */
  2118. public function setTMIndustry($id1,$id2=''){
  2119. if ($id1) $data['industry_id1'] = $id1;
  2120. if ($id2) $data['industry_id2'] = $id2;
  2121. if (!$this->access_token && !$this->checkAuth()) return false;
  2122. $result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_SET_INDUSTRY_URL.'access_token='.$this->access_token,self::json_encode($data));
  2123. if($result){
  2124. $json = json_decode($result,true);
  2125. if (!$json || !empty($json['errcode'])) {
  2126. $this->errCode = $json['errcode'];
  2127. $this->errMsg = $json['errmsg'];
  2128. return false;
  2129. }
  2130. return $json;
  2131. }
  2132. return false;
  2133. }
  2134. /**
  2135. * 模板消息 添加消息模板
  2136. * 成功返回消息模板的调用id
  2137. * @param string $tpl_id 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
  2138. * @return boolean|string
  2139. */
  2140. public function addTemplateMessage($tpl_id){
  2141. $data = array ('template_id_short' =>$tpl_id);
  2142. if (!$this->access_token && !$this->checkAuth()) return false;
  2143. $result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_ADD_TPL_URL.'access_token='.$this->access_token,self::json_encode($data));
  2144. if($result){
  2145. $json = json_decode($result,true);
  2146. if (!$json || !empty($json['errcode'])) {
  2147. $this->errCode = $json['errcode'];
  2148. $this->errMsg = $json['errmsg'];
  2149. return false;
  2150. }
  2151. return $json['template_id'];
  2152. }
  2153. return false;
  2154. }
  2155. /**
  2156. * 发送模板消息
  2157. * @param array $data 消息结构
  2158. * {
  2159. "touser":"OPENID",
  2160. "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
  2161. "url":"http://weixin.qq.com/download",
  2162. "topcolor":"#FF0000",
  2163. "data":{
  2164. "参数名1": {
  2165. "value":"参数",
  2166. "color":"#173177" //参数颜色
  2167. },
  2168. "Date":{
  2169. "value":"06月07日 19时24分",
  2170. "color":"#173177"
  2171. },
  2172. "CardNumber":{
  2173. "value":"0426",
  2174. "color":"#173177"
  2175. },
  2176. "Type":{
  2177. "value":"消费",
  2178. "color":"#173177"
  2179. }
  2180. }
  2181. }
  2182. * @return boolean|array
  2183. */
  2184. public function sendTemplateMessage($data){
  2185. if (!$this->access_token && !$this->checkAuth()) return false;
  2186. $result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
  2187. if($result){
  2188. $json = json_decode($result,true);
  2189. if (!$json || !empty($json['errcode'])) {
  2190. $this->errCode = $json['errcode'];
  2191. $this->errMsg = $json['errmsg'];
  2192. return false;
  2193. }
  2194. return $json;
  2195. }
  2196. return false;
  2197. }
  2198. /**
  2199. * 获取多客服会话记录
  2200. * @param array $data 数据结构{"starttime":123456789,"endtime":987654321,"openid":"OPENID","pagesize":10,"pageindex":1,}
  2201. * @return boolean|array
  2202. */
  2203. public function getCustomServiceMessage($data){
  2204. if (!$this->access_token && !$this->checkAuth()) return false;
  2205. $result = $this->http_post(self::API_URL_PREFIX.self::CUSTOM_SERVICE_GET_RECORD.'access_token='.$this->access_token,self::json_encode($data));
  2206. if ($result)
  2207. {
  2208. $json = json_decode($result,true);
  2209. if (!$json || !empty($json['errcode'])) {
  2210. $this->errCode = $json['errcode'];
  2211. $this->errMsg = $json['errmsg'];
  2212. return false;
  2213. }
  2214. return $json;
  2215. }
  2216. return false;
  2217. }
  2218. /**
  2219. * 转发多客服消息
  2220. * Example: $obj->transfer_customer_service($customer_account)->reply();
  2221. * @param string $customer_account 转发到指定客服帐号:test1@test
  2222. */
  2223. public function transfer_customer_service($customer_account = '')
  2224. {
  2225. $msg = array(
  2226. 'ToUserName' => $this->getRevFrom(),
  2227. 'FromUserName'=>$this->getRevTo(),
  2228. 'CreateTime'=>time(),
  2229. 'MsgType'=>'transfer_customer_service',
  2230. );
  2231. if (!$customer_account) {
  2232. $msg['TransInfo'] = array('KfAccount'=>$customer_account);
  2233. }
  2234. $this->Message($msg);
  2235. return $this;
  2236. }
  2237. /**
  2238. * 获取多客服客服基本信息
  2239. *
  2240. * @return boolean|array
  2241. */
  2242. public function getCustomServiceKFlist(){
  2243. if (!$this->access_token && !$this->checkAuth()) return false;
  2244. $result = $this->http_get(self::API_URL_PREFIX.self::CUSTOM_SERVICE_GET_KFLIST.'access_token='.$this->access_token);
  2245. if ($result)
  2246. {
  2247. $json = json_decode($result,true);
  2248. if (!$json || !empty($json['errcode'])) {
  2249. $this->errCode = $json['errcode'];
  2250. $this->errMsg = $json['errmsg'];
  2251. return false;
  2252. }
  2253. return $json;
  2254. }
  2255. return false;
  2256. }
  2257. /**
  2258. * 获取多客服在线客服接待信息
  2259. *
  2260. * @return boolean|array {
  2261. "kf_online_list": [
  2262. {
  2263. "kf_account": "test1@test", //客服账号@微信别名
  2264. "status": 1, //客服在线状态 1:pc在线,2:手机在线,若pc和手机同时在线则为 1+2=3
  2265. "kf_id": "1001", //客服工号
  2266. "auto_accept": 0, //客服设置的最大自动接入数
  2267. "accepted_case": 1 //客服当前正在接待的会话数
  2268. }
  2269. ]
  2270. }
  2271. */
  2272. public function getCustomServiceOnlineKFlist(){
  2273. if (!$this->access_token && !$this->checkAuth()) return false;
  2274. $result = $this->http_get(self::API_URL_PREFIX.self::CUSTOM_SERVICE_GET_ONLINEKFLIST.'access_token='.$this->access_token);
  2275. if ($result)
  2276. {
  2277. $json = json_decode($result,true);
  2278. if (!$json || !empty($json['errcode'])) {
  2279. $this->errCode = $json['errcode'];
  2280. $this->errMsg = $json['errmsg'];
  2281. return false;
  2282. }
  2283. return $json;
  2284. }
  2285. return false;
  2286. }
  2287. /**
  2288. * 创建指定多客服会话
  2289. * @tutorial 当用户已被其他客服接待或指定客服不在线则会失败
  2290. * @param string $openid //用户openid
  2291. * @param string $kf_account //客服账号
  2292. * @param string $text //附加信息,文本会展示在客服人员的多客服客户端,可为空
  2293. * @return boolean | array //成功返回json数组
  2294. * {
  2295. * "errcode": 0,
  2296. * "errmsg": "ok",
  2297. * }
  2298. */
  2299. public function createKFSession($openid,$kf_account,$text=''){
  2300. $data=array(
  2301. "openid" =>$openid,
  2302. "nickname" => $kf_account
  2303. );
  2304. if ($text) $data["text"] = $text;
  2305. if (!$this->access_token && !$this->checkAuth()) return false;
  2306. $result = $this->http_post(self::API_URL_PREFIX.self::CUSTOM_SEESSION_CREATE.'access_token='.$this->access_token,self::json_encode($data));
  2307. if ($result)
  2308. {
  2309. $json = json_decode($result,true);
  2310. if (!$json || !empty($json['errcode'])) {
  2311. $this->errCode = $json['errcode'];
  2312. $this->errMsg = $json['errmsg'];
  2313. return false;
  2314. }
  2315. return $json;
  2316. }
  2317. return false;
  2318. }
  2319. /**
  2320. * 关闭指定多客服会话
  2321. * @tutorial 当用户被其他客服接待时则会失败
  2322. * @param string $openid //用户openid
  2323. * @param string $kf_account //客服账号
  2324. * @param string $text //附加信息,文本会展示在客服人员的多客服客户端,可为空
  2325. * @return boolean | array //成功返回json数组
  2326. * {
  2327. * "errcode": 0,
  2328. * "errmsg": "ok",
  2329. * }
  2330. */
  2331. public function closeKFSession($openid,$kf_account,$text=''){
  2332. $data=array(
  2333. "openid" =>$openid,
  2334. "nickname" => $kf_account
  2335. );
  2336. if ($text) $data["text"] = $text;
  2337. if (!$this->access_token && !$this->checkAuth()) return false;
  2338. $result = $this->http_post(self::API_URL_PREFIX.self::CUSTOM_SEESSION_CLOSE .'access_token='.$this->access_token,self::json_encode($data));
  2339. if ($result)
  2340. {
  2341. $json = json_decode($result,true);
  2342. if (!$json || !empty($json['errcode'])) {
  2343. $this->errCode = $json['errcode'];
  2344. $this->errMsg = $json['errmsg'];
  2345. return false;
  2346. }
  2347. return $json;
  2348. }
  2349. return false;
  2350. }
  2351. /**
  2352. * 获取用户会话状态
  2353. * @param string $openid //用户openid
  2354. * @return boolean | array //成功返回json数组
  2355. * {
  2356. * "errcode" : 0,
  2357. * "errmsg" : "ok",
  2358. * "kf_account" : "test1@test", //正在接待的客服
  2359. * "createtime": 123456789, //会话接入时间
  2360. * }
  2361. */
  2362. public function getKFSession($openid){
  2363. if (!$this->access_token && !$this->checkAuth()) return false;
  2364. $result = $this->http_get(self::API_URL_PREFIX.self::CUSTOM_SEESSION_GET .'access_token='.$this->access_token.'&openid='.$openid);
  2365. if ($result)
  2366. {
  2367. $json = json_decode($result,true);
  2368. if (!$json || !empty($json['errcode'])) {
  2369. $this->errCode = $json['errcode'];
  2370. $this->errMsg = $json['errmsg'];
  2371. return false;
  2372. }
  2373. return $json;
  2374. }
  2375. return false;
  2376. }
  2377. /**
  2378. * 获取指定客服的会话列表
  2379. * @param string $openid //用户openid
  2380. * @return boolean | array //成功返回json数组
  2381. * array(
  2382. * 'sessionlist' => array (
  2383. * array (
  2384. * 'openid'=>'OPENID', //客户 openid
  2385. * 'createtime'=>123456789, //会话创建时间,UNIX 时间戳
  2386. * ),
  2387. * array (
  2388. * 'openid'=>'OPENID', //客户 openid
  2389. * 'createtime'=>123456789, //会话创建时间,UNIX 时间戳
  2390. * ),
  2391. * )
  2392. * )
  2393. */
  2394. public function getKFSessionlist($kf_account){
  2395. if (!$this->access_token && !$this->checkAuth()) return false;
  2396. $result = $this->http_get(self::API_URL_PREFIX.self::CUSTOM_SEESSION_GET_LIST .'access_token='.$this->access_token.'&kf_account='.$kf_account);
  2397. if ($result)
  2398. {
  2399. $json = json_decode($result,true);
  2400. if (!$json || !empty($json['errcode'])) {
  2401. $this->errCode = $json['errcode'];
  2402. $this->errMsg = $json['errmsg'];
  2403. return false;
  2404. }
  2405. return $json;
  2406. }
  2407. return false;
  2408. }
  2409. /**
  2410. * 获取未接入会话列表
  2411. * @param string $openid //用户openid
  2412. * @return boolean | array //成功返回json数组
  2413. * array (
  2414. * 'count' => 150 , //未接入会话数量
  2415. * 'waitcaselist' => array (
  2416. * array (
  2417. * 'openid'=>'OPENID', //客户 openid
  2418. * 'kf_account ' =>'', //指定接待的客服,为空则未指定
  2419. * 'createtime'=>123456789, //会话创建时间,UNIX 时间戳
  2420. * ),
  2421. * array (
  2422. * 'openid'=>'OPENID', //客户 openid
  2423. * 'kf_account ' =>'', //指定接待的客服,为空则未指定
  2424. * 'createtime'=>123456789, //会话创建时间,UNIX 时间戳
  2425. * )
  2426. * )
  2427. * )
  2428. */
  2429. public function getKFSessionWait(){
  2430. if (!$this->access_token && !$this->checkAuth()) return false;
  2431. $result = $this->http_get(self::API_URL_PREFIX.self::CUSTOM_SEESSION_GET_WAIT .'access_token='.$this->access_token);
  2432. if ($result)
  2433. {
  2434. $json = json_decode($result,true);
  2435. if (!$json || !empty($json['errcode'])) {
  2436. $this->errCode = $json['errcode'];
  2437. $this->errMsg = $json['errmsg'];
  2438. return false;
  2439. }
  2440. return $json;
  2441. }
  2442. return false;
  2443. }
  2444. /**
  2445. * 添加客服账号
  2446. *
  2447. * @param string $account //完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符
  2448. * @param string $nickname //客服昵称,最长6个汉字或12个英文字符
  2449. * @param string $password //客服账号明文登录密码,会自动加密
  2450. * @return boolean|array
  2451. * 成功返回结果
  2452. * {
  2453. * "errcode": 0,
  2454. * "errmsg": "ok",
  2455. * }
  2456. */
  2457. public function addKFAccount($account,$nickname,$password){
  2458. $data=array(
  2459. "kf_account" =>$account,
  2460. "nickname" => $nickname,
  2461. "password" => md5($password)
  2462. );
  2463. if (!$this->access_token && !$this->checkAuth()) return false;
  2464. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::CS_KF_ACCOUNT_ADD_URL.'access_token='.$this->access_token,self::json_encode($data));
  2465. if ($result)
  2466. {
  2467. $json = json_decode($result,true);
  2468. if (!$json || !empty($json['errcode'])) {
  2469. $this->errCode = $json['errcode'];
  2470. $this->errMsg = $json['errmsg'];
  2471. return false;
  2472. }
  2473. return $json;
  2474. }
  2475. return false;
  2476. }
  2477. /**
  2478. * 修改客服账号信息
  2479. *
  2480. * @param string $account //完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符
  2481. * @param string $nickname //客服昵称,最长6个汉字或12个英文字符
  2482. * @param string $password //客服账号明文登录密码,会自动加密
  2483. * @return boolean|array
  2484. * 成功返回结果
  2485. * {
  2486. * "errcode": 0,
  2487. * "errmsg": "ok",
  2488. * }
  2489. */
  2490. public function updateKFAccount($account,$nickname,$password){
  2491. $data=array(
  2492. "kf_account" =>$account,
  2493. "nickname" => $nickname,
  2494. "password" => md5($password)
  2495. );
  2496. if (!$this->access_token && !$this->checkAuth()) return false;
  2497. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::CS_KF_ACCOUNT_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  2498. if ($result)
  2499. {
  2500. $json = json_decode($result,true);
  2501. if (!$json || !empty($json['errcode'])) {
  2502. $this->errCode = $json['errcode'];
  2503. $this->errMsg = $json['errmsg'];
  2504. return false;
  2505. }
  2506. return $json;
  2507. }
  2508. return false;
  2509. }
  2510. /**
  2511. * 删除客服账号
  2512. *
  2513. * @param string $account //完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符
  2514. * @return boolean|array
  2515. * 成功返回结果
  2516. * {
  2517. * "errcode": 0,
  2518. * "errmsg": "ok",
  2519. * }
  2520. */
  2521. public function deleteKFAccount($account){
  2522. if (!$this->access_token && !$this->checkAuth()) return false;
  2523. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::CS_KF_ACCOUNT_DEL_URL.'access_token='.$this->access_token.'&kf_account='.$account);
  2524. if ($result)
  2525. {
  2526. $json = json_decode($result,true);
  2527. if (!$json || !empty($json['errcode'])) {
  2528. $this->errCode = $json['errcode'];
  2529. $this->errMsg = $json['errmsg'];
  2530. return false;
  2531. }
  2532. return $json;
  2533. }
  2534. return false;
  2535. }
  2536. /**
  2537. * 上传客服头像
  2538. *
  2539. * @param string $account //完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符
  2540. * @param string $imgfile //头像文件完整路径,如:'D:\user.jpg'。头像文件必须JPG格式,像素建议640*640
  2541. * @return boolean|array
  2542. * 成功返回结果
  2543. * {
  2544. * "errcode": 0,
  2545. * "errmsg": "ok",
  2546. * }
  2547. */
  2548. public function setKFHeadImg($account,$imgfile){
  2549. if (!$this->access_token && !$this->checkAuth()) return false;
  2550. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::CS_KF_ACCOUNT_UPLOAD_HEADIMG_URL.'access_token='.$this->access_token.'&kf_account='.$account,array('media'=>'@'.$imgfile),true);
  2551. if ($result)
  2552. {
  2553. $json = json_decode($result,true);
  2554. if (!$json || !empty($json['errcode'])) {
  2555. $this->errCode = $json['errcode'];
  2556. $this->errMsg = $json['errmsg'];
  2557. return false;
  2558. }
  2559. return $json;
  2560. }
  2561. return false;
  2562. }
  2563. /**
  2564. * 语义理解接口
  2565. * @param String $uid 用户唯一id(非开发者id),用户区分公众号下的不同用户(建议填入用户openid)
  2566. * @param String $query 输入文本串
  2567. * @param String $category 需要使用的服务类型,多个用“,”隔开,不能为空
  2568. * @param Float $latitude 纬度坐标,与经度同时传入;与城市二选一传入
  2569. * @param Float $longitude 经度坐标,与纬度同时传入;与城市二选一传入
  2570. * @param String $city 城市名称,与经纬度二选一传入
  2571. * @param String $region 区域名称,在城市存在的情况下可省略;与经纬度二选一传入
  2572. * @return boolean|array
  2573. */
  2574. public function querySemantic($uid,$query,$category,$latitude=0,$longitude=0,$city="",$region=""){
  2575. if (!$this->access_token && !$this->checkAuth()) return false;
  2576. $data=array(
  2577. 'query' => $query,
  2578. 'category' => $category,
  2579. 'appid' => $this->appid,
  2580. 'uid' => ''
  2581. );
  2582. //地理坐标或城市名称二选一
  2583. if ($latitude) {
  2584. $data['latitude'] = $latitude;
  2585. $data['longitude'] = $longitude;
  2586. } elseif ($city) {
  2587. $data['city'] = $city;
  2588. } elseif ($region) {
  2589. $data['region'] = $region;
  2590. }
  2591. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::SEMANTIC_API_URL.'access_token='.$this->access_token,self::json_encode($data));
  2592. if ($result)
  2593. {
  2594. $json = json_decode($result,true);
  2595. if (!$json || !empty($json['errcode'])) {
  2596. $this->errCode = $json['errcode'];
  2597. $this->errMsg = $json['errmsg'];
  2598. return false;
  2599. }
  2600. return $json;
  2601. }
  2602. return false;
  2603. }
  2604. /**
  2605. * 创建卡券
  2606. * @param Array $data 卡券数据
  2607. * @return array|boolean 返回数组中card_id为卡券ID
  2608. */
  2609. public function createCard($data) {
  2610. if (!$this->access_token && !$this->checkAuth()) return false;
  2611. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CREATE . 'access_token=' . $this->access_token, self::json_encode($data));
  2612. if ($result) {
  2613. $json = json_decode($result, true);
  2614. if (!$json || !empty($json['errcode'])) {
  2615. $this->errCode = $json['errcode'];
  2616. $this->errMsg = $json['errmsg'];
  2617. return false;
  2618. }
  2619. return $json;
  2620. }
  2621. return false;
  2622. }
  2623. /**
  2624. * 更改卡券信息
  2625. * 调用该接口更新信息后会重新送审,卡券状态变更为待审核。已被用户领取的卡券会实时更新票面信息。
  2626. * @param string $data
  2627. * @return boolean
  2628. */
  2629. public function updateCard($data) {
  2630. if (!$this->access_token && !$this->checkAuth()) return false;
  2631. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  2632. if ($result) {
  2633. $json = json_decode($result, true);
  2634. if (!$json || !empty($json['errcode'])) {
  2635. $this->errCode = $json['errcode'];
  2636. $this->errMsg = $json['errmsg'];
  2637. return false;
  2638. }
  2639. return true;
  2640. }
  2641. return false;
  2642. }
  2643. /**
  2644. * 删除卡券
  2645. * 允许商户删除任意一类卡券。删除卡券后,该卡券对应已生成的领取用二维码、添加到卡包 JS API 均会失效。
  2646. * 注意:删除卡券不能删除已被用户领取,保存在微信客户端中的卡券,已领取的卡券依旧有效。
  2647. * @param string $card_id 卡券ID
  2648. * @return boolean
  2649. */
  2650. public function delCard($card_id) {
  2651. $data = array(
  2652. 'card_id' => $card_id,
  2653. );
  2654. if (!$this->access_token && !$this->checkAuth()) return false;
  2655. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_DELETE . 'access_token=' . $this->access_token, self::json_encode($data));
  2656. if ($result) {
  2657. $json = json_decode($result, true);
  2658. if (!$json || !empty($json['errcode'])) {
  2659. $this->errCode = $json['errcode'];
  2660. $this->errMsg = $json['errmsg'];
  2661. return false;
  2662. }
  2663. return true;
  2664. }
  2665. return false;
  2666. }
  2667. /**
  2668. * 查询卡券详情
  2669. * @param string $card_id
  2670. * @return boolean|array 返回数组信息比较复杂,请参看卡券接口文档
  2671. */
  2672. public function getCardInfo($card_id) {
  2673. $data = array(
  2674. 'card_id' => $card_id,
  2675. );
  2676. if (!$this->access_token && !$this->checkAuth()) return false;
  2677. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_GET . 'access_token=' . $this->access_token, self::json_encode($data));
  2678. if ($result) {
  2679. $json = json_decode($result, true);
  2680. if (!$json || !empty($json['errcode'])) {
  2681. $this->errCode = $json['errcode'];
  2682. $this->errMsg = $json['errmsg'];
  2683. return false;
  2684. }
  2685. return $json;
  2686. }
  2687. return false;
  2688. }
  2689. /**
  2690. * 获取颜色列表
  2691. * 获得卡券的最新颜色列表,用于创建卡券
  2692. * @return boolean|array 返回数组请参看 微信卡券接口文档 的json格式
  2693. */
  2694. public function getCardColors() {
  2695. if (!$this->access_token && !$this->checkAuth()) return false;
  2696. $result = $this->http_get(self::API_BASE_URL_PREFIX . self::CARD_GETCOLORS . 'access_token=' . $this->access_token);
  2697. if ($result) {
  2698. $json = json_decode($result, true);
  2699. if (!$json || !empty($json['errcode'])) {
  2700. $this->errCode = $json['errcode'];
  2701. $this->errMsg = $json['errmsg'];
  2702. return false;
  2703. }
  2704. return $json;
  2705. }
  2706. return false;
  2707. }
  2708. /**
  2709. * 拉取门店列表
  2710. * 获取在公众平台上申请创建的门店列表
  2711. * @param int $offset 开始拉取的偏移,默认为0从头开始
  2712. * @param int $count 拉取的数量,默认为0拉取全部
  2713. * @return boolean|array 返回数组请参看 微信卡券接口文档 的json格式
  2714. */
  2715. public function getCardLocations($offset=0,$count=0) {
  2716. $data=array(
  2717. 'offset'=>$offset,
  2718. 'count'=>$count
  2719. );
  2720. if (!$this->access_token && !$this->checkAuth()) return false;
  2721. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_LOCATION_BATCHGET . 'access_token=' . $this->access_token, self::json_encode($data));
  2722. if ($result) {
  2723. $json = json_decode($result, true);
  2724. if (!$json || !empty($json['errcode'])) {
  2725. $this->errCode = $json['errcode'];
  2726. $this->errMsg = $json['errmsg'];
  2727. return false;
  2728. }
  2729. return $json;
  2730. }
  2731. return false;
  2732. }
  2733. /**
  2734. * 批量导入门店信息
  2735. * @tutorial 返回插入的门店id列表,以逗号分隔。如果有插入失败的,则为-1,请自行核查是哪个插入失败
  2736. * @param array $data 数组形式的json数据,由于内容较多,具体内容格式请查看 微信卡券接口文档
  2737. * @return boolean|string 成功返回插入的门店id列表
  2738. */
  2739. public function addCardLocations($data) {
  2740. if (!$this->access_token && !$this->checkAuth()) return false;
  2741. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_LOCATION_BATCHADD . 'access_token=' . $this->access_token, self::json_encode($data));
  2742. if ($result) {
  2743. $json = json_decode($result, true);
  2744. if (!$json || !empty($json['errcode'])) {
  2745. $this->errCode = $json['errcode'];
  2746. $this->errMsg = $json['errmsg'];
  2747. return false;
  2748. }
  2749. return $json;
  2750. }
  2751. return false;
  2752. }
  2753. /**
  2754. * 生成卡券二维码
  2755. * 成功则直接返回ticket值,可以用 getQRUrl($ticket) 换取二维码url
  2756. *
  2757. * @param string $cardid 卡券ID 必须
  2758. * @param string $code 指定卡券 code 码,只能被领一次。use_custom_code 字段为 true 的卡券必须填写,非自定义 code 不必填写。
  2759. * @param string $openid 指定领取者的 openid,只有该用户能领取。bind_openid 字段为 true 的卡券必须填写,非自定义 openid 不必填写。
  2760. * @param int $expire_seconds 指定二维码的有效时间,范围是 60 ~ 1800 秒。不填默认为永久有效。
  2761. * @param boolean $is_unique_code 指定下发二维码,生成的二维码随机分配一个 code,领取后不可再次扫描。填写 true 或 false。默认 false。
  2762. * @param string $balance 红包余额,以分为单位。红包类型必填(LUCKY_MONEY),其他卡券类型不填。
  2763. * @return boolean|string
  2764. */
  2765. public function createCardQrcode($card_id,$code='',$openid='',$expire_seconds=0,$is_unique_code=false,$balance='') {
  2766. $card = array(
  2767. 'card_id' => $card_id
  2768. );
  2769. if ($code)
  2770. $card['code'] = $code;
  2771. if ($openid)
  2772. $card['openid'] = $openid;
  2773. if ($expire_seconds)
  2774. $card['expire_seconds'] = $expire_seconds;
  2775. if ($is_unique_code)
  2776. $card['is_unique_code'] = $is_unique_code;
  2777. if ($balance)
  2778. $card['balance'] = $balance;
  2779. $data = array(
  2780. 'action_name' => "QR_CARD",
  2781. 'action_info' => array('card' => $card)
  2782. );
  2783. if (!$this->access_token && !$this->checkAuth()) return false;
  2784. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_QRCODE_CREATE . 'access_token=' . $this->access_token, self::json_encode($data));
  2785. if ($result) {
  2786. $json = json_decode($result, true);
  2787. if (!$json || !empty($json['errcode'])) {
  2788. $this->errCode = $json['errcode'];
  2789. $this->errMsg = $json['errmsg'];
  2790. return false;
  2791. }
  2792. return $json;
  2793. }
  2794. return false;
  2795. }
  2796. /**
  2797. * 消耗 code
  2798. * 自定义 code(use_custom_code 为 true)的优惠券,在 code 被核销时,必须调用此接口。
  2799. *
  2800. * @param string $code 要消耗的序列号
  2801. * @param string $code_id 要消耗序列号所述的 card_id,创建卡券时use_custom_code 填写 true 时必填。
  2802. * @return boolean|array
  2803. * {
  2804. * "errcode":0,
  2805. * "errmsg":"ok",
  2806. * "card":{"card_id":"pFS7Fjg8kV1IdDz01r4SQwMkuCKc"},
  2807. * "openid":"oFS7Fjl0WsZ9AMZqrI80nbIq8xrA"
  2808. * }
  2809. */
  2810. public function consumeCardCode($code,$card_id='') {
  2811. $data = array('code' => $code);
  2812. if ($card_id)
  2813. $data['card_id'] = $card_id;
  2814. if (!$this->access_token && !$this->checkAuth()) return false;
  2815. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_CONSUME . 'access_token=' . $this->access_token, self::json_encode($data));
  2816. if ($result) {
  2817. $json = json_decode($result, true);
  2818. if (!$json || !empty($json['errcode'])) {
  2819. $this->errCode = $json['errcode'];
  2820. $this->errMsg = $json['errmsg'];
  2821. return false;
  2822. }
  2823. return $json;
  2824. }
  2825. return false;
  2826. }
  2827. /**
  2828. * code 解码
  2829. * @param string $encrypt_code 通过 choose_card_info 获取的加密字符串
  2830. * @return boolean|array
  2831. * {
  2832. * "errcode":0,
  2833. * "errmsg":"ok",
  2834. * "code":"751234212312"
  2835. * }
  2836. */
  2837. public function decryptCardCode($encrypt_code) {
  2838. $data = array(
  2839. 'encrypt_code' => $encrypt_code,
  2840. );
  2841. if (!$this->access_token && !$this->checkAuth()) return false;
  2842. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_DECRYPT . 'access_token=' . $this->access_token, self::json_encode($data));
  2843. if ($result) {
  2844. $json = json_decode($result, true);
  2845. if (!$json || !empty($json['errcode'])) {
  2846. $this->errCode = $json['errcode'];
  2847. $this->errMsg = $json['errmsg'];
  2848. return false;
  2849. }
  2850. return $json;
  2851. }
  2852. return false;
  2853. }
  2854. /**
  2855. * 查询 code 的有效性(非自定义 code)
  2856. * @param string $code
  2857. * @return boolean|array
  2858. * {
  2859. * "errcode":0,
  2860. * "errmsg":"ok",
  2861. * "openid":"oFS7Fjl0WsZ9AMZqrI80nbIq8xrA", //用户 openid
  2862. * "card":{
  2863. * "card_id":"pFS7Fjg8kV1IdDz01r4SQwMkuCKc",
  2864. * "begin_time": 1404205036, //起始使用时间
  2865. * "end_time": 1404205036, //结束时间
  2866. * }
  2867. * }
  2868. */
  2869. public function checkCardCode($code) {
  2870. $data = array(
  2871. 'code' => $code,
  2872. );
  2873. if (!$this->access_token && !$this->checkAuth()) return false;
  2874. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_GET . 'access_token=' . $this->access_token, self::json_encode($data));
  2875. if ($result) {
  2876. $json = json_decode($result, true);
  2877. if (!$json || !empty($json['errcode'])) {
  2878. $this->errCode = $json['errcode'];
  2879. $this->errMsg = $json['errmsg'];
  2880. return false;
  2881. }
  2882. return $json;
  2883. }
  2884. return false;
  2885. }
  2886. /**
  2887. * 批量查询卡列表
  2888. * @param $offset 开始拉取的偏移,默认为0从头开始
  2889. * @param $count 需要查询的卡片的数量(数量最大50,默认50)
  2890. * @return boolean|array
  2891. * {
  2892. * "errcode":0,
  2893. * "errmsg":"ok",
  2894. * "card_id_list":["ph_gmt7cUVrlRk8swPwx7aDyF-pg"], //卡 id 列表
  2895. * "total_num":1 //该商户名下 card_id 总数
  2896. * }
  2897. */
  2898. public function getCardIdList($offset=0,$count=50) {
  2899. if ($count>50)
  2900. $count = 50;
  2901. $data = array(
  2902. 'offset' => $offset,
  2903. 'count' => $count,
  2904. );
  2905. if (!$this->access_token && !$this->checkAuth()) return false;
  2906. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_BATCHGET . 'access_token=' . $this->access_token, self::json_encode($data));
  2907. if ($result) {
  2908. $json = json_decode($result, true);
  2909. if (!$json || !empty($json['errcode'])) {
  2910. $this->errCode = $json['errcode'];
  2911. $this->errMsg = $json['errmsg'];
  2912. return false;
  2913. }
  2914. return $json;
  2915. }
  2916. return false;
  2917. }
  2918. /**
  2919. * 更改 code
  2920. * 为确保转赠后的安全性,微信允许自定义code的商户对已下发的code进行更改。
  2921. * 注:为避免用户疑惑,建议仅在发生转赠行为后(发生转赠后,微信会通过事件推送的方式告知商户被转赠的卡券code)对用户的code进行更改。
  2922. * @param string $code 卡券的 code 编码
  2923. * @param string $code_id 卡券 ID
  2924. * @param string $new_code 新的卡券 code 编码
  2925. * @return boolean
  2926. */
  2927. public function updateCardCode($code,$code_id,$new_code) {
  2928. $data = array(
  2929. 'code' => $card,
  2930. 'card_id' => $card_id,
  2931. 'new_code' => $new_code,
  2932. );
  2933. if (!$this->access_token && !$this->checkAuth()) return false;
  2934. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  2935. if ($result) {
  2936. $json = json_decode($result, true);
  2937. if (!$json || !empty($json['errcode'])) {
  2938. $this->errCode = $json['errcode'];
  2939. $this->errMsg = $json['errmsg'];
  2940. return false;
  2941. }
  2942. return true;
  2943. }
  2944. return false;
  2945. }
  2946. /**
  2947. * 设置卡券失效
  2948. * 设置卡券失效的操作不可逆
  2949. * @param string $code 需要设置为失效的 code
  2950. * @param string $code 自定义 code 的卡券必填。非自定义 code 的卡券不填。
  2951. * @return boolean
  2952. */
  2953. public function unavailableCardCode($code,$code_id='') {
  2954. $data = array(
  2955. 'code' => $code,
  2956. );
  2957. if ($code_id)
  2958. $data['code_id'] = $code_id;
  2959. if (!$this->access_token && !$this->checkAuth()) return false;
  2960. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_UNAVAILABLE . 'access_token=' . $this->access_token, self::json_encode($data));
  2961. if ($result) {
  2962. $json = json_decode($result, true);
  2963. if (!$json || !empty($json['errcode'])) {
  2964. $this->errCode = $json['errcode'];
  2965. $this->errMsg = $json['errmsg'];
  2966. return false;
  2967. }
  2968. return true;
  2969. }
  2970. return false;
  2971. }
  2972. /**
  2973. * 库存修改
  2974. * @param string $data
  2975. * @return boolean
  2976. */
  2977. public function modifyCardStock($data) {
  2978. if (!$this->access_token && !$this->checkAuth()) return false;
  2979. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_MODIFY_STOCK . 'access_token=' . $this->access_token, self::json_encode($data));
  2980. if ($result) {
  2981. $json = json_decode($result, true);
  2982. if (!$json || !empty($json['errcode'])) {
  2983. $this->errCode = $json['errcode'];
  2984. $this->errMsg = $json['errmsg'];
  2985. return false;
  2986. }
  2987. return true;
  2988. }
  2989. return false;
  2990. }
  2991. /**
  2992. * 激活/绑定会员卡
  2993. * @param string $data 具体结构请参看卡券开发文档(6.1.1 激活/绑定会员卡)章节
  2994. * @return boolean
  2995. */
  2996. public function activateMemberCard($data) {
  2997. if (!$this->access_token && !$this->checkAuth()) return false;
  2998. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_MEMBERCARD_ACTIVATE . 'access_token=' . $this->access_token, self::json_encode($data));
  2999. if ($result) {
  3000. $json = json_decode($result, true);
  3001. if (!$json || !empty($json['errcode'])) {
  3002. $this->errCode = $json['errcode'];
  3003. $this->errMsg = $json['errmsg'];
  3004. return false;
  3005. }
  3006. return true;
  3007. }
  3008. return false;
  3009. }
  3010. /**
  3011. * 会员卡交易
  3012. * 会员卡交易后每次积分及余额变更需通过接口通知微信,便于后续消息通知及其他扩展功能。
  3013. * @param string $data 具体结构请参看卡券开发文档(6.1.2 会员卡交易)章节
  3014. * @return boolean|array
  3015. */
  3016. public function updateMemberCard($data) {
  3017. if (!$this->access_token && !$this->checkAuth()) return false;
  3018. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_MEMBERCARD_UPDATEUSER . 'access_token=' . $this->access_token, self::json_encode($data));
  3019. if ($result) {
  3020. $json = json_decode($result, true);
  3021. if (!$json || !empty($json['errcode'])) {
  3022. $this->errCode = $json['errcode'];
  3023. $this->errMsg = $json['errmsg'];
  3024. return false;
  3025. }
  3026. return $json;
  3027. }
  3028. return false;
  3029. }
  3030. /**
  3031. * 更新红包金额
  3032. * @param string $code 红包的序列号
  3033. * @param $balance 红包余额
  3034. * @param string $card_id 自定义 code 的卡券必填。非自定义 code 可不填。
  3035. * @return boolean|array
  3036. */
  3037. public function updateLuckyMoney($code,$balance,$card_id='') {
  3038. $data = array(
  3039. 'code' => $code,
  3040. 'balance' => $balance
  3041. );
  3042. if ($card_id)
  3043. $data['card_id'] = $card_id;
  3044. if (!$this->access_token && !$this->checkAuth()) return false;
  3045. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_LUCKYMONEY_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3046. if ($result) {
  3047. $json = json_decode($result, true);
  3048. if (!$json || !empty($json['errcode'])) {
  3049. $this->errCode = $json['errcode'];
  3050. $this->errMsg = $json['errmsg'];
  3051. return false;
  3052. }
  3053. return true;
  3054. }
  3055. return false;
  3056. }
  3057. /**
  3058. * 设置卡券测试白名单
  3059. * @param string $openid 测试的 openid 列表
  3060. * @param string $user 测试的微信号列表
  3061. * @return boolean
  3062. */
  3063. public function setCardTestWhiteList($openid=array(),$user=array()) {
  3064. $data = array();
  3065. if (count($openid) > 0)
  3066. $data['openid'] = $openid;
  3067. if (count($user) > 0)
  3068. $data['username'] = $user;
  3069. if (!$this->access_token && !$this->checkAuth()) return false;
  3070. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_TESTWHILELIST_SET . 'access_token=' . $this->access_token, self::json_encode($data));
  3071. if ($result) {
  3072. $json = json_decode($result, true);
  3073. if (!$json || !empty($json['errcode'])) {
  3074. $this->errCode = $json['errcode'];
  3075. $this->errMsg = $json['errmsg'];
  3076. return false;
  3077. }
  3078. return true;
  3079. }
  3080. return false;
  3081. }
  3082. }
  3083. /**
  3084. * PKCS7Encoder class
  3085. *
  3086. * 提供基于PKCS7算法的加解密接口.
  3087. */
  3088. class PKCS7Encoder
  3089. {
  3090. public static $block_size = 32;
  3091. /**
  3092. * 对需要加密的明文进行填充补位
  3093. * @param $text 需要进行填充补位操作的明文
  3094. * @return 补齐明文字符串
  3095. */
  3096. function encode($text)
  3097. {
  3098. $block_size = PKCS7Encoder::$block_size;
  3099. $text_length = strlen($text);
  3100. //计算需要填充的位数
  3101. $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
  3102. if ($amount_to_pad == 0) {
  3103. $amount_to_pad = PKCS7Encoder::block_size;
  3104. }
  3105. //获得补位所用的字符
  3106. $pad_chr = chr($amount_to_pad);
  3107. $tmp = "";
  3108. for ($index = 0; $index < $amount_to_pad; $index++) {
  3109. $tmp .= $pad_chr;
  3110. }
  3111. return $text . $tmp;
  3112. }
  3113. /**
  3114. * 对解密后的明文进行补位删除
  3115. * @param decrypted 解密后的明文
  3116. * @return 删除填充补位后的明文
  3117. */
  3118. function decode($text)
  3119. {
  3120. $pad = ord(substr($text, -1));
  3121. if ($pad < 1 || $pad > PKCS7Encoder::$block_size) {
  3122. $pad = 0;
  3123. }
  3124. return substr($text, 0, (strlen($text) - $pad));
  3125. }
  3126. }
  3127. /**
  3128. * Prpcrypt class
  3129. *
  3130. * 提供接收和推送给公众平台消息的加解密接口.
  3131. */
  3132. class Prpcrypt
  3133. {
  3134. public $key;
  3135. function Prpcrypt($k)
  3136. {
  3137. $this->key = base64_decode($k . "=");
  3138. }
  3139. /**
  3140. * 对明文进行加密
  3141. * @param string $text 需要加密的明文
  3142. * @return string 加密后的密文
  3143. */
  3144. public function encrypt($text, $appid)
  3145. {
  3146. try {
  3147. //获得16位随机字符串,填充到明文之前
  3148. $random = $this->getRandomStr();//"aaaabbbbccccdddd";
  3149. $text = $random . pack("N", strlen($text)) . $text . $appid;
  3150. // 网络字节序
  3151. $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  3152. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  3153. $iv = substr($this->key, 0, 16);
  3154. //使用自定义的填充方式对明文进行补位填充
  3155. $pkc_encoder = new PKCS7Encoder;
  3156. $text = $pkc_encoder->encode($text);
  3157. mcrypt_generic_init($module, $this->key, $iv);
  3158. //加密
  3159. $encrypted = mcrypt_generic($module, $text);
  3160. mcrypt_generic_deinit($module);
  3161. mcrypt_module_close($module);
  3162. // print(base64_encode($encrypted));
  3163. //使用BASE64对加密后的字符串进行编码
  3164. return array(ErrorCode::$OK, base64_encode($encrypted));
  3165. } catch (Exception $e) {
  3166. //print $e;
  3167. return array(ErrorCode::$EncryptAESError, null);
  3168. }
  3169. }
  3170. /**
  3171. * 对密文进行解密
  3172. * @param string $encrypted 需要解密的密文
  3173. * @return string 解密得到的明文
  3174. */
  3175. public function decrypt($encrypted, $appid)
  3176. {
  3177. try {
  3178. //使用BASE64对需要解密的字符串进行解码
  3179. $ciphertext_dec = base64_decode($encrypted);
  3180. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  3181. $iv = substr($this->key, 0, 16);
  3182. mcrypt_generic_init($module, $this->key, $iv);
  3183. //解密
  3184. $decrypted = mdecrypt_generic($module, $ciphertext_dec);
  3185. mcrypt_generic_deinit($module);
  3186. mcrypt_module_close($module);
  3187. } catch (Exception $e) {
  3188. return array(ErrorCode::$DecryptAESError, null);
  3189. }
  3190. try {
  3191. //去除补位字符
  3192. $pkc_encoder = new PKCS7Encoder;
  3193. $result = $pkc_encoder->decode($decrypted);
  3194. //去除16位随机字符串,网络字节序和AppId
  3195. if (strlen($result) < 16)
  3196. return "";
  3197. $content = substr($result, 16, strlen($result));
  3198. $len_list = unpack("N", substr($content, 0, 4));
  3199. $xml_len = $len_list[1];
  3200. $xml_content = substr($content, 4, $xml_len);
  3201. $from_appid = substr($content, $xml_len + 4);
  3202. if (!$appid)
  3203. $appid = $from_appid;
  3204. //如果传入的appid是空的,则认为是订阅号,使用数据中提取出来的appid
  3205. } catch (Exception $e) {
  3206. //print $e;
  3207. return array(ErrorCode::$IllegalBuffer, null);
  3208. }
  3209. if ($from_appid != $appid)
  3210. return array(ErrorCode::$ValidateAppidError, null);
  3211. //不注释上边两行,避免传入appid是错误的情况
  3212. return array(0, $xml_content, $from_appid); //增加appid,为了解决后面加密回复消息的时候没有appid的订阅号会无法回复
  3213. }
  3214. /**
  3215. * 随机生成16位字符串
  3216. * @return string 生成的字符串
  3217. */
  3218. function getRandomStr()
  3219. {
  3220. $str = "";
  3221. $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  3222. $max = strlen($str_pol) - 1;
  3223. for ($i = 0; $i < 16; $i++) {
  3224. $str .= $str_pol[mt_rand(0, $max)];
  3225. }
  3226. return $str;
  3227. }
  3228. }
  3229. /**
  3230. * error code
  3231. * 仅用作类内部使用,不用于官方API接口的errCode码
  3232. */
  3233. class ErrorCode
  3234. {
  3235. public static $OK = 0;
  3236. public static $ValidateSignatureError = 40001;
  3237. public static $ParseXmlError = 40002;
  3238. public static $ComputeSignatureError = 40003;
  3239. public static $IllegalAesKey = 40004;
  3240. public static $ValidateAppidError = 40005;
  3241. public static $EncryptAESError = 40006;
  3242. public static $DecryptAESError = 40007;
  3243. public static $IllegalBuffer = 40008;
  3244. public static $EncodeBase64Error = 40009;
  3245. public static $DecodeBase64Error = 40010;
  3246. public static $GenReturnXmlError = 40011;
  3247. public static $errCode=array(
  3248. '0' => '处理成功',
  3249. '40001' => '校验签名失败',
  3250. '40002' => '解析xml失败',
  3251. '40003' => '计算签名失败',
  3252. '40004' => '不合法的AESKey',
  3253. '40005' => '校验AppID失败',
  3254. '40006' => 'AES加密失败',
  3255. '40007' => 'AES解密失败',
  3256. '40008' => '公众平台发送的xml不合法',
  3257. '40009' => 'Base64编码失败',
  3258. '40010' => 'Base64解码失败',
  3259. '40011' => '公众帐号生成回包xml失败'
  3260. );
  3261. public static function getErrText($err) {
  3262. if (isset(self::$errCode[$err])) {
  3263. return self::$errCode[$err];
  3264. }else {
  3265. return false;
  3266. };
  3267. }
  3268. }