wechat.class.php 149 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314
  1. <?php
  2. /**
  3. * 微信公众平台PHP-SDK, 官方API部分
  4. * @author dodge <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. * );
  14. * $weObj = new Wechat($options);
  15. * $weObj->valid();
  16. * $type = $weObj->getRev()->getRevType();
  17. * switch($type) {
  18. * case Wechat::MSGTYPE_TEXT:
  19. * $weObj->text("hello, I'm wechat")->reply();
  20. * exit;
  21. * break;
  22. * case Wechat::MSGTYPE_EVENT:
  23. * ....
  24. * break;
  25. * case Wechat::MSGTYPE_IMAGE:
  26. * ...
  27. * break;
  28. * default:
  29. * $weObj->text("help info")->reply();
  30. * }
  31. *
  32. * //获取菜单操作:
  33. * $menu = $weObj->getMenu();
  34. * //设置菜单
  35. * $newmenu = array(
  36. * "button"=>
  37. * array(
  38. * array('type'=>'click','name'=>'最新消息','key'=>'MENU_KEY_NEWS'),
  39. * array('type'=>'view','name'=>'我要搜索','url'=>'http://www.baidu.com'),
  40. * )
  41. * );
  42. * $result = $weObj->createMenu($newmenu);
  43. */
  44. class Wechat
  45. {
  46. const MSGTYPE_TEXT = 'text';
  47. const MSGTYPE_IMAGE = 'image';
  48. const MSGTYPE_LOCATION = 'location';
  49. const MSGTYPE_LINK = 'link';
  50. const MSGTYPE_EVENT = 'event';
  51. const MSGTYPE_MUSIC = 'music';
  52. const MSGTYPE_NEWS = 'news';
  53. const MSGTYPE_VOICE = 'voice';
  54. const MSGTYPE_VIDEO = 'video';
  55. const EVENT_SUBSCRIBE = 'subscribe'; //订阅
  56. const EVENT_UNSUBSCRIBE = 'unsubscribe'; //取消订阅
  57. const EVENT_SCAN = 'SCAN'; //扫描带参数二维码
  58. const EVENT_LOCATION = 'LOCATION'; //上报地理位置
  59. const EVENT_MENU_VIEW = 'VIEW'; //菜单 - 点击菜单跳转链接
  60. const EVENT_MENU_CLICK = 'CLICK'; //菜单 - 点击菜单拉取消息
  61. const EVENT_MENU_SCAN_PUSH = 'scancode_push'; //菜单 - 扫码推事件(客户端跳URL)
  62. const EVENT_MENU_SCAN_WAITMSG = 'scancode_waitmsg'; //菜单 - 扫码推事件(客户端不跳URL)
  63. const EVENT_MENU_PIC_SYS = 'pic_sysphoto'; //菜单 - 弹出系统拍照发图
  64. const EVENT_MENU_PIC_PHOTO = 'pic_photo_or_album'; //菜单 - 弹出拍照或者相册发图
  65. const EVENT_MENU_PIC_WEIXIN = 'pic_weixin'; //菜单 - 弹出微信相册发图器
  66. const EVENT_MENU_LOCATION = 'location_select'; //菜单 - 弹出地理位置选择器
  67. const EVENT_SEND_MASS = 'MASSSENDJOBFINISH'; //发送结果 - 高级群发完成
  68. const EVENT_SEND_TEMPLATE = 'TEMPLATESENDJOBFINISH';//发送结果 - 模板消息发送结果
  69. const EVENT_KF_SEESION_CREATE = 'kfcreatesession'; //多客服 - 接入会话
  70. const EVENT_KF_SEESION_CLOSE = 'kfclosesession'; //多客服 - 关闭会话
  71. const EVENT_KF_SEESION_SWITCH = 'kfswitchsession'; //多客服 - 转接会话
  72. const EVENT_CARD_PASS = 'card_pass_check'; //卡券 - 审核通过
  73. const EVENT_CARD_NOTPASS = 'card_not_pass_check'; //卡券 - 审核未通过
  74. const EVENT_CARD_USER_GET = 'user_get_card'; //卡券 - 用户领取卡券
  75. const EVENT_CARD_USER_DEL = 'user_del_card'; //卡券 - 用户删除卡券
  76. const API_URL_PREFIX = 'https://api.weixin.qq.com/cgi-bin';
  77. const AUTH_URL = '/token?grant_type=client_credential&';
  78. const MENU_CREATE_URL = '/menu/create?';
  79. const MENU_GET_URL = '/menu/get?';
  80. const MENU_DELETE_URL = '/menu/delete?';
  81. const GET_TICKET_URL = '/ticket/getticket?';
  82. const CALLBACKSERVER_GET_URL = '/getcallbackip?';
  83. const QRCODE_CREATE_URL='/qrcode/create?';
  84. const QR_SCENE = 0;
  85. const QR_LIMIT_SCENE = 1;
  86. const QRCODE_IMG_URL='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=';
  87. const SHORT_URL='/shorturl?';
  88. const USER_GET_URL='/user/get?';
  89. const USER_INFO_URL='/user/info?';
  90. const USER_UPDATEREMARK_URL='/user/info/updateremark?';
  91. const GROUP_GET_URL='/groups/get?';
  92. const USER_GROUP_URL='/groups/getid?';
  93. const GROUP_CREATE_URL='/groups/create?';
  94. const GROUP_UPDATE_URL='/groups/update?';
  95. const GROUP_MEMBER_UPDATE_URL='/groups/members/update?';
  96. const GROUP_MEMBER_BATCHUPDATE_URL='/groups/members/batchupdate?';
  97. const CUSTOM_SEND_URL='/message/custom/send?';
  98. const MEDIA_UPLOADNEWS_URL = '/media/uploadnews?';
  99. const MASS_SEND_URL = '/message/mass/send?';
  100. const TEMPLATE_SET_INDUSTRY_URL = '/message/template/api_set_industry?';
  101. const TEMPLATE_ADD_TPL_URL = '/message/template/api_add_template?';
  102. const TEMPLATE_SEND_URL = '/message/template/send?';
  103. const MASS_SEND_GROUP_URL = '/message/mass/sendall?';
  104. const MASS_DELETE_URL = '/message/mass/delete?';
  105. const MASS_PREVIEW_URL = '/message/mass/preview?';
  106. const MASS_QUERY_URL = '/message/mass/get?';
  107. const UPLOAD_MEDIA_URL = 'http://file.api.weixin.qq.com/cgi-bin';
  108. const MEDIA_UPLOAD_URL = '/media/upload?';
  109. const MEDIA_GET_URL = '/media/get?';
  110. const MEDIA_VIDEO_UPLOAD = '/media/uploadvideo?';
  111. const MEDIA_FOREVER_UPLOAD_URL = '/material/add_material?';
  112. const MEDIA_FOREVER_NEWS_UPLOAD_URL = '/material/add_news?';
  113. const MEDIA_FOREVER_NEWS_UPDATE_URL = '/material/update_news?';
  114. const MEDIA_FOREVER_GET_URL = '/material/get_material?';
  115. const MEDIA_FOREVER_DEL_URL = '/material/del_material?';
  116. const MEDIA_FOREVER_COUNT_URL = '/material/get_materialcount?';
  117. const MEDIA_FOREVER_BATCHGET_URL = '/material/batchget_material?';
  118. const OAUTH_PREFIX = 'https://open.weixin.qq.com/connect/oauth2';
  119. const OAUTH_AUTHORIZE_URL = '/authorize?';
  120. ///多客服相关地址
  121. const CUSTOM_SERVICE_GET_RECORD = '/customservice/getrecord?';
  122. const CUSTOM_SERVICE_GET_KFLIST = '/customservice/getkflist?';
  123. const CUSTOM_SERVICE_GET_ONLINEKFLIST = '/customservice/getonlinekflist?';
  124. const API_BASE_URL_PREFIX = 'https://api.weixin.qq.com'; //以下API接口URL需要使用此前缀
  125. const OAUTH_TOKEN_URL = '/sns/oauth2/access_token?';
  126. const OAUTH_REFRESH_URL = '/sns/oauth2/refresh_token?';
  127. const OAUTH_USERINFO_URL = '/sns/userinfo?';
  128. const OAUTH_AUTH_URL = '/sns/auth?';
  129. ///多客服相关地址
  130. const CUSTOM_SESSION_CREATE = '/customservice/kfsession/create?';
  131. const CUSTOM_SESSION_CLOSE = '/customservice/kfsession/close?';
  132. const CUSTOM_SESSION_SWITCH = '/customservice/kfsession/switch?';
  133. const CUSTOM_SESSION_GET = '/customservice/kfsession/getsession?';
  134. const CUSTOM_SESSION_GET_LIST = '/customservice/kfsession/getsessionlist?';
  135. const CUSTOM_SESSION_GET_WAIT = '/customservice/kfsession/getwaitcase?';
  136. const CS_KF_ACCOUNT_ADD_URL = '/customservice/kfaccount/add?';
  137. const CS_KF_ACCOUNT_UPDATE_URL = '/customservice/kfaccount/update?';
  138. const CS_KF_ACCOUNT_DEL_URL = '/customservice/kfaccount/del?';
  139. const CS_KF_ACCOUNT_UPLOAD_HEADIMG_URL = '/customservice/kfaccount/uploadheadimg?';
  140. ///卡券相关地址
  141. const CARD_CREATE = '/card/create?';
  142. const CARD_DELETE = '/card/delete?';
  143. const CARD_UPDATE = '/card/update?';
  144. const CARD_GET = '/card/get?';
  145. const CARD_BATCHGET = '/card/batchget?';
  146. const CARD_MODIFY_STOCK = '/card/modifystock?';
  147. const CARD_LOCATION_BATCHADD = '/card/location/batchadd?';
  148. const CARD_LOCATION_BATCHGET = '/card/location/batchget?';
  149. const CARD_GETCOLORS = '/card/getcolors?';
  150. const CARD_QRCODE_CREATE = '/card/qrcode/create?';
  151. const CARD_CODE_CONSUME = '/card/code/consume?';
  152. const CARD_CODE_DECRYPT = '/card/code/decrypt?';
  153. const CARD_CODE_GET = '/card/code/get?';
  154. const CARD_CODE_UPDATE = '/card/code/update?';
  155. const CARD_CODE_UNAVAILABLE = '/card/code/unavailable?';
  156. const CARD_TESTWHILELIST_SET = '/card/testwhitelist/set?';
  157. const CARD_MEMBERCARD_ACTIVATE = '/card/membercard/activate?'; //激活会员卡
  158. const CARD_MEMBERCARD_UPDATEUSER = '/card/membercard/updateuser?'; //更新会员卡
  159. const CARD_MOVIETICKET_UPDATEUSER = '/card/movieticket/updateuser?'; //更新电影票(未加方法)
  160. const CARD_BOARDINGPASS_CHECKIN = '/card/boardingpass/checkin?'; //飞机票-在线选座(未加方法)
  161. const CARD_LUCKYMONEY_UPDATE = '/card/luckymoney/updateuserbalance?'; //更新红包金额
  162. const SEMANTIC_API_URL = '/semantic/semproxy/search?'; //语义理解
  163. ///数据分析接口
  164. static $DATACUBE_URL_ARR = array( //用户分析
  165. 'user' => array(
  166. 'summary' => '/datacube/getusersummary?', //获取用户增减数据(getusersummary)
  167. 'cumulate' => '/datacube/getusercumulate?', //获取累计用户数据(getusercumulate)
  168. ),
  169. 'article' => array( //图文分析
  170. 'summary' => '/datacube/getarticlesummary?', //获取图文群发每日数据(getarticlesummary)
  171. 'total' => '/datacube/getarticletotal?', //获取图文群发总数据(getarticletotal)
  172. 'read' => '/datacube/getuserread?', //获取图文统计数据(getuserread)
  173. 'readhour' => '/datacube/getuserreadhour?', //获取图文统计分时数据(getuserreadhour)
  174. 'share' => '/datacube/getusershare?', //获取图文分享转发数据(getusershare)
  175. 'sharehour' => '/datacube/getusersharehour?', //获取图文分享转发分时数据(getusersharehour)
  176. ),
  177. 'upstreammsg' => array( //消息分析
  178. 'summary' => '/datacube/getupstreammsg?', //获取消息发送概况数据(getupstreammsg)
  179. 'hour' => '/datacube/getupstreammsghour?', //获取消息分送分时数据(getupstreammsghour)
  180. 'week' => '/datacube/getupstreammsgweek?', //获取消息发送周数据(getupstreammsgweek)
  181. 'month' => '/datacube/getupstreammsgmonth?', //获取消息发送月数据(getupstreammsgmonth)
  182. 'dist' => '/datacube/getupstreammsgdist?', //获取消息发送分布数据(getupstreammsgdist)
  183. 'distweek' => '/datacube/getupstreammsgdistweek?', //获取消息发送分布周数据(getupstreammsgdistweek)
  184. 'distmonth' => '/datacube/getupstreammsgdistmonth?', //获取消息发送分布月数据(getupstreammsgdistmonth)
  185. ),
  186. 'interface' => array( //接口分析
  187. 'summary' => '/datacube/getinterfacesummary?', //获取接口分析数据(getinterfacesummary)
  188. 'summaryhour' => '/datacube/getinterfacesummaryhour?', //获取接口分析分时数据(getinterfacesummaryhour)
  189. )
  190. );
  191. ///微信摇一摇周边
  192. const SHAKEAROUND_DEVICE_APPLYID = '/shakearound/device/applyid?';//申请设备ID
  193. const SHAKEAROUND_DEVICE_UPDATE = '/shakearound/device/update?';//编辑设备信息
  194. const SHAKEAROUND_DEVICE_SEARCH = '/shakearound/device/search?';//查询设备列表
  195. const SHAKEAROUND_DEVICE_BINDLOCATION = '/shakearound/device/bindlocation?';//配置设备与门店ID的关系
  196. const SHAKEAROUND_DEVICE_BINDPAGE = '/shakearound/device/bindpage?';//配置设备与页面的绑定关系
  197. const SHAKEAROUND_MATERIAL_ADD = '/shakearound/material/add?';//上传摇一摇图片素材
  198. const SHAKEAROUND_PAGE_ADD = '/shakearound/page/add?';//增加页面
  199. const SHAKEAROUND_PAGE_UPDATE = '/shakearound/page/update?';//编辑页面
  200. const SHAKEAROUND_PAGE_SEARCH = '/shakearound/page/search?';//查询页面列表
  201. const SHAKEAROUND_PAGE_DELETE = '/shakearound/page/delete?';//删除页面
  202. const SHAKEAROUND_USER_GETSHAKEINFO = '/shakearound/user/getshakeinfo?';//获取摇周边的设备及用户信息
  203. const SHAKEAROUND_STATISTICS_DEVICE = '/shakearound/statistics/device?';//以设备为维度的数据统计接口
  204. const SHAKEAROUND_STATISTICS_PAGE = '/shakearound/statistics/page?';//以页面为维度的数据统计接口
  205. private $token;
  206. private $encodingAesKey;
  207. private $encrypt_type;
  208. private $appid;
  209. private $appsecret;
  210. private $access_token;
  211. private $jsapi_ticket;
  212. private $user_token;
  213. private $partnerid;
  214. private $partnerkey;
  215. private $paysignkey;
  216. private $postxml;
  217. private $_msg;
  218. private $_funcflag = false;
  219. private $_receive;
  220. private $_text_filter = true;
  221. public $debug = false;
  222. public $errCode = 40001;
  223. public $errMsg = "no access";
  224. public $logcallback;
  225. public function __construct($options)
  226. {
  227. $this->token = isset($options['token'])?$options['token']:'';
  228. $this->encodingAesKey = isset($options['encodingaeskey'])?$options['encodingaeskey']:'';
  229. $this->appid = isset($options['appid'])?$options['appid']:'';
  230. $this->appsecret = isset($options['appsecret'])?$options['appsecret']:'';
  231. $this->debug = isset($options['debug'])?$options['debug']:false;
  232. $this->logcallback = isset($options['logcallback'])?$options['logcallback']:false;
  233. }
  234. /**
  235. * For weixin server validation
  236. */
  237. private function checkSignature($str='')
  238. {
  239. $signature = isset($_GET["signature"])?$_GET["signature"]:'';
  240. $signature = isset($_GET["msg_signature"])?$_GET["msg_signature"]:$signature; //如果存在加密验证则用加密验证段
  241. $timestamp = isset($_GET["timestamp"])?$_GET["timestamp"]:'';
  242. $nonce = isset($_GET["nonce"])?$_GET["nonce"]:'';
  243. $token = $this->token;
  244. $tmpArr = array($token, $timestamp, $nonce,$str);
  245. sort($tmpArr, SORT_STRING);
  246. $tmpStr = implode( $tmpArr );
  247. $tmpStr = sha1( $tmpStr );
  248. if( $tmpStr == $signature ){
  249. return true;
  250. }else{
  251. return false;
  252. }
  253. }
  254. /**
  255. * For weixin server validation
  256. * @param bool $return 是否返回
  257. */
  258. public function valid($return=false)
  259. {
  260. $encryptStr="";
  261. if ($_SERVER['REQUEST_METHOD'] == "POST") {
  262. $postStr = file_get_contents("php://input");
  263. $array = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  264. $this->encrypt_type = isset($_GET["encrypt_type"]) ? $_GET["encrypt_type"]: '';
  265. if ($this->encrypt_type == 'aes') { //aes加密
  266. $this->log($postStr);
  267. $encryptStr = $array['Encrypt'];
  268. $pc = new Prpcrypt($this->encodingAesKey);
  269. $array = $pc->decrypt($encryptStr,$this->appid);
  270. if (!isset($array[0]) || ($array[0] != 0)) {
  271. if (!$return) {
  272. die('decrypt error!');
  273. } else {
  274. return false;
  275. }
  276. }
  277. $this->postxml = $array[1];
  278. if (!$this->appid)
  279. $this->appid = $array[2];//为了没有appid的订阅号。
  280. } else {
  281. $this->postxml = $postStr;
  282. }
  283. } elseif (isset($_GET["echostr"])) {
  284. $echoStr = $_GET["echostr"];
  285. if ($return) {
  286. if ($this->checkSignature())
  287. return $echoStr;
  288. else
  289. return false;
  290. } else {
  291. if ($this->checkSignature())
  292. die($echoStr);
  293. else
  294. die('no access');
  295. }
  296. }
  297. if (!$this->checkSignature($encryptStr)) {
  298. if ($return)
  299. return false;
  300. else
  301. die('no access');
  302. }
  303. return true;
  304. }
  305. /**
  306. * 设置发送消息
  307. * @param array $msg 消息数组
  308. * @param bool $append 是否在原消息数组追加
  309. */
  310. public function Message($msg = '',$append = false){
  311. if (is_null($msg)) {
  312. $this->_msg =array();
  313. }elseif (is_array($msg)) {
  314. if ($append)
  315. $this->_msg = array_merge($this->_msg,$msg);
  316. else
  317. $this->_msg = $msg;
  318. return $this->_msg;
  319. } else {
  320. return $this->_msg;
  321. }
  322. }
  323. /**
  324. * 设置消息的星标标志,官方已取消对此功能的支持
  325. */
  326. public function setFuncFlag($flag) {
  327. $this->_funcflag = $flag;
  328. return $this;
  329. }
  330. /**
  331. * 日志记录,可被重载。
  332. * @param mixed $log 输入日志
  333. * @return mixed
  334. */
  335. protected function log($log){
  336. if ($this->debug && function_exists($this->logcallback)) {
  337. if (is_array($log)) $log = print_r($log,true);
  338. return call_user_func($this->logcallback,$log);
  339. }
  340. }
  341. /**
  342. * 获取微信服务器发来的信息
  343. */
  344. public function getRev()
  345. {
  346. if ($this->_receive) return $this;
  347. $postStr = !empty($this->postxml)?$this->postxml:file_get_contents("php://input");
  348. //兼顾使用明文又不想调用valid()方法的情况
  349. $this->log($postStr);
  350. if (!empty($postStr)) {
  351. $this->_receive = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  352. }
  353. return $this;
  354. }
  355. /**
  356. * 获取微信服务器发来的信息
  357. */
  358. public function getRevData()
  359. {
  360. return $this->_receive;
  361. }
  362. /**
  363. * 获取消息发送者
  364. */
  365. public function getRevFrom() {
  366. if (isset($this->_receive['FromUserName']))
  367. return $this->_receive['FromUserName'];
  368. else
  369. return false;
  370. }
  371. /**
  372. * 获取消息接受者
  373. */
  374. public function getRevTo() {
  375. if (isset($this->_receive['ToUserName']))
  376. return $this->_receive['ToUserName'];
  377. else
  378. return false;
  379. }
  380. /**
  381. * 获取接收消息的类型
  382. */
  383. public function getRevType() {
  384. if (isset($this->_receive['MsgType']))
  385. return $this->_receive['MsgType'];
  386. else
  387. return false;
  388. }
  389. /**
  390. * 获取消息ID
  391. */
  392. public function getRevID() {
  393. if (isset($this->_receive['MsgId']))
  394. return $this->_receive['MsgId'];
  395. else
  396. return false;
  397. }
  398. /**
  399. * 获取消息发送时间
  400. */
  401. public function getRevCtime() {
  402. if (isset($this->_receive['CreateTime']))
  403. return $this->_receive['CreateTime'];
  404. else
  405. return false;
  406. }
  407. /**
  408. * 获取接收消息内容正文
  409. */
  410. public function getRevContent(){
  411. if (isset($this->_receive['Content']))
  412. return $this->_receive['Content'];
  413. else if (isset($this->_receive['Recognition'])) //获取语音识别文字内容,需申请开通
  414. return $this->_receive['Recognition'];
  415. else
  416. return false;
  417. }
  418. /**
  419. * 获取接收消息图片
  420. */
  421. public function getRevPic(){
  422. if (isset($this->_receive['PicUrl']))
  423. return array(
  424. 'mediaid'=>$this->_receive['MediaId'],
  425. 'picurl'=>(string)$this->_receive['PicUrl'], //防止picurl为空导致解析出错
  426. );
  427. else
  428. return false;
  429. }
  430. /**
  431. * 获取接收消息链接
  432. */
  433. public function getRevLink(){
  434. if (isset($this->_receive['Url'])){
  435. return array(
  436. 'url'=>$this->_receive['Url'],
  437. 'title'=>$this->_receive['Title'],
  438. 'description'=>$this->_receive['Description']
  439. );
  440. } else
  441. return false;
  442. }
  443. /**
  444. * 获取接收地理位置
  445. */
  446. public function getRevGeo(){
  447. if (isset($this->_receive['Location_X'])){
  448. return array(
  449. 'x'=>$this->_receive['Location_X'],
  450. 'y'=>$this->_receive['Location_Y'],
  451. 'scale'=>$this->_receive['Scale'],
  452. 'label'=>$this->_receive['Label']
  453. );
  454. } else
  455. return false;
  456. }
  457. /**
  458. * 获取上报地理位置事件
  459. */
  460. public function getRevEventGeo(){
  461. if (isset($this->_receive['Latitude'])){
  462. return array(
  463. 'x'=>$this->_receive['Latitude'],
  464. 'y'=>$this->_receive['Longitude'],
  465. 'precision'=>$this->_receive['Precision'],
  466. );
  467. } else
  468. return false;
  469. }
  470. /**
  471. * 获取接收事件推送
  472. */
  473. public function getRevEvent(){
  474. if (isset($this->_receive['Event'])){
  475. $array['event'] = $this->_receive['Event'];
  476. }
  477. if (isset($this->_receive['EventKey'])){
  478. $array['key'] = $this->_receive['EventKey'];
  479. }
  480. if (isset($array) && count($array) > 0) {
  481. return $array;
  482. } else {
  483. return false;
  484. }
  485. }
  486. /**
  487. * 获取自定义菜单的扫码推事件信息
  488. *
  489. * 事件类型为以下两种时则调用此方法有效
  490. * Event 事件类型,scancode_push
  491. * Event 事件类型,scancode_waitmsg
  492. *
  493. * @return: array | false
  494. * array (
  495. * 'ScanType'=>'qrcode',
  496. * 'ScanResult'=>'123123'
  497. * )
  498. */
  499. public function getRevScanInfo(){
  500. if (isset($this->_receive['ScanCodeInfo'])){
  501. if (!is_array($this->_receive['ScanCodeInfo'])) {
  502. $array=(array)$this->_receive['ScanCodeInfo'];
  503. $this->_receive['ScanCodeInfo']=$array;
  504. }else {
  505. $array=$this->_receive['ScanCodeInfo'];
  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 事件类型,pic_sysphoto 弹出系统拍照发图的事件推送
  519. * Event 事件类型,pic_photo_or_album 弹出拍照或者相册发图的事件推送
  520. * Event 事件类型,pic_weixin 弹出微信相册发图器的事件推送
  521. *
  522. * @return: array | false
  523. * array (
  524. * 'Count' => '2',
  525. * 'PicList' =>array (
  526. * 'item' =>array (
  527. * 0 =>array ('PicMd5Sum' => 'aaae42617cf2a14342d96005af53624c'),
  528. * 1 =>array ('PicMd5Sum' => '149bd39e296860a2adc2f1bb81616ff8'),
  529. * ),
  530. * ),
  531. * )
  532. *
  533. */
  534. public function getRevSendPicsInfo(){
  535. if (isset($this->_receive['SendPicsInfo'])){
  536. if (!is_array($this->_receive['SendPicsInfo'])) {
  537. $array=(array)$this->_receive['SendPicsInfo'];
  538. if (isset($array['PicList'])){
  539. $array['PicList']=(array)$array['PicList'];
  540. $item=$array['PicList']['item'];
  541. $array['PicList']['item']=array();
  542. foreach ( $item as $key => $value ){
  543. $array['PicList']['item'][$key]=(array)$value;
  544. }
  545. }
  546. $this->_receive['SendPicsInfo']=$array;
  547. } else {
  548. $array=$this->_receive['SendPicsInfo'];
  549. }
  550. }
  551. if (isset($array) && count($array) > 0) {
  552. return $array;
  553. } else {
  554. return false;
  555. }
  556. }
  557. /**
  558. * 获取自定义菜单的地理位置选择器事件推送
  559. *
  560. * 事件类型为以下时则可以调用此方法有效
  561. * Event 事件类型,location_select 弹出地理位置选择器的事件推送
  562. *
  563. * @return: array | false
  564. * array (
  565. * 'Location_X' => '33.731655000061',
  566. * 'Location_Y' => '113.29955200008047',
  567. * 'Scale' => '16',
  568. * 'Label' => '某某市某某区某某路',
  569. * 'Poiname' => '',
  570. * )
  571. *
  572. */
  573. public function getRevSendGeoInfo(){
  574. if (isset($this->_receive['SendLocationInfo'])){
  575. if (!is_array($this->_receive['SendLocationInfo'])) {
  576. $array=(array)$this->_receive['SendLocationInfo'];
  577. if (empty($array['Poiname'])) {
  578. $array['Poiname']="";
  579. }
  580. if (empty($array['Label'])) {
  581. $array['Label']="";
  582. }
  583. $this->_receive['SendLocationInfo']=$array;
  584. } else {
  585. $array=$this->_receive['SendLocationInfo'];
  586. }
  587. }
  588. if (isset($array) && count($array) > 0) {
  589. return $array;
  590. } else {
  591. return false;
  592. }
  593. }
  594. /**
  595. * 获取接收语音推送
  596. */
  597. public function getRevVoice(){
  598. if (isset($this->_receive['MediaId'])){
  599. return array(
  600. 'mediaid'=>$this->_receive['MediaId'],
  601. 'format'=>$this->_receive['Format'],
  602. );
  603. } else
  604. return false;
  605. }
  606. /**
  607. * 获取接收视频推送
  608. */
  609. public function getRevVideo(){
  610. if (isset($this->_receive['MediaId'])){
  611. return array(
  612. 'mediaid'=>$this->_receive['MediaId'],
  613. 'thumbmediaid'=>$this->_receive['ThumbMediaId']
  614. );
  615. } else
  616. return false;
  617. }
  618. /**
  619. * 获取接收TICKET
  620. */
  621. public function getRevTicket(){
  622. if (isset($this->_receive['Ticket'])){
  623. return $this->_receive['Ticket'];
  624. } else
  625. return false;
  626. }
  627. /**
  628. * 获取二维码的场景值
  629. */
  630. public function getRevSceneId (){
  631. if (isset($this->_receive['EventKey'])){
  632. return str_replace('qrscene_','',$this->_receive['EventKey']);
  633. } else{
  634. return false;
  635. }
  636. }
  637. /**
  638. * 获取主动推送的消息ID
  639. * 经过验证,这个和普通的消息MsgId不一样
  640. * 当Event为 MASSSENDJOBFINISH 或 TEMPLATESENDJOBFINISH
  641. */
  642. public function getRevTplMsgID(){
  643. if (isset($this->_receive['MsgID'])){
  644. return $this->_receive['MsgID'];
  645. } else
  646. return false;
  647. }
  648. /**
  649. * 获取模板消息发送状态
  650. */
  651. public function getRevStatus(){
  652. if (isset($this->_receive['Status'])){
  653. return $this->_receive['Status'];
  654. } else
  655. return false;
  656. }
  657. /**
  658. * 获取群发或模板消息发送结果
  659. * 当Event为 MASSSENDJOBFINISH 或 TEMPLATESENDJOBFINISH,即高级群发/模板消息
  660. */
  661. public function getRevResult(){
  662. if (isset($this->_receive['Status'])) //发送是否成功,具体的返回值请参考 高级群发/模板消息 的事件推送说明
  663. $array['Status'] = $this->_receive['Status'];
  664. if (isset($this->_receive['MsgID'])) //发送的消息id
  665. $array['MsgID'] = $this->_receive['MsgID'];
  666. //以下仅当群发消息时才会有的事件内容
  667. if (isset($this->_receive['TotalCount'])) //分组或openid列表内粉丝数量
  668. $array['TotalCount'] = $this->_receive['TotalCount'];
  669. if (isset($this->_receive['FilterCount'])) //过滤(过滤是指特定地区、性别的过滤、用户设置拒收的过滤,用户接收已超4条的过滤)后,准备发送的粉丝数
  670. $array['FilterCount'] = $this->_receive['FilterCount'];
  671. if (isset($this->_receive['SentCount'])) //发送成功的粉丝数
  672. $array['SentCount'] = $this->_receive['SentCount'];
  673. if (isset($this->_receive['ErrorCount'])) //发送失败的粉丝数
  674. $array['ErrorCount'] = $this->_receive['ErrorCount'];
  675. if (isset($array) && count($array) > 0) {
  676. return $array;
  677. } else {
  678. return false;
  679. }
  680. }
  681. /**
  682. * 获取多客服会话状态推送事件 - 接入会话
  683. * 当Event为 kfcreatesession 即接入会话
  684. * @return string | boolean 返回分配到的客服
  685. */
  686. public function getRevKFCreate(){
  687. if (isset($this->_receive['KfAccount'])){
  688. return $this->_receive['KfAccount'];
  689. } else
  690. return false;
  691. }
  692. /**
  693. * 获取多客服会话状态推送事件 - 关闭会话
  694. * 当Event为 kfclosesession 即关闭会话
  695. * @return string | boolean 返回分配到的客服
  696. */
  697. public function getRevKFClose(){
  698. if (isset($this->_receive['KfAccount'])){
  699. return $this->_receive['KfAccount'];
  700. } else
  701. return false;
  702. }
  703. /**
  704. * 获取多客服会话状态推送事件 - 转接会话
  705. * 当Event为 kfswitchsession 即转接会话
  706. * @return array | boolean 返回分配到的客服
  707. * {
  708. * 'FromKfAccount' => '', //原接入客服
  709. * 'ToKfAccount' => '' //转接到客服
  710. * }
  711. */
  712. public function getRevKFSwitch(){
  713. if (isset($this->_receive['FromKfAccount'])) //原接入客服
  714. $array['FromKfAccount'] = $this->_receive['FromKfAccount'];
  715. if (isset($this->_receive['ToKfAccount'])) //转接到客服
  716. $array['ToKfAccount'] = $this->_receive['ToKfAccount'];
  717. if (isset($array) && count($array) > 0) {
  718. return $array;
  719. } else {
  720. return false;
  721. }
  722. }
  723. /**
  724. * 获取卡券事件推送 - 卡卷审核是否通过
  725. * 当Event为 card_pass_check(审核通过) 或 card_not_pass_check(未通过)
  726. * @return string|boolean 返回卡券ID
  727. */
  728. public function getRevCardPass(){
  729. if (isset($this->_receive['CardId']))
  730. return $this->_receive['CardId'];
  731. else
  732. return false;
  733. }
  734. /**
  735. * 获取卡券事件推送 - 领取卡券
  736. * 当Event为 user_get_card(用户领取卡券)
  737. * @return array|boolean
  738. */
  739. public function getRevCardGet(){
  740. if (isset($this->_receive['CardId'])) //卡券 ID
  741. $array['CardId'] = $this->_receive['CardId'];
  742. if (isset($this->_receive['IsGiveByFriend'])) //是否为转赠,1 代表是,0 代表否。
  743. $array['IsGiveByFriend'] = $this->_receive['IsGiveByFriend'];
  744. if (isset($this->_receive['UserCardCode']) && !empty($this->_receive['UserCardCode'])) //code 序列号。自定义 code 及非自定义 code的卡券被领取后都支持事件推送。
  745. $array['UserCardCode'] = $this->_receive['UserCardCode'];
  746. if (isset($array) && count($array) > 0) {
  747. return $array;
  748. } else {
  749. return false;
  750. }
  751. }
  752. /**
  753. * 获取卡券事件推送 - 删除卡券
  754. * 当Event为 user_del_card(用户删除卡券)
  755. * @return array|boolean
  756. */
  757. public function getRevCardDel(){
  758. if (isset($this->_receive['CardId'])) //卡券 ID
  759. $array['CardId'] = $this->_receive['CardId'];
  760. if (isset($this->_receive['UserCardCode']) && !empty($this->_receive['UserCardCode'])) //code 序列号。自定义 code 及非自定义 code的卡券被领取后都支持事件推送。
  761. $array['UserCardCode'] = $this->_receive['UserCardCode'];
  762. if (isset($array) && count($array) > 0) {
  763. return $array;
  764. } else {
  765. return false;
  766. }
  767. }
  768. public static function xmlSafeStr($str)
  769. {
  770. return '<![CDATA['.preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/",'',$str).']]>';
  771. }
  772. /**
  773. * 数据XML编码
  774. * @param mixed $data 数据
  775. * @return string
  776. */
  777. public static function data_to_xml($data) {
  778. $xml = '';
  779. foreach ($data as $key => $val) {
  780. is_numeric($key) && $key = "item id=\"$key\"";
  781. $xml .= "<$key>";
  782. $xml .= ( is_array($val) || is_object($val)) ? self::data_to_xml($val) : self::xmlSafeStr($val);
  783. list($key, ) = explode(' ', $key);
  784. $xml .= "</$key>";
  785. }
  786. return $xml;
  787. }
  788. /**
  789. * XML编码
  790. * @param mixed $data 数据
  791. * @param string $root 根节点名
  792. * @param string $item 数字索引的子节点名
  793. * @param string $attr 根节点属性
  794. * @param string $id 数字索引子节点key转换的属性名
  795. * @param string $encoding 数据编码
  796. * @return string
  797. */
  798. public function xml_encode($data, $root='xml', $item='item', $attr='', $id='id', $encoding='utf-8') {
  799. if(is_array($attr)){
  800. $_attr = array();
  801. foreach ($attr as $key => $value) {
  802. $_attr[] = "{$key}=\"{$value}\"";
  803. }
  804. $attr = implode(' ', $_attr);
  805. }
  806. $attr = trim($attr);
  807. $attr = empty($attr) ? '' : " {$attr}";
  808. $xml = "<{$root}{$attr}>";
  809. $xml .= self::data_to_xml($data, $item, $id);
  810. $xml .= "</{$root}>";
  811. return $xml;
  812. }
  813. /**
  814. * 过滤文字回复\r\n换行符
  815. * @param string $text
  816. * @return string|mixed
  817. */
  818. private function _auto_text_filter($text) {
  819. if (!$this->_text_filter) return $text;
  820. return str_replace("\r\n", "\n", $text);
  821. }
  822. /**
  823. * 设置回复消息
  824. * Example: $obj->text('hello')->reply();
  825. * @param string $text
  826. */
  827. public function text($text='')
  828. {
  829. $FuncFlag = $this->_funcflag ? 1 : 0;
  830. $msg = array(
  831. 'ToUserName' => $this->getRevFrom(),
  832. 'FromUserName'=>$this->getRevTo(),
  833. 'MsgType'=>self::MSGTYPE_TEXT,
  834. 'Content'=>$this->_auto_text_filter($text),
  835. 'CreateTime'=>time(),
  836. 'FuncFlag'=>$FuncFlag
  837. );
  838. $this->Message($msg);
  839. return $this;
  840. }
  841. /**
  842. * 设置回复消息
  843. * Example: $obj->image('media_id')->reply();
  844. * @param string $mediaid
  845. */
  846. public function image($mediaid='')
  847. {
  848. $FuncFlag = $this->_funcflag ? 1 : 0;
  849. $msg = array(
  850. 'ToUserName' => $this->getRevFrom(),
  851. 'FromUserName'=>$this->getRevTo(),
  852. 'MsgType'=>self::MSGTYPE_IMAGE,
  853. 'Image'=>array('MediaId'=>$mediaid),
  854. 'CreateTime'=>time(),
  855. 'FuncFlag'=>$FuncFlag
  856. );
  857. $this->Message($msg);
  858. return $this;
  859. }
  860. /**
  861. * 设置回复消息
  862. * Example: $obj->voice('media_id')->reply();
  863. * @param string $mediaid
  864. */
  865. public function voice($mediaid='')
  866. {
  867. $FuncFlag = $this->_funcflag ? 1 : 0;
  868. $msg = array(
  869. 'ToUserName' => $this->getRevFrom(),
  870. 'FromUserName'=>$this->getRevTo(),
  871. 'MsgType'=>self::MSGTYPE_VOICE,
  872. 'Voice'=>array('MediaId'=>$mediaid),
  873. 'CreateTime'=>time(),
  874. 'FuncFlag'=>$FuncFlag
  875. );
  876. $this->Message($msg);
  877. return $this;
  878. }
  879. /**
  880. * 设置回复消息
  881. * Example: $obj->video('media_id','title','description')->reply();
  882. * @param string $mediaid
  883. */
  884. public function video($mediaid='',$title='',$description='')
  885. {
  886. $FuncFlag = $this->_funcflag ? 1 : 0;
  887. $msg = array(
  888. 'ToUserName' => $this->getRevFrom(),
  889. 'FromUserName'=>$this->getRevTo(),
  890. 'MsgType'=>self::MSGTYPE_VIDEO,
  891. 'Video'=>array(
  892. 'MediaId'=>$mediaid,
  893. 'Title'=>$title,
  894. 'Description'=>$description
  895. ),
  896. 'CreateTime'=>time(),
  897. 'FuncFlag'=>$FuncFlag
  898. );
  899. $this->Message($msg);
  900. return $this;
  901. }
  902. /**
  903. * 设置回复音乐
  904. * @param string $title
  905. * @param string $desc
  906. * @param string $musicurl
  907. * @param string $hgmusicurl
  908. * @param string $thumbmediaid 音乐图片缩略图的媒体id,非必须
  909. */
  910. public function music($title,$desc,$musicurl,$hgmusicurl='',$thumbmediaid='') {
  911. $FuncFlag = $this->_funcflag ? 1 : 0;
  912. $msg = array(
  913. 'ToUserName' => $this->getRevFrom(),
  914. 'FromUserName'=>$this->getRevTo(),
  915. 'CreateTime'=>time(),
  916. 'MsgType'=>self::MSGTYPE_MUSIC,
  917. 'Music'=>array(
  918. 'Title'=>$title,
  919. 'Description'=>$desc,
  920. 'MusicUrl'=>$musicurl,
  921. 'HQMusicUrl'=>$hgmusicurl
  922. ),
  923. 'FuncFlag'=>$FuncFlag
  924. );
  925. if ($thumbmediaid) {
  926. $msg['Music']['ThumbMediaId'] = $thumbmediaid;
  927. }
  928. $this->Message($msg);
  929. return $this;
  930. }
  931. /**
  932. * 设置回复图文
  933. * @param array $newsData
  934. * 数组结构:
  935. * array(
  936. * "0"=>array(
  937. * 'Title'=>'msg title',
  938. * 'Description'=>'summary text',
  939. * 'PicUrl'=>'http://www.domain.com/1.jpg',
  940. * 'Url'=>'http://www.domain.com/1.html'
  941. * ),
  942. * "1"=>....
  943. * )
  944. */
  945. public function news($newsData=array())
  946. {
  947. $FuncFlag = $this->_funcflag ? 1 : 0;
  948. $count = count($newsData);
  949. $msg = array(
  950. 'ToUserName' => $this->getRevFrom(),
  951. 'FromUserName'=>$this->getRevTo(),
  952. 'MsgType'=>self::MSGTYPE_NEWS,
  953. 'CreateTime'=>time(),
  954. 'ArticleCount'=>$count,
  955. 'Articles'=>$newsData,
  956. 'FuncFlag'=>$FuncFlag
  957. );
  958. $this->Message($msg);
  959. return $this;
  960. }
  961. /**
  962. *
  963. * 回复微信服务器, 此函数支持链式操作
  964. * Example: $this->text('msg tips')->reply();
  965. * @param string $msg 要发送的信息, 默认取$this->_msg
  966. * @param bool $return 是否返回信息而不抛出到浏览器 默认:否
  967. */
  968. public function reply($msg=array(),$return = false)
  969. {
  970. if (empty($msg)) {
  971. if (empty($this->_msg)) //防止不先设置回复内容,直接调用reply方法导致异常
  972. return false;
  973. $msg = $this->_msg;
  974. }
  975. $xmldata= $this->xml_encode($msg);
  976. $this->log($xmldata);
  977. if ($this->encrypt_type == 'aes') { //如果来源消息为加密方式
  978. $pc = new Prpcrypt($this->encodingAesKey);
  979. $array = $pc->encrypt($xmldata, $this->appid);
  980. $ret = $array[0];
  981. if ($ret != 0) {
  982. $this->log('encrypt err!');
  983. return false;
  984. }
  985. $timestamp = time();
  986. $nonce = rand(77,999)*rand(605,888)*rand(11,99);
  987. $encrypt = $array[1];
  988. $tmpArr = array($this->token, $timestamp, $nonce,$encrypt);//比普通公众平台多了一个加密的密文
  989. sort($tmpArr, SORT_STRING);
  990. $signature = implode($tmpArr);
  991. $signature = sha1($signature);
  992. $xmldata = $this->generate($encrypt, $signature, $timestamp, $nonce);
  993. $this->log($xmldata);
  994. }
  995. if ($return)
  996. return $xmldata;
  997. else
  998. echo $xmldata;
  999. }
  1000. /**
  1001. * xml格式加密,仅请求为加密方式时再用
  1002. */
  1003. private function generate($encrypt, $signature, $timestamp, $nonce)
  1004. {
  1005. //格式化加密信息
  1006. $format = "<xml>
  1007. <Encrypt><![CDATA[%s]]></Encrypt>
  1008. <MsgSignature><![CDATA[%s]]></MsgSignature>
  1009. <TimeStamp>%s</TimeStamp>
  1010. <Nonce><![CDATA[%s]]></Nonce>
  1011. </xml>";
  1012. return sprintf($format, $encrypt, $signature, $timestamp, $nonce);
  1013. }
  1014. /**
  1015. * GET 请求
  1016. * @param string $url
  1017. */
  1018. private function http_get($url){
  1019. $oCurl = curl_init();
  1020. if(stripos($url,"https://")!==FALSE){
  1021. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  1022. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
  1023. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  1024. }
  1025. curl_setopt($oCurl, CURLOPT_URL, $url);
  1026. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
  1027. $sContent = curl_exec($oCurl);
  1028. $aStatus = curl_getinfo($oCurl);
  1029. curl_close($oCurl);
  1030. if(intval($aStatus["http_code"])==200){
  1031. return $sContent;
  1032. }else{
  1033. return false;
  1034. }
  1035. }
  1036. /**
  1037. * POST 请求
  1038. * @param string $url
  1039. * @param array $param
  1040. * @param boolean $post_file 是否文件上传
  1041. * @return string content
  1042. */
  1043. private function http_post($url,$param,$post_file=false){
  1044. $oCurl = curl_init();
  1045. if(stripos($url,"https://")!==FALSE){
  1046. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  1047. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  1048. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  1049. }
  1050. if (is_string($param) || $post_file) {
  1051. $strPOST = $param;
  1052. } else {
  1053. $aPOST = array();
  1054. foreach($param as $key=>$val){
  1055. $aPOST[] = $key."=".urlencode($val);
  1056. }
  1057. $strPOST = join("&", $aPOST);
  1058. }
  1059. curl_setopt($oCurl, CURLOPT_URL, $url);
  1060. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
  1061. curl_setopt($oCurl, CURLOPT_POST,true);
  1062. curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
  1063. $sContent = curl_exec($oCurl);
  1064. $aStatus = curl_getinfo($oCurl);
  1065. curl_close($oCurl);
  1066. if(intval($aStatus["http_code"])==200){
  1067. return $sContent;
  1068. }else{
  1069. return false;
  1070. }
  1071. }
  1072. /**
  1073. * 设置缓存,按需重载
  1074. * @param string $cachename
  1075. * @param mixed $value
  1076. * @param int $expired
  1077. * @return boolean
  1078. */
  1079. protected function setCache($cachename,$value,$expired){
  1080. //TODO: set cache implementation
  1081. return false;
  1082. }
  1083. /**
  1084. * 获取缓存,按需重载
  1085. * @param string $cachename
  1086. * @return mixed
  1087. */
  1088. protected function getCache($cachename){
  1089. //TODO: get cache implementation
  1090. return false;
  1091. }
  1092. /**
  1093. * 清除缓存,按需重载
  1094. * @param string $cachename
  1095. * @return boolean
  1096. */
  1097. protected function removeCache($cachename){
  1098. //TODO: remove cache implementation
  1099. return false;
  1100. }
  1101. /**
  1102. * 获取access_token
  1103. * @param string $appid 如在类初始化时已提供,则可为空
  1104. * @param string $appsecret 如在类初始化时已提供,则可为空
  1105. * @param string $token 手动指定access_token,非必要情况不建议用
  1106. */
  1107. public function checkAuth($appid='',$appsecret='',$token=''){
  1108. if (!$appid || !$appsecret) {
  1109. $appid = $this->appid;
  1110. $appsecret = $this->appsecret;
  1111. }
  1112. if ($token) { //手动指定token,优先使用
  1113. $this->access_token=$token;
  1114. return $this->access_token;
  1115. }
  1116. $authname = 'wechat_access_token'.$appid;
  1117. if ($rs = $this->getCache($authname)) {
  1118. $this->access_token = $rs;
  1119. return $rs;
  1120. }
  1121. $result = $this->http_get(self::API_URL_PREFIX.self::AUTH_URL.'appid='.$appid.'&secret='.$appsecret);
  1122. if ($result)
  1123. {
  1124. $json = json_decode($result,true);
  1125. if (!$json || isset($json['errcode'])) {
  1126. $this->errCode = $json['errcode'];
  1127. $this->errMsg = $json['errmsg'];
  1128. return false;
  1129. }
  1130. $this->access_token = $json['access_token'];
  1131. $expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
  1132. $this->setCache($authname,$this->access_token,$expire);
  1133. return $this->access_token;
  1134. }
  1135. return false;
  1136. }
  1137. /**
  1138. * 删除验证数据
  1139. * @param string $appid
  1140. */
  1141. public function resetAuth($appid=''){
  1142. if (!$appid) $appid = $this->appid;
  1143. $this->access_token = '';
  1144. $authname = 'wechat_access_token'.$appid;
  1145. $this->removeCache($authname);
  1146. return true;
  1147. }
  1148. /**
  1149. * 删除JSAPI授权TICKET
  1150. * @param string $appid 用于多个appid时使用
  1151. */
  1152. public function resetJsTicket($appid=''){
  1153. if (!$appid) $appid = $this->appid;
  1154. $this->jsapi_ticket = '';
  1155. $authname = 'wechat_jsapi_ticket'.$appid;
  1156. $this->removeCache($authname);
  1157. return true;
  1158. }
  1159. /**
  1160. * 获取JSAPI授权TICKET
  1161. * @param string $appid 用于多个appid时使用,可空
  1162. * @param string $jsapi_ticket 手动指定jsapi_ticket,非必要情况不建议用
  1163. */
  1164. public function getJsTicket($appid='',$jsapi_ticket=''){
  1165. if (!$this->access_token && !$this->checkAuth()) return false;
  1166. if (!$appid) $appid = $this->appid;
  1167. if ($jsapi_ticket) { //手动指定token,优先使用
  1168. $this->jsapi_ticket = $jsapi_ticket;
  1169. return $this->jsapi_ticket;
  1170. }
  1171. $authname = 'wechat_jsapi_ticket'.$appid;
  1172. if ($rs = $this->getCache($authname)) {
  1173. $this->jsapi_ticket = $rs;
  1174. return $rs;
  1175. }
  1176. $result = $this->http_get(self::API_URL_PREFIX.self::GET_TICKET_URL.'access_token='.$this->access_token.'&type=jsapi');
  1177. if ($result)
  1178. {
  1179. $json = json_decode($result,true);
  1180. if (!$json || !empty($json['errcode'])) {
  1181. $this->errCode = $json['errcode'];
  1182. $this->errMsg = $json['errmsg'];
  1183. return false;
  1184. }
  1185. $this->jsapi_ticket = $json['ticket'];
  1186. $expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
  1187. $this->setCache($authname,$this->jsapi_ticket,$expire);
  1188. return $this->jsapi_ticket;
  1189. }
  1190. return false;
  1191. }
  1192. /**
  1193. * 获取JsApi使用签名
  1194. * @param string $url 网页的URL,自动处理#及其后面部分
  1195. * @param string $timestamp 当前时间戳 (为空则自动生成)
  1196. * @param string $noncestr 随机串 (为空则自动生成)
  1197. * @param string $appid 用于多个appid时使用,可空
  1198. * @return array|bool 返回签名字串
  1199. */
  1200. public function getJsSign($url, $timestamp=0, $noncestr='', $appid=''){
  1201. if (!$this->jsapi_ticket && !$this->getJsTicket($appid) || !$url) return false;
  1202. if (!$timestamp)
  1203. $timestamp = time();
  1204. if (!$noncestr)
  1205. $noncestr = $this->generateNonceStr();
  1206. $ret = strpos($url,'#');
  1207. if ($ret)
  1208. $url = substr($url,0,$ret);
  1209. $url = trim($url);
  1210. if (empty($url))
  1211. return false;
  1212. $arrdata = array("timestamp" => $timestamp, "noncestr" => $noncestr, "url" => $url, "jsapi_ticket" => $this->jsapi_ticket);
  1213. $sign = $this->getSignature($arrdata);
  1214. if (!$sign)
  1215. return false;
  1216. $signPackage = array(
  1217. "appid" => $this->appid,
  1218. "noncestr" => $noncestr,
  1219. "timestamp" => $timestamp,
  1220. "url" => $url,
  1221. "signature" => $sign
  1222. );
  1223. return $signPackage;
  1224. }
  1225. /**
  1226. * 微信api不支持中文转义的json结构
  1227. * @param array $arr
  1228. */
  1229. static function json_encode($arr) {
  1230. $parts = array ();
  1231. $is_list = false;
  1232. //Find out if the given array is a numerical array
  1233. $keys = array_keys ( $arr );
  1234. $max_length = count ( $arr ) - 1;
  1235. if (($keys [0] === 0) && ($keys [$max_length] === $max_length )) { //See if the first key is 0 and last key is length - 1
  1236. $is_list = true;
  1237. for($i = 0; $i < count ( $keys ); $i ++) { //See if each key correspondes to its position
  1238. if ($i != $keys [$i]) { //A key fails at position check.
  1239. $is_list = false; //It is an associative array.
  1240. break;
  1241. }
  1242. }
  1243. }
  1244. foreach ( $arr as $key => $value ) {
  1245. if (is_array ( $value )) { //Custom handling for arrays
  1246. if ($is_list)
  1247. $parts [] = self::json_encode ( $value ); /* :RECURSION: */
  1248. else
  1249. $parts [] = '"' . $key . '":' . self::json_encode ( $value ); /* :RECURSION: */
  1250. } else {
  1251. $str = '';
  1252. if (! $is_list)
  1253. $str = '"' . $key . '":';
  1254. //Custom handling for multiple data types
  1255. if (!is_string ( $value ) && is_numeric ( $value ) && $value<2000000000)
  1256. $str .= $value; //Numbers
  1257. elseif ($value === false)
  1258. $str .= 'false'; //The booleans
  1259. elseif ($value === true)
  1260. $str .= 'true';
  1261. else
  1262. $str .= '"' . addslashes ( $value ) . '"'; //All other things
  1263. // :TODO: Is there any more datatype we should be in the lookout for? (Object?)
  1264. $parts [] = $str;
  1265. }
  1266. }
  1267. $json = implode ( ',', $parts );
  1268. if ($is_list)
  1269. return '[' . $json . ']'; //Return numerical JSON
  1270. return '{' . $json . '}'; //Return associative JSON
  1271. }
  1272. /**
  1273. * 获取签名
  1274. * @param array $arrdata 签名数组
  1275. * @param string $method 签名方法
  1276. * @return boolean|string 签名值
  1277. */
  1278. public function getSignature($arrdata,$method="sha1") {
  1279. if (!function_exists($method)) return false;
  1280. ksort($arrdata);
  1281. $paramstring = "";
  1282. foreach($arrdata as $key => $value)
  1283. {
  1284. if(strlen($paramstring) == 0)
  1285. $paramstring .= $key . "=" . $value;
  1286. else
  1287. $paramstring .= "&" . $key . "=" . $value;
  1288. }
  1289. $Sign = $method($paramstring);
  1290. return $Sign;
  1291. }
  1292. /**
  1293. * 生成随机字串
  1294. * @param number $length 长度,默认为16,最长为32字节
  1295. * @return string
  1296. */
  1297. public function generateNonceStr($length=16){
  1298. // 密码字符集,可任意添加你需要的字符
  1299. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  1300. $str = "";
  1301. for($i = 0; $i < $length; $i++)
  1302. {
  1303. $str .= $chars[mt_rand(0, strlen($chars) - 1)];
  1304. }
  1305. return $str;
  1306. }
  1307. /**
  1308. * 获取微信服务器IP地址列表
  1309. * @return array('127.0.0.1','127.0.0.1')
  1310. */
  1311. public function getServerIp(){
  1312. if (!$this->access_token && !$this->checkAuth()) return false;
  1313. $result = $this->http_get(self::API_URL_PREFIX.self::CALLBACKSERVER_GET_URL.'access_token='.$this->access_token);
  1314. if ($result)
  1315. {
  1316. $json = json_decode($result,true);
  1317. if (!$json || isset($json['errcode'])) {
  1318. $this->errCode = $json['errcode'];
  1319. $this->errMsg = $json['errmsg'];
  1320. return false;
  1321. }
  1322. return $json['ip_list'];
  1323. }
  1324. return false;
  1325. }
  1326. /**
  1327. * 创建菜单(认证后的订阅号可用)
  1328. * @param array $data 菜单数组数据
  1329. * example:
  1330. * array (
  1331. * 'button' => array (
  1332. * 0 => array (
  1333. * 'name' => '扫码',
  1334. * 'sub_button' => array (
  1335. * 0 => array (
  1336. * 'type' => 'scancode_waitmsg',
  1337. * 'name' => '扫码带提示',
  1338. * 'key' => 'rselfmenu_0_0',
  1339. * ),
  1340. * 1 => array (
  1341. * 'type' => 'scancode_push',
  1342. * 'name' => '扫码推事件',
  1343. * 'key' => 'rselfmenu_0_1',
  1344. * ),
  1345. * ),
  1346. * ),
  1347. * 1 => array (
  1348. * 'name' => '发图',
  1349. * 'sub_button' => array (
  1350. * 0 => array (
  1351. * 'type' => 'pic_sysphoto',
  1352. * 'name' => '系统拍照发图',
  1353. * 'key' => 'rselfmenu_1_0',
  1354. * ),
  1355. * 1 => array (
  1356. * 'type' => 'pic_photo_or_album',
  1357. * 'name' => '拍照或者相册发图',
  1358. * 'key' => 'rselfmenu_1_1',
  1359. * )
  1360. * ),
  1361. * ),
  1362. * 2 => array (
  1363. * 'type' => 'location_select',
  1364. * 'name' => '发送位置',
  1365. * 'key' => 'rselfmenu_2_0'
  1366. * ),
  1367. * ),
  1368. * )
  1369. * type可以选择为以下几种,其中5-8除了收到菜单事件以外,还会单独收到对应类型的信息。
  1370. * 1、click:点击推事件
  1371. * 2、view:跳转URL
  1372. * 3、scancode_push:扫码推事件
  1373. * 4、scancode_waitmsg:扫码推事件且弹出“消息接收中”提示框
  1374. * 5、pic_sysphoto:弹出系统拍照发图
  1375. * 6、pic_photo_or_album:弹出拍照或者相册发图
  1376. * 7、pic_weixin:弹出微信相册发图器
  1377. * 8、location_select:弹出地理位置选择器
  1378. */
  1379. public function createMenu($data){
  1380. if (!$this->access_token && !$this->checkAuth()) return false;
  1381. $result = $this->http_post(self::API_URL_PREFIX.self::MENU_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1382. if ($result)
  1383. {
  1384. $json = json_decode($result,true);
  1385. if (!$json || !empty($json['errcode'])) {
  1386. $this->errCode = $json['errcode'];
  1387. $this->errMsg = $json['errmsg'];
  1388. return false;
  1389. }
  1390. return true;
  1391. }
  1392. return false;
  1393. }
  1394. /**
  1395. * 获取菜单(认证后的订阅号可用)
  1396. * @return array('menu'=>array(....s))
  1397. */
  1398. public function getMenu(){
  1399. if (!$this->access_token && !$this->checkAuth()) return false;
  1400. $result = $this->http_get(self::API_URL_PREFIX.self::MENU_GET_URL.'access_token='.$this->access_token);
  1401. if ($result)
  1402. {
  1403. $json = json_decode($result,true);
  1404. if (!$json || isset($json['errcode'])) {
  1405. $this->errCode = $json['errcode'];
  1406. $this->errMsg = $json['errmsg'];
  1407. return false;
  1408. }
  1409. return $json;
  1410. }
  1411. return false;
  1412. }
  1413. /**
  1414. * 删除菜单(认证后的订阅号可用)
  1415. * @return boolean
  1416. */
  1417. public function deleteMenu(){
  1418. if (!$this->access_token && !$this->checkAuth()) return false;
  1419. $result = $this->http_get(self::API_URL_PREFIX.self::MENU_DELETE_URL.'access_token='.$this->access_token);
  1420. if ($result)
  1421. {
  1422. $json = json_decode($result,true);
  1423. if (!$json || !empty($json['errcode'])) {
  1424. $this->errCode = $json['errcode'];
  1425. $this->errMsg = $json['errmsg'];
  1426. return false;
  1427. }
  1428. return true;
  1429. }
  1430. return false;
  1431. }
  1432. /**
  1433. * 上传临时素材,有效期为3天(认证后的订阅号可用)
  1434. * 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
  1435. * 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
  1436. * 注意:临时素材的media_id是可复用的!
  1437. * @param array $data {"media":'@Path\filename.jpg'}
  1438. * @param type 类型:图片:image 语音:voice 视频:video 缩略图:thumb
  1439. * @return boolean|array
  1440. */
  1441. public function uploadMedia($data, $type){
  1442. if (!$this->access_token && !$this->checkAuth()) return false;
  1443. //原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
  1444. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOAD_URL.'access_token='.$this->access_token.'&type='.$type,$data,true);
  1445. if ($result)
  1446. {
  1447. $json = json_decode($result,true);
  1448. if (!$json || !empty($json['errcode'])) {
  1449. $this->errCode = $json['errcode'];
  1450. $this->errMsg = $json['errmsg'];
  1451. return false;
  1452. }
  1453. return $json;
  1454. }
  1455. return false;
  1456. }
  1457. /**
  1458. * 获取临时素材(认证后的订阅号可用)
  1459. * @param string $media_id 媒体文件id
  1460. * @param boolean $is_video 是否为视频文件,默认为否
  1461. * @return raw data
  1462. */
  1463. public function getMedia($media_id,$is_video=false){
  1464. if (!$this->access_token && !$this->checkAuth()) return false;
  1465. //原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
  1466. //如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
  1467. $url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
  1468. $result = $this->http_get($url_prefix.self::MEDIA_GET_URL.'access_token='.$this->access_token.'&media_id='.$media_id);
  1469. if ($result)
  1470. {
  1471. if (is_string($result)) {
  1472. $json = json_decode($result,true);
  1473. if (isset($json['errcode'])) {
  1474. $this->errCode = $json['errcode'];
  1475. $this->errMsg = $json['errmsg'];
  1476. return false;
  1477. }
  1478. }
  1479. return $result;
  1480. }
  1481. return false;
  1482. }
  1483. /**
  1484. * 上传永久素材(认证后的订阅号可用)
  1485. * 新增的永久素材也可以在公众平台官网素材管理模块中看到
  1486. * 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
  1487. * 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
  1488. * @param array $data {"media":'@Path\filename.jpg'}
  1489. * @param type 类型:图片:image 语音:voice 视频:video 缩略图:thumb
  1490. * @param boolean $is_video 是否为视频文件,默认为否
  1491. * @param array $video_info 视频信息数组,非视频素材不需要提供 array('title'=>'视频标题','introduction'=>'描述')
  1492. * @return boolean|array
  1493. */
  1494. public function uploadForeverMedia($data, $type,$is_video=false,$video_info=array()){
  1495. if (!$this->access_token && !$this->checkAuth()) return false;
  1496. //#TODO 暂不确定此接口是否需要让视频文件走http协议
  1497. //如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
  1498. //$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
  1499. //当上传视频文件时,附加视频文件信息
  1500. if ($is_video) $data['description'] = self::json_encode($video_info);
  1501. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_UPLOAD_URL.'access_token='.$this->access_token.'&type='.$type,$data,true);
  1502. if ($result)
  1503. {
  1504. $json = json_decode($result,true);
  1505. if (!$json || !empty($json['errcode'])) {
  1506. $this->errCode = $json['errcode'];
  1507. $this->errMsg = $json['errmsg'];
  1508. return false;
  1509. }
  1510. return $json;
  1511. }
  1512. return false;
  1513. }
  1514. /**
  1515. * 上传永久图文素材(认证后的订阅号可用)
  1516. * 新增的永久素材也可以在公众平台官网素材管理模块中看到
  1517. * @param array $data 消息结构{"articles":[{...}]}
  1518. * @return boolean|array
  1519. */
  1520. public function uploadForeverArticles($data){
  1521. if (!$this->access_token && !$this->checkAuth()) return false;
  1522. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPLOAD_URL.'access_token='.$this->access_token,self::json_encode($data));
  1523. if ($result)
  1524. {
  1525. $json = json_decode($result,true);
  1526. if (!$json || !empty($json['errcode'])) {
  1527. $this->errCode = $json['errcode'];
  1528. $this->errMsg = $json['errmsg'];
  1529. return false;
  1530. }
  1531. return $json;
  1532. }
  1533. return false;
  1534. }
  1535. /**
  1536. * 修改永久图文素材(认证后的订阅号可用)
  1537. * 永久素材也可以在公众平台官网素材管理模块中看到
  1538. * @param string $media_id 图文素材id
  1539. * @param array $data 消息结构{"articles":[{...}]}
  1540. * @param int $index 更新的文章在图文素材的位置,第一篇为0,仅多图文使用
  1541. * @return boolean|array
  1542. */
  1543. public function updateForeverArticles($media_id,$data,$index=0){
  1544. if (!$this->access_token && !$this->checkAuth()) return false;
  1545. if (!isset($data['media_id'])) $data['media_id'] = $media_id;
  1546. if (!isset($data['index'])) $data['index'] = $index;
  1547. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1548. if ($result)
  1549. {
  1550. $json = json_decode($result,true);
  1551. if (!$json || !empty($json['errcode'])) {
  1552. $this->errCode = $json['errcode'];
  1553. $this->errMsg = $json['errmsg'];
  1554. return false;
  1555. }
  1556. return $json;
  1557. }
  1558. return false;
  1559. }
  1560. /**
  1561. * 获取永久素材(认证后的订阅号可用)
  1562. * 返回图文消息数组或二进制数据,失败返回false
  1563. * @param string $media_id 媒体文件id
  1564. * @param boolean $is_video 是否为视频文件,默认为否
  1565. * @return boolean|array|raw data
  1566. */
  1567. public function getForeverMedia($media_id,$is_video=false){
  1568. if (!$this->access_token && !$this->checkAuth()) return false;
  1569. $data = array('media_id' => $media_id);
  1570. //#TODO 暂不确定此接口是否需要让视频文件走http协议
  1571. //如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
  1572. //$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
  1573. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_GET_URL.'access_token='.$this->access_token,self::json_encode($data));
  1574. if ($result)
  1575. {
  1576. if (is_string($result)) {
  1577. $json = json_decode($result,true);
  1578. if (isset($json['errcode'])) {
  1579. $this->errCode = $json['errcode'];
  1580. $this->errMsg = $json['errmsg'];
  1581. return false;
  1582. }
  1583. return $json;
  1584. }
  1585. return $result;
  1586. }
  1587. return false;
  1588. }
  1589. /**
  1590. * 删除永久素材(认证后的订阅号可用)
  1591. * @param string $media_id 媒体文件id
  1592. * @return boolean
  1593. */
  1594. public function delForeverMedia($media_id){
  1595. if (!$this->access_token && !$this->checkAuth()) return false;
  1596. $data = array('media_id' => $media_id);
  1597. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_DEL_URL.'access_token='.$this->access_token,self::json_encode($data));
  1598. if ($result)
  1599. {
  1600. $json = json_decode($result,true);
  1601. if (!$json || !empty($json['errcode'])) {
  1602. $this->errCode = $json['errcode'];
  1603. $this->errMsg = $json['errmsg'];
  1604. return false;
  1605. }
  1606. return true;
  1607. }
  1608. return false;
  1609. }
  1610. /**
  1611. * 获取永久素材列表(认证后的订阅号可用)
  1612. * @param string $type 素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
  1613. * @param int $offset 全部素材的偏移位置,0表示从第一个素材
  1614. * @param int $count 返回素材的数量,取值在1到20之间
  1615. * @return boolean|array
  1616. * 返回数组格式:
  1617. * array(
  1618. * 'total_count'=>0, //该类型的素材的总数
  1619. * 'item_count'=>0, //本次调用获取的素材的数量
  1620. * 'item'=>array() //素材列表数组,内容定义请参考官方文档
  1621. * )
  1622. */
  1623. public function getForeverList($type,$offset,$count){
  1624. if (!$this->access_token && !$this->checkAuth()) return false;
  1625. $data = array(
  1626. 'type' => $type,
  1627. 'offset' => $offset,
  1628. 'count' => $count,
  1629. );
  1630. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_BATCHGET_URL.'access_token='.$this->access_token,self::json_encode($data));
  1631. if ($result)
  1632. {
  1633. $json = json_decode($result,true);
  1634. if (isset($json['errcode'])) {
  1635. $this->errCode = $json['errcode'];
  1636. $this->errMsg = $json['errmsg'];
  1637. return false;
  1638. }
  1639. return $json;
  1640. }
  1641. return false;
  1642. }
  1643. /**
  1644. * 获取永久素材总数(认证后的订阅号可用)
  1645. * @return boolean|array
  1646. * 返回数组格式:
  1647. * array(
  1648. * 'voice_count'=>0, //语音总数量
  1649. * 'video_count'=>0, //视频总数量
  1650. * 'image_count'=>0, //图片总数量
  1651. * 'news_count'=>0 //图文总数量
  1652. * )
  1653. */
  1654. public function getForeverCount(){
  1655. if (!$this->access_token && !$this->checkAuth()) return false;
  1656. $result = $this->http_get(self::API_URL_PREFIX.self::MEDIA_FOREVER_COUNT_URL.'access_token='.$this->access_token);
  1657. if ($result)
  1658. {
  1659. $json = json_decode($result,true);
  1660. if (isset($json['errcode'])) {
  1661. $this->errCode = $json['errcode'];
  1662. $this->errMsg = $json['errmsg'];
  1663. return false;
  1664. }
  1665. return $json;
  1666. }
  1667. return false;
  1668. }
  1669. /**
  1670. * 上传图文消息素材,用于群发(认证后的订阅号可用)
  1671. * @param array $data 消息结构{"articles":[{...}]}
  1672. * @return boolean|array
  1673. */
  1674. public function uploadArticles($data){
  1675. if (!$this->access_token && !$this->checkAuth()) return false;
  1676. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOADNEWS_URL.'access_token='.$this->access_token,self::json_encode($data));
  1677. if ($result)
  1678. {
  1679. $json = json_decode($result,true);
  1680. if (!$json || !empty($json['errcode'])) {
  1681. $this->errCode = $json['errcode'];
  1682. $this->errMsg = $json['errmsg'];
  1683. return false;
  1684. }
  1685. return $json;
  1686. }
  1687. return false;
  1688. }
  1689. /**
  1690. * 上传视频素材(认证后的订阅号可用)
  1691. * @param array $data 消息结构
  1692. * {
  1693. * "media_id"=>"", //通过上传媒体接口得到的MediaId
  1694. * "title"=>"TITLE", //视频标题
  1695. * "description"=>"Description" //视频描述
  1696. * }
  1697. * @return boolean|array
  1698. * {
  1699. * "type":"video",
  1700. * "media_id":"mediaid",
  1701. * "created_at":1398848981
  1702. * }
  1703. */
  1704. public function uploadMpVideo($data){
  1705. if (!$this->access_token && !$this->checkAuth()) return false;
  1706. $result = $this->http_post(self::UPLOAD_MEDIA_URL.self::MEDIA_VIDEO_UPLOAD.'access_token='.$this->access_token,self::json_encode($data));
  1707. if ($result)
  1708. {
  1709. $json = json_decode($result,true);
  1710. if (!$json || !empty($json['errcode'])) {
  1711. $this->errCode = $json['errcode'];
  1712. $this->errMsg = $json['errmsg'];
  1713. return false;
  1714. }
  1715. return $json;
  1716. }
  1717. return false;
  1718. }
  1719. /**
  1720. * 高级群发消息, 根据OpenID列表群发图文消息(订阅号不可用)
  1721. * 注意:视频需要在调用uploadMedia()方法后,再使用 uploadMpVideo() 方法生成,
  1722. * 然后获得的 mediaid 才能用于群发,且消息类型为 mpvideo 类型。
  1723. * @param array $data 消息结构
  1724. * {
  1725. * "touser"=>array(
  1726. * "OPENID1",
  1727. * "OPENID2"
  1728. * ),
  1729. * "msgtype"=>"mpvideo",
  1730. * // 在下面5种类型中选择对应的参数内容
  1731. * // mpnews | voice | image | mpvideo => array( "media_id"=>"MediaId")
  1732. * // text => array ( "content" => "hello")
  1733. * }
  1734. * @return boolean|array
  1735. */
  1736. public function sendMassMessage($data){
  1737. if (!$this->access_token && !$this->checkAuth()) return false;
  1738. $result = $this->http_post(self::API_URL_PREFIX.self::MASS_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
  1739. if ($result)
  1740. {
  1741. $json = json_decode($result,true);
  1742. if (!$json || !empty($json['errcode'])) {
  1743. $this->errCode = $json['errcode'];
  1744. $this->errMsg = $json['errmsg'];
  1745. return false;
  1746. }
  1747. return $json;
  1748. }
  1749. return false;
  1750. }
  1751. /**
  1752. * 高级群发消息, 根据群组id群发图文消息(认证后的订阅号可用)
  1753. * 注意:视频需要在调用uploadMedia()方法后,再使用 uploadMpVideo() 方法生成,
  1754. * 然后获得的 mediaid 才能用于群发,且消息类型为 mpvideo 类型。
  1755. * @param array $data 消息结构
  1756. * {
  1757. * "filter"=>array(
  1758. * "is_to_all"=>False, //是否群发给所有用户.True不用分组id,False需填写分组id
  1759. * "group_id"=>"2" //群发的分组id
  1760. * ),
  1761. * "msgtype"=>"mpvideo",
  1762. * // 在下面5种类型中选择对应的参数内容
  1763. * // mpnews | voice | image | mpvideo => array( "media_id"=>"MediaId")
  1764. * // text => array ( "content" => "hello")
  1765. * }
  1766. * @return boolean|array
  1767. */
  1768. public function sendGroupMassMessage($data){
  1769. if (!$this->access_token && !$this->checkAuth()) return false;
  1770. $result = $this->http_post(self::API_URL_PREFIX.self::MASS_SEND_GROUP_URL.'access_token='.$this->access_token,self::json_encode($data));
  1771. if ($result)
  1772. {
  1773. $json = json_decode($result,true);
  1774. if (!$json || !empty($json['errcode'])) {
  1775. $this->errCode = $json['errcode'];
  1776. $this->errMsg = $json['errmsg'];
  1777. return false;
  1778. }
  1779. return $json;
  1780. }
  1781. return false;
  1782. }
  1783. /**
  1784. * 高级群发消息, 删除群发图文消息(认证后的订阅号可用)
  1785. * @param int $msg_id 消息id
  1786. * @return boolean|array
  1787. */
  1788. public function deleteMassMessage($msg_id){
  1789. if (!$this->access_token && !$this->checkAuth()) return false;
  1790. $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)));
  1791. if ($result)
  1792. {
  1793. $json = json_decode($result,true);
  1794. if (!$json || !empty($json['errcode'])) {
  1795. $this->errCode = $json['errcode'];
  1796. $this->errMsg = $json['errmsg'];
  1797. return false;
  1798. }
  1799. return true;
  1800. }
  1801. return false;
  1802. }
  1803. /**
  1804. * 高级群发消息, 预览群发消息(认证后的订阅号可用)
  1805. * 注意:视频需要在调用uploadMedia()方法后,再使用 uploadMpVideo() 方法生成,
  1806. * 然后获得的 mediaid 才能用于群发,且消息类型为 mpvideo 类型。
  1807. * @param array $data 消息结构
  1808. * {
  1809. * "touser"=>"OPENID",
  1810. * "msgtype"=>"mpvideo",
  1811. * // 在下面5种类型中选择对应的参数内容
  1812. * // mpnews | voice | image | mpvideo => array( "media_id"=>"MediaId")
  1813. * // text => array ( "content" => "hello")
  1814. * }
  1815. * @return boolean|array
  1816. */
  1817. public function previewMassMessage($data){
  1818. if (!$this->access_token && !$this->checkAuth()) return false;
  1819. $result = $this->http_post(self::API_URL_PREFIX.self::MASS_PREVIEW_URL.'access_token='.$this->access_token,self::json_encode($data));
  1820. if ($result)
  1821. {
  1822. $json = json_decode($result,true);
  1823. if (!$json || !empty($json['errcode'])) {
  1824. $this->errCode = $json['errcode'];
  1825. $this->errMsg = $json['errmsg'];
  1826. return false;
  1827. }
  1828. return $json;
  1829. }
  1830. return false;
  1831. }
  1832. /**
  1833. * 高级群发消息, 查询群发消息发送状态(认证后的订阅号可用)
  1834. * @param int $msg_id 消息id
  1835. * @return boolean|array
  1836. * {
  1837. * "msg_id":201053012, //群发消息后返回的消息id
  1838. * "msg_status":"SEND_SUCCESS" //消息发送后的状态,SENDING表示正在发送 SEND_SUCCESS表示发送成功
  1839. * }
  1840. */
  1841. public function queryMassMessage($msg_id){
  1842. if (!$this->access_token && !$this->checkAuth()) return false;
  1843. $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)));
  1844. if ($result)
  1845. {
  1846. $json = json_decode($result,true);
  1847. if (!$json || !empty($json['errcode'])) {
  1848. $this->errCode = $json['errcode'];
  1849. $this->errMsg = $json['errmsg'];
  1850. return false;
  1851. }
  1852. return $json;
  1853. }
  1854. return false;
  1855. }
  1856. /**
  1857. * 创建二维码ticket
  1858. * @param int|string $scene_id 自定义追踪id,临时二维码只能用数值型
  1859. * @param int $type 0:临时二维码;1:永久二维码(此时expire参数无效);2:永久二维码(此时expire参数无效)
  1860. * @param int $expire 临时二维码有效期,最大为1800秒
  1861. * @return array('ticket'=>'qrcode字串','expire_seconds'=>1800,'url'=>'二维码图片解析后的地址')
  1862. */
  1863. public function getQRCode($scene_id,$type=0,$expire=1800){
  1864. if (!$this->access_token && !$this->checkAuth()) return false;
  1865. $type = ($type && is_string($scene_id))?2:$type;
  1866. $data = array(
  1867. 'action_name'=>$type?($type == 2?"QR_LIMIT_STR_SCENE":"QR_LIMIT_SCENE"):"QR_SCENE",
  1868. 'expire_seconds'=>$expire,
  1869. 'action_info'=>array('scene'=>($type == 2?array('scene_str'=>$scene_id):array('scene_id'=>$scene_id)))
  1870. );
  1871. if ($type == 1) {
  1872. unset($data['expire_seconds']);
  1873. }
  1874. $result = $this->http_post(self::API_URL_PREFIX.self::QRCODE_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1875. if ($result)
  1876. {
  1877. $json = json_decode($result,true);
  1878. if (!$json || !empty($json['errcode'])) {
  1879. $this->errCode = $json['errcode'];
  1880. $this->errMsg = $json['errmsg'];
  1881. return false;
  1882. }
  1883. return $json;
  1884. }
  1885. return false;
  1886. }
  1887. /**
  1888. * 获取二维码图片
  1889. * @param string $ticket 传入由getQRCode方法生成的ticket参数
  1890. * @return string url 返回http地址
  1891. */
  1892. public function getQRUrl($ticket) {
  1893. return self::QRCODE_IMG_URL.urlencode($ticket);
  1894. }
  1895. /**
  1896. * 长链接转短链接接口
  1897. * @param string $long_url 传入要转换的长url
  1898. * @return boolean|string url 成功则返回转换后的短url
  1899. */
  1900. public function getShortUrl($long_url){
  1901. if (!$this->access_token && !$this->checkAuth()) return false;
  1902. $data = array(
  1903. 'action'=>'long2short',
  1904. 'long_url'=>$long_url
  1905. );
  1906. $result = $this->http_post(self::API_URL_PREFIX.self::SHORT_URL.'access_token='.$this->access_token,self::json_encode($data));
  1907. if ($result)
  1908. {
  1909. $json = json_decode($result,true);
  1910. if (!$json || !empty($json['errcode'])) {
  1911. $this->errCode = $json['errcode'];
  1912. $this->errMsg = $json['errmsg'];
  1913. return false;
  1914. }
  1915. return $json['short_url'];
  1916. }
  1917. return false;
  1918. }
  1919. /**
  1920. * 获取统计数据
  1921. * @param string $type 数据分类(user|article|upstreammsg|interface)分别为(用户分析|图文分析|消息分析|接口分析)
  1922. * @param string $subtype 数据子分类,参考 DATACUBE_URL_ARR 常量定义部分 或者README.md说明文档
  1923. * @param string $begin_date 开始时间
  1924. * @param string $end_date 结束时间
  1925. * @return boolean|array 成功返回查询结果数组,其定义请看官方文档
  1926. */
  1927. public function getDatacube($type,$subtype,$begin_date,$end_date=''){
  1928. if (!$this->access_token && !$this->checkAuth()) return false;
  1929. if (!isset(self::$DATACUBE_URL_ARR[$type]) || !isset(self::$DATACUBE_URL_ARR[$type][$subtype]))
  1930. return false;
  1931. $data = array(
  1932. 'begin_date'=>$begin_date,
  1933. 'end_date'=>$end_date?$end_date:$begin_date
  1934. );
  1935. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::$DATACUBE_URL_ARR[$type][$subtype].'access_token='.$this->access_token,self::json_encode($data));
  1936. if ($result)
  1937. {
  1938. $json = json_decode($result,true);
  1939. if (!$json || !empty($json['errcode'])) {
  1940. $this->errCode = $json['errcode'];
  1941. $this->errMsg = $json['errmsg'];
  1942. return false;
  1943. }
  1944. return isset($json['list'])?$json['list']:$json;
  1945. }
  1946. return false;
  1947. }
  1948. /**
  1949. * 批量获取关注用户列表
  1950. * @param unknown $next_openid
  1951. */
  1952. public function getUserList($next_openid=''){
  1953. if (!$this->access_token && !$this->checkAuth()) return false;
  1954. $result = $this->http_get(self::API_URL_PREFIX.self::USER_GET_URL.'access_token='.$this->access_token.'&next_openid='.$next_openid);
  1955. if ($result)
  1956. {
  1957. $json = json_decode($result,true);
  1958. if (isset($json['errcode'])) {
  1959. $this->errCode = $json['errcode'];
  1960. $this->errMsg = $json['errmsg'];
  1961. return false;
  1962. }
  1963. return $json;
  1964. }
  1965. return false;
  1966. }
  1967. /**
  1968. * 获取关注者详细信息
  1969. * @param string $openid
  1970. * @return array {subscribe,openid,nickname,sex,city,province,country,language,headimgurl,subscribe_time,[unionid]}
  1971. * 注意:unionid字段 只有在用户将公众号绑定到微信开放平台账号后,才会出现。建议调用前用isset()检测一下
  1972. */
  1973. public function getUserInfo($openid){
  1974. if (!$this->access_token && !$this->checkAuth()) return false;
  1975. $result = $this->http_get(self::API_URL_PREFIX.self::USER_INFO_URL.'access_token='.$this->access_token.'&openid='.$openid);
  1976. if ($result)
  1977. {
  1978. $json = json_decode($result,true);
  1979. if (isset($json['errcode'])) {
  1980. $this->errCode = $json['errcode'];
  1981. $this->errMsg = $json['errmsg'];
  1982. return false;
  1983. }
  1984. return $json;
  1985. }
  1986. return false;
  1987. }
  1988. /**
  1989. * 设置用户备注名
  1990. * @param string $openid
  1991. * @param string $remark 备注名
  1992. * @return boolean|array
  1993. */
  1994. public function updateUserRemark($openid,$remark){
  1995. if (!$this->access_token && !$this->checkAuth()) return false;
  1996. $data = array(
  1997. 'openid'=>$openid,
  1998. 'remark'=>$remark
  1999. );
  2000. $result = $this->http_post(self::API_URL_PREFIX.self::USER_UPDATEREMARK_URL.'access_token='.$this->access_token,self::json_encode($data));
  2001. if ($result)
  2002. {
  2003. $json = json_decode($result,true);
  2004. if (!$json || !empty($json['errcode'])) {
  2005. $this->errCode = $json['errcode'];
  2006. $this->errMsg = $json['errmsg'];
  2007. return false;
  2008. }
  2009. return $json;
  2010. }
  2011. return false;
  2012. }
  2013. /**
  2014. * 获取用户分组列表
  2015. * @return boolean|array
  2016. */
  2017. public function getGroup(){
  2018. if (!$this->access_token && !$this->checkAuth()) return false;
  2019. $result = $this->http_get(self::API_URL_PREFIX.self::GROUP_GET_URL.'access_token='.$this->access_token);
  2020. if ($result)
  2021. {
  2022. $json = json_decode($result,true);
  2023. if (isset($json['errcode'])) {
  2024. $this->errCode = $json['errcode'];
  2025. $this->errMsg = $json['errmsg'];
  2026. return false;
  2027. }
  2028. return $json;
  2029. }
  2030. return false;
  2031. }
  2032. /**
  2033. * 获取用户所在分组
  2034. * @param string $openid
  2035. * @return boolean|int 成功则返回用户分组id
  2036. */
  2037. public function getUserGroup($openid){
  2038. if (!$this->access_token && !$this->checkAuth()) return false;
  2039. $data = array(
  2040. 'openid'=>$openid
  2041. );
  2042. $result = $this->http_post(self::API_URL_PREFIX.self::USER_GROUP_URL.'access_token='.$this->access_token,self::json_encode($data));
  2043. if ($result)
  2044. {
  2045. $json = json_decode($result,true);
  2046. if (!$json || !empty($json['errcode'])) {
  2047. $this->errCode = $json['errcode'];
  2048. $this->errMsg = $json['errmsg'];
  2049. return false;
  2050. } else
  2051. if (isset($json['groupid'])) return $json['groupid'];
  2052. }
  2053. return false;
  2054. }
  2055. /**
  2056. * 新增自定分组
  2057. * @param string $name 分组名称
  2058. * @return boolean|array
  2059. */
  2060. public function createGroup($name){
  2061. if (!$this->access_token && !$this->checkAuth()) return false;
  2062. $data = array(
  2063. 'group'=>array('name'=>$name)
  2064. );
  2065. $result = $this->http_post(self::API_URL_PREFIX.self::GROUP_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  2066. if ($result)
  2067. {
  2068. $json = json_decode($result,true);
  2069. if (!$json || !empty($json['errcode'])) {
  2070. $this->errCode = $json['errcode'];
  2071. $this->errMsg = $json['errmsg'];
  2072. return false;
  2073. }
  2074. return $json;
  2075. }
  2076. return false;
  2077. }
  2078. /**
  2079. * 更改分组名称
  2080. * @param int $groupid 分组id
  2081. * @param string $name 分组名称
  2082. * @return boolean|array
  2083. */
  2084. public function updateGroup($groupid,$name){
  2085. if (!$this->access_token && !$this->checkAuth()) return false;
  2086. $data = array(
  2087. 'group'=>array('id'=>$groupid,'name'=>$name)
  2088. );
  2089. $result = $this->http_post(self::API_URL_PREFIX.self::GROUP_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  2090. if ($result)
  2091. {
  2092. $json = json_decode($result,true);
  2093. if (!$json || !empty($json['errcode'])) {
  2094. $this->errCode = $json['errcode'];
  2095. $this->errMsg = $json['errmsg'];
  2096. return false;
  2097. }
  2098. return $json;
  2099. }
  2100. return false;
  2101. }
  2102. /**
  2103. * 移动用户分组
  2104. * @param int $groupid 分组id
  2105. * @param string $openid 用户openid
  2106. * @return boolean|array
  2107. */
  2108. public function updateGroupMembers($groupid,$openid){
  2109. if (!$this->access_token && !$this->checkAuth()) return false;
  2110. $data = array(
  2111. 'openid'=>$openid,
  2112. 'to_groupid'=>$groupid
  2113. );
  2114. $result = $this->http_post(self::API_URL_PREFIX.self::GROUP_MEMBER_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  2115. if ($result)
  2116. {
  2117. $json = json_decode($result,true);
  2118. if (!$json || !empty($json['errcode'])) {
  2119. $this->errCode = $json['errcode'];
  2120. $this->errMsg = $json['errmsg'];
  2121. return false;
  2122. }
  2123. return $json;
  2124. }
  2125. return false;
  2126. }
  2127. /**
  2128. * 批量移动用户分组
  2129. * @param int $groupid 分组id
  2130. * @param string $openid_list 用户openid数组,一次不能超过50个
  2131. * @return boolean|array
  2132. */
  2133. public function batchUpdateGroupMembers($groupid,$openid_list){
  2134. if (!$this->access_token && !$this->checkAuth()) return false;
  2135. $data = array(
  2136. 'openid_list'=>$openid_list,
  2137. 'to_groupid'=>$groupid
  2138. );
  2139. $result = $this->http_post(self::API_URL_PREFIX.self::GROUP_MEMBER_BATCHUPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  2140. if ($result)
  2141. {
  2142. $json = json_decode($result,true);
  2143. if (!$json || !empty($json['errcode'])) {
  2144. $this->errCode = $json['errcode'];
  2145. $this->errMsg = $json['errmsg'];
  2146. return false;
  2147. }
  2148. return $json;
  2149. }
  2150. return false;
  2151. }
  2152. /**
  2153. * 发送客服消息
  2154. * @param array $data 消息结构{"touser":"OPENID","msgtype":"news","news":{...}}
  2155. * @return boolean|array
  2156. */
  2157. public function sendCustomMessage($data){
  2158. if (!$this->access_token && !$this->checkAuth()) return false;
  2159. $result = $this->http_post(self::API_URL_PREFIX.self::CUSTOM_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
  2160. if ($result)
  2161. {
  2162. $json = json_decode($result,true);
  2163. if (!$json || !empty($json['errcode'])) {
  2164. $this->errCode = $json['errcode'];
  2165. $this->errMsg = $json['errmsg'];
  2166. return false;
  2167. }
  2168. return $json;
  2169. }
  2170. return false;
  2171. }
  2172. /**
  2173. * oauth 授权跳转接口
  2174. * @param string $callback 回调URI
  2175. * @return string
  2176. */
  2177. public function getOauthRedirect($callback,$state='',$scope='snsapi_userinfo'){
  2178. return self::OAUTH_PREFIX.self::OAUTH_AUTHORIZE_URL.'appid='.$this->appid.'&redirect_uri='.urlencode($callback).'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';
  2179. }
  2180. /**
  2181. * 通过code获取Access Token
  2182. * @return array {access_token,expires_in,refresh_token,openid,scope}
  2183. */
  2184. public function getOauthAccessToken(){
  2185. $code = isset($_GET['code'])?$_GET['code']:'';
  2186. if (!$code) return false;
  2187. $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');
  2188. if ($result)
  2189. {
  2190. $json = json_decode($result,true);
  2191. if (!$json || !empty($json['errcode'])) {
  2192. $this->errCode = $json['errcode'];
  2193. $this->errMsg = $json['errmsg'];
  2194. return false;
  2195. }
  2196. $this->user_token = $json['access_token'];
  2197. return $json;
  2198. }
  2199. return false;
  2200. }
  2201. /**
  2202. * 刷新access token并续期
  2203. * @param string $refresh_token
  2204. * @return boolean|mixed
  2205. */
  2206. public function getOauthRefreshToken($refresh_token){
  2207. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::OAUTH_REFRESH_URL.'appid='.$this->appid.'&grant_type=refresh_token&refresh_token='.$refresh_token);
  2208. if ($result)
  2209. {
  2210. $json = json_decode($result,true);
  2211. if (!$json || !empty($json['errcode'])) {
  2212. $this->errCode = $json['errcode'];
  2213. $this->errMsg = $json['errmsg'];
  2214. return false;
  2215. }
  2216. $this->user_token = $json['access_token'];
  2217. return $json;
  2218. }
  2219. return false;
  2220. }
  2221. /**
  2222. * 获取授权后的用户资料
  2223. * @param string $access_token
  2224. * @param string $openid
  2225. * @return array {openid,nickname,sex,province,city,country,headimgurl,privilege,[unionid]}
  2226. * 注意:unionid字段 只有在用户将公众号绑定到微信开放平台账号后,才会出现。建议调用前用isset()检测一下
  2227. */
  2228. public function getOauthUserinfo($access_token,$openid){
  2229. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::OAUTH_USERINFO_URL.'access_token='.$access_token.'&openid='.$openid);
  2230. if ($result)
  2231. {
  2232. $json = json_decode($result,true);
  2233. if (!$json || !empty($json['errcode'])) {
  2234. $this->errCode = $json['errcode'];
  2235. $this->errMsg = $json['errmsg'];
  2236. return false;
  2237. }
  2238. return $json;
  2239. }
  2240. return false;
  2241. }
  2242. /**
  2243. * 检验授权凭证是否有效
  2244. * @param string $access_token
  2245. * @param string $openid
  2246. * @return boolean 是否有效
  2247. */
  2248. public function getOauthAuth($access_token,$openid){
  2249. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::OAUTH_AUTH_URL.'access_token='.$access_token.'&openid='.$openid);
  2250. if ($result)
  2251. {
  2252. $json = json_decode($result,true);
  2253. if (!$json || !empty($json['errcode'])) {
  2254. $this->errCode = $json['errcode'];
  2255. $this->errMsg = $json['errmsg'];
  2256. return false;
  2257. } else
  2258. if ($json['errcode']==0) return true;
  2259. }
  2260. return false;
  2261. }
  2262. /**
  2263. * 模板消息 设置所属行业
  2264. * @param int $id1 公众号模板消息所属行业编号,参看官方开发文档 行业代码
  2265. * @param int $id2 同$id1。但如果只有一个行业,此参数可省略
  2266. * @return boolean|array
  2267. */
  2268. public function setTMIndustry($id1,$id2=''){
  2269. if ($id1) $data['industry_id1'] = $id1;
  2270. if ($id2) $data['industry_id2'] = $id2;
  2271. if (!$this->access_token && !$this->checkAuth()) return false;
  2272. $result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_SET_INDUSTRY_URL.'access_token='.$this->access_token,self::json_encode($data));
  2273. if($result){
  2274. $json = json_decode($result,true);
  2275. if (!$json || !empty($json['errcode'])) {
  2276. $this->errCode = $json['errcode'];
  2277. $this->errMsg = $json['errmsg'];
  2278. return false;
  2279. }
  2280. return $json;
  2281. }
  2282. return false;
  2283. }
  2284. /**
  2285. * 模板消息 添加消息模板
  2286. * 成功返回消息模板的调用id
  2287. * @param string $tpl_id 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
  2288. * @return boolean|string
  2289. */
  2290. public function addTemplateMessage($tpl_id){
  2291. $data = array ('template_id_short' =>$tpl_id);
  2292. if (!$this->access_token && !$this->checkAuth()) return false;
  2293. $result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_ADD_TPL_URL.'access_token='.$this->access_token,self::json_encode($data));
  2294. if($result){
  2295. $json = json_decode($result,true);
  2296. if (!$json || !empty($json['errcode'])) {
  2297. $this->errCode = $json['errcode'];
  2298. $this->errMsg = $json['errmsg'];
  2299. return false;
  2300. }
  2301. return $json['template_id'];
  2302. }
  2303. return false;
  2304. }
  2305. /**
  2306. * 发送模板消息
  2307. * @param array $data 消息结构
  2308. * {
  2309. "touser":"OPENID",
  2310. "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
  2311. "url":"http://weixin.qq.com/download",
  2312. "topcolor":"#FF0000",
  2313. "data":{
  2314. "参数名1": {
  2315. "value":"参数",
  2316. "color":"#173177" //参数颜色
  2317. },
  2318. "Date":{
  2319. "value":"06月07日 19时24分",
  2320. "color":"#173177"
  2321. },
  2322. "CardNumber":{
  2323. "value":"0426",
  2324. "color":"#173177"
  2325. },
  2326. "Type":{
  2327. "value":"消费",
  2328. "color":"#173177"
  2329. }
  2330. }
  2331. }
  2332. * @return boolean|array
  2333. */
  2334. public function sendTemplateMessage($data){
  2335. if (!$this->access_token && !$this->checkAuth()) return false;
  2336. $result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
  2337. if($result){
  2338. $json = json_decode($result,true);
  2339. if (!$json || !empty($json['errcode'])) {
  2340. $this->errCode = $json['errcode'];
  2341. $this->errMsg = $json['errmsg'];
  2342. return false;
  2343. }
  2344. return $json;
  2345. }
  2346. return false;
  2347. }
  2348. /**
  2349. * 获取多客服会话记录
  2350. * @param array $data 数据结构{"starttime":123456789,"endtime":987654321,"openid":"OPENID","pagesize":10,"pageindex":1,}
  2351. * @return boolean|array
  2352. */
  2353. public function getCustomServiceMessage($data){
  2354. if (!$this->access_token && !$this->checkAuth()) return false;
  2355. $result = $this->http_post(self::API_URL_PREFIX.self::CUSTOM_SERVICE_GET_RECORD.'access_token='.$this->access_token,self::json_encode($data));
  2356. if ($result)
  2357. {
  2358. $json = json_decode($result,true);
  2359. if (!$json || !empty($json['errcode'])) {
  2360. $this->errCode = $json['errcode'];
  2361. $this->errMsg = $json['errmsg'];
  2362. return false;
  2363. }
  2364. return $json;
  2365. }
  2366. return false;
  2367. }
  2368. /**
  2369. * 转发多客服消息
  2370. * Example: $obj->transfer_customer_service($customer_account)->reply();
  2371. * @param string $customer_account 转发到指定客服帐号:test1@test
  2372. */
  2373. public function transfer_customer_service($customer_account = '')
  2374. {
  2375. $msg = array(
  2376. 'ToUserName' => $this->getRevFrom(),
  2377. 'FromUserName'=>$this->getRevTo(),
  2378. 'CreateTime'=>time(),
  2379. 'MsgType'=>'transfer_customer_service',
  2380. );
  2381. if ($customer_account) {
  2382. $msg['TransInfo'] = array('KfAccount'=>$customer_account);
  2383. }
  2384. $this->Message($msg);
  2385. return $this;
  2386. }
  2387. /**
  2388. * 获取多客服客服基本信息
  2389. *
  2390. * @return boolean|array
  2391. */
  2392. public function getCustomServiceKFlist(){
  2393. if (!$this->access_token && !$this->checkAuth()) return false;
  2394. $result = $this->http_get(self::API_URL_PREFIX.self::CUSTOM_SERVICE_GET_KFLIST.'access_token='.$this->access_token);
  2395. if ($result)
  2396. {
  2397. $json = json_decode($result,true);
  2398. if (!$json || !empty($json['errcode'])) {
  2399. $this->errCode = $json['errcode'];
  2400. $this->errMsg = $json['errmsg'];
  2401. return false;
  2402. }
  2403. return $json;
  2404. }
  2405. return false;
  2406. }
  2407. /**
  2408. * 获取多客服在线客服接待信息
  2409. *
  2410. * @return boolean|array {
  2411. "kf_online_list": [
  2412. {
  2413. "kf_account": "test1@test", //客服账号@微信别名
  2414. "status": 1, //客服在线状态 1:pc在线,2:手机在线,若pc和手机同时在线则为 1+2=3
  2415. "kf_id": "1001", //客服工号
  2416. "auto_accept": 0, //客服设置的最大自动接入数
  2417. "accepted_case": 1 //客服当前正在接待的会话数
  2418. }
  2419. ]
  2420. }
  2421. */
  2422. public function getCustomServiceOnlineKFlist(){
  2423. if (!$this->access_token && !$this->checkAuth()) return false;
  2424. $result = $this->http_get(self::API_URL_PREFIX.self::CUSTOM_SERVICE_GET_ONLINEKFLIST.'access_token='.$this->access_token);
  2425. if ($result)
  2426. {
  2427. $json = json_decode($result,true);
  2428. if (!$json || !empty($json['errcode'])) {
  2429. $this->errCode = $json['errcode'];
  2430. $this->errMsg = $json['errmsg'];
  2431. return false;
  2432. }
  2433. return $json;
  2434. }
  2435. return false;
  2436. }
  2437. /**
  2438. * 创建指定多客服会话
  2439. * @tutorial 当用户已被其他客服接待或指定客服不在线则会失败
  2440. * @param string $openid //用户openid
  2441. * @param string $kf_account //客服账号
  2442. * @param string $text //附加信息,文本会展示在客服人员的多客服客户端,可为空
  2443. * @return boolean | array //成功返回json数组
  2444. * {
  2445. * "errcode": 0,
  2446. * "errmsg": "ok",
  2447. * }
  2448. */
  2449. public function createKFSession($openid,$kf_account,$text=''){
  2450. $data=array(
  2451. "openid" =>$openid,
  2452. "kf_account" => $kf_account
  2453. );
  2454. if ($text) $data["text"] = $text;
  2455. if (!$this->access_token && !$this->checkAuth()) return false;
  2456. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::CUSTOM_SESSION_CREATE.'access_token='.$this->access_token,self::json_encode($data));
  2457. if ($result)
  2458. {
  2459. $json = json_decode($result,true);
  2460. if (!$json || !empty($json['errcode'])) {
  2461. $this->errCode = $json['errcode'];
  2462. $this->errMsg = $json['errmsg'];
  2463. return false;
  2464. }
  2465. return $json;
  2466. }
  2467. return false;
  2468. }
  2469. /**
  2470. * 关闭指定多客服会话
  2471. * @tutorial 当用户被其他客服接待时则会失败
  2472. * @param string $openid //用户openid
  2473. * @param string $kf_account //客服账号
  2474. * @param string $text //附加信息,文本会展示在客服人员的多客服客户端,可为空
  2475. * @return boolean | array //成功返回json数组
  2476. * {
  2477. * "errcode": 0,
  2478. * "errmsg": "ok",
  2479. * }
  2480. */
  2481. public function closeKFSession($openid,$kf_account,$text=''){
  2482. $data=array(
  2483. "openid" =>$openid,
  2484. "nickname" => $kf_account
  2485. );
  2486. if ($text) $data["text"] = $text;
  2487. if (!$this->access_token && !$this->checkAuth()) return false;
  2488. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::CUSTOM_SESSION_CLOSE .'access_token='.$this->access_token,self::json_encode($data));
  2489. if ($result)
  2490. {
  2491. $json = json_decode($result,true);
  2492. if (!$json || !empty($json['errcode'])) {
  2493. $this->errCode = $json['errcode'];
  2494. $this->errMsg = $json['errmsg'];
  2495. return false;
  2496. }
  2497. return $json;
  2498. }
  2499. return false;
  2500. }
  2501. /**
  2502. * 获取用户会话状态
  2503. * @param string $openid //用户openid
  2504. * @return boolean | array //成功返回json数组
  2505. * {
  2506. * "errcode" : 0,
  2507. * "errmsg" : "ok",
  2508. * "kf_account" : "test1@test", //正在接待的客服
  2509. * "createtime": 123456789, //会话接入时间
  2510. * }
  2511. */
  2512. public function getKFSession($openid){
  2513. if (!$this->access_token && !$this->checkAuth()) return false;
  2514. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::CUSTOM_SESSION_GET .'access_token='.$this->access_token.'&openid='.$openid);
  2515. if ($result)
  2516. {
  2517. $json = json_decode($result,true);
  2518. if (!$json || !empty($json['errcode'])) {
  2519. $this->errCode = $json['errcode'];
  2520. $this->errMsg = $json['errmsg'];
  2521. return false;
  2522. }
  2523. return $json;
  2524. }
  2525. return false;
  2526. }
  2527. /**
  2528. * 获取指定客服的会话列表
  2529. * @param string $openid //用户openid
  2530. * @return boolean | array //成功返回json数组
  2531. * array(
  2532. * 'sessionlist' => array (
  2533. * array (
  2534. * 'openid'=>'OPENID', //客户 openid
  2535. * 'createtime'=>123456789, //会话创建时间,UNIX 时间戳
  2536. * ),
  2537. * array (
  2538. * 'openid'=>'OPENID', //客户 openid
  2539. * 'createtime'=>123456789, //会话创建时间,UNIX 时间戳
  2540. * ),
  2541. * )
  2542. * )
  2543. */
  2544. public function getKFSessionlist($kf_account){
  2545. if (!$this->access_token && !$this->checkAuth()) return false;
  2546. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::CUSTOM_SESSION_GET_LIST .'access_token='.$this->access_token.'&kf_account='.$kf_account);
  2547. if ($result)
  2548. {
  2549. $json = json_decode($result,true);
  2550. if (!$json || !empty($json['errcode'])) {
  2551. $this->errCode = $json['errcode'];
  2552. $this->errMsg = $json['errmsg'];
  2553. return false;
  2554. }
  2555. return $json;
  2556. }
  2557. return false;
  2558. }
  2559. /**
  2560. * 获取未接入会话列表
  2561. * @param string $openid //用户openid
  2562. * @return boolean | array //成功返回json数组
  2563. * array (
  2564. * 'count' => 150 , //未接入会话数量
  2565. * 'waitcaselist' => array (
  2566. * array (
  2567. * 'openid'=>'OPENID', //客户 openid
  2568. * 'kf_account ' =>'', //指定接待的客服,为空则未指定
  2569. * 'createtime'=>123456789, //会话创建时间,UNIX 时间戳
  2570. * ),
  2571. * array (
  2572. * 'openid'=>'OPENID', //客户 openid
  2573. * 'kf_account ' =>'', //指定接待的客服,为空则未指定
  2574. * 'createtime'=>123456789, //会话创建时间,UNIX 时间戳
  2575. * )
  2576. * )
  2577. * )
  2578. */
  2579. public function getKFSessionWait(){
  2580. if (!$this->access_token && !$this->checkAuth()) return false;
  2581. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::CUSTOM_SESSION_GET_WAIT .'access_token='.$this->access_token);
  2582. if ($result)
  2583. {
  2584. $json = json_decode($result,true);
  2585. if (!$json || !empty($json['errcode'])) {
  2586. $this->errCode = $json['errcode'];
  2587. $this->errMsg = $json['errmsg'];
  2588. return false;
  2589. }
  2590. return $json;
  2591. }
  2592. return false;
  2593. }
  2594. /**
  2595. * 添加客服账号
  2596. *
  2597. * @param string $account //完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符
  2598. * @param string $nickname //客服昵称,最长6个汉字或12个英文字符
  2599. * @param string $password //客服账号明文登录密码,会自动加密
  2600. * @return boolean|array
  2601. * 成功返回结果
  2602. * {
  2603. * "errcode": 0,
  2604. * "errmsg": "ok",
  2605. * }
  2606. */
  2607. public function addKFAccount($account,$nickname,$password){
  2608. $data=array(
  2609. "kf_account" =>$account,
  2610. "nickname" => $nickname,
  2611. "password" => md5($password)
  2612. );
  2613. if (!$this->access_token && !$this->checkAuth()) return false;
  2614. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::CS_KF_ACCOUNT_ADD_URL.'access_token='.$this->access_token,self::json_encode($data));
  2615. if ($result)
  2616. {
  2617. $json = json_decode($result,true);
  2618. if (!$json || !empty($json['errcode'])) {
  2619. $this->errCode = $json['errcode'];
  2620. $this->errMsg = $json['errmsg'];
  2621. return false;
  2622. }
  2623. return $json;
  2624. }
  2625. return false;
  2626. }
  2627. /**
  2628. * 修改客服账号信息
  2629. *
  2630. * @param string $account //完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符
  2631. * @param string $nickname //客服昵称,最长6个汉字或12个英文字符
  2632. * @param string $password //客服账号明文登录密码,会自动加密
  2633. * @return boolean|array
  2634. * 成功返回结果
  2635. * {
  2636. * "errcode": 0,
  2637. * "errmsg": "ok",
  2638. * }
  2639. */
  2640. public function updateKFAccount($account,$nickname,$password){
  2641. $data=array(
  2642. "kf_account" =>$account,
  2643. "nickname" => $nickname,
  2644. "password" => md5($password)
  2645. );
  2646. if (!$this->access_token && !$this->checkAuth()) return false;
  2647. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::CS_KF_ACCOUNT_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  2648. if ($result)
  2649. {
  2650. $json = json_decode($result,true);
  2651. if (!$json || !empty($json['errcode'])) {
  2652. $this->errCode = $json['errcode'];
  2653. $this->errMsg = $json['errmsg'];
  2654. return false;
  2655. }
  2656. return $json;
  2657. }
  2658. return false;
  2659. }
  2660. /**
  2661. * 删除客服账号
  2662. *
  2663. * @param string $account //完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符
  2664. * @return boolean|array
  2665. * 成功返回结果
  2666. * {
  2667. * "errcode": 0,
  2668. * "errmsg": "ok",
  2669. * }
  2670. */
  2671. public function deleteKFAccount($account){
  2672. if (!$this->access_token && !$this->checkAuth()) return false;
  2673. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::CS_KF_ACCOUNT_DEL_URL.'access_token='.$this->access_token.'&kf_account='.$account);
  2674. if ($result)
  2675. {
  2676. $json = json_decode($result,true);
  2677. if (!$json || !empty($json['errcode'])) {
  2678. $this->errCode = $json['errcode'];
  2679. $this->errMsg = $json['errmsg'];
  2680. return false;
  2681. }
  2682. return $json;
  2683. }
  2684. return false;
  2685. }
  2686. /**
  2687. * 上传客服头像
  2688. *
  2689. * @param string $account //完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符
  2690. * @param string $imgfile //头像文件完整路径,如:'D:\user.jpg'。头像文件必须JPG格式,像素建议640*640
  2691. * @return boolean|array
  2692. * 成功返回结果
  2693. * {
  2694. * "errcode": 0,
  2695. * "errmsg": "ok",
  2696. * }
  2697. */
  2698. public function setKFHeadImg($account,$imgfile){
  2699. if (!$this->access_token && !$this->checkAuth()) return false;
  2700. $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);
  2701. if ($result)
  2702. {
  2703. $json = json_decode($result,true);
  2704. if (!$json || !empty($json['errcode'])) {
  2705. $this->errCode = $json['errcode'];
  2706. $this->errMsg = $json['errmsg'];
  2707. return false;
  2708. }
  2709. return $json;
  2710. }
  2711. return false;
  2712. }
  2713. /**
  2714. * 语义理解接口
  2715. * @param String $uid 用户唯一id(非开发者id),用户区分公众号下的不同用户(建议填入用户openid)
  2716. * @param String $query 输入文本串
  2717. * @param String $category 需要使用的服务类型,多个用“,”隔开,不能为空
  2718. * @param Float $latitude 纬度坐标,与经度同时传入;与城市二选一传入
  2719. * @param Float $longitude 经度坐标,与纬度同时传入;与城市二选一传入
  2720. * @param String $city 城市名称,与经纬度二选一传入
  2721. * @param String $region 区域名称,在城市存在的情况下可省略;与经纬度二选一传入
  2722. * @return boolean|array
  2723. */
  2724. public function querySemantic($uid,$query,$category,$latitude=0,$longitude=0,$city="",$region=""){
  2725. if (!$this->access_token && !$this->checkAuth()) return false;
  2726. $data=array(
  2727. 'query' => $query,
  2728. 'category' => $category,
  2729. 'appid' => $this->appid,
  2730. 'uid' => ''
  2731. );
  2732. //地理坐标或城市名称二选一
  2733. if ($latitude) {
  2734. $data['latitude'] = $latitude;
  2735. $data['longitude'] = $longitude;
  2736. } elseif ($city) {
  2737. $data['city'] = $city;
  2738. } elseif ($region) {
  2739. $data['region'] = $region;
  2740. }
  2741. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::SEMANTIC_API_URL.'access_token='.$this->access_token,self::json_encode($data));
  2742. if ($result)
  2743. {
  2744. $json = json_decode($result,true);
  2745. if (!$json || !empty($json['errcode'])) {
  2746. $this->errCode = $json['errcode'];
  2747. $this->errMsg = $json['errmsg'];
  2748. return false;
  2749. }
  2750. return $json;
  2751. }
  2752. return false;
  2753. }
  2754. /**
  2755. * 创建卡券
  2756. * @param Array $data 卡券数据
  2757. * @return array|boolean 返回数组中card_id为卡券ID
  2758. */
  2759. public function createCard($data) {
  2760. if (!$this->access_token && !$this->checkAuth()) return false;
  2761. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CREATE . 'access_token=' . $this->access_token, self::json_encode($data));
  2762. if ($result) {
  2763. $json = json_decode($result, true);
  2764. if (!$json || !empty($json['errcode'])) {
  2765. $this->errCode = $json['errcode'];
  2766. $this->errMsg = $json['errmsg'];
  2767. return false;
  2768. }
  2769. return $json;
  2770. }
  2771. return false;
  2772. }
  2773. /**
  2774. * 更改卡券信息
  2775. * 调用该接口更新信息后会重新送审,卡券状态变更为待审核。已被用户领取的卡券会实时更新票面信息。
  2776. * @param string $data
  2777. * @return boolean
  2778. */
  2779. public function updateCard($data) {
  2780. if (!$this->access_token && !$this->checkAuth()) return false;
  2781. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  2782. if ($result) {
  2783. $json = json_decode($result, true);
  2784. if (!$json || !empty($json['errcode'])) {
  2785. $this->errCode = $json['errcode'];
  2786. $this->errMsg = $json['errmsg'];
  2787. return false;
  2788. }
  2789. return true;
  2790. }
  2791. return false;
  2792. }
  2793. /**
  2794. * 删除卡券
  2795. * 允许商户删除任意一类卡券。删除卡券后,该卡券对应已生成的领取用二维码、添加到卡包 JS API 均会失效。
  2796. * 注意:删除卡券不能删除已被用户领取,保存在微信客户端中的卡券,已领取的卡券依旧有效。
  2797. * @param string $card_id 卡券ID
  2798. * @return boolean
  2799. */
  2800. public function delCard($card_id) {
  2801. $data = array(
  2802. 'card_id' => $card_id,
  2803. );
  2804. if (!$this->access_token && !$this->checkAuth()) return false;
  2805. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_DELETE . 'access_token=' . $this->access_token, self::json_encode($data));
  2806. if ($result) {
  2807. $json = json_decode($result, true);
  2808. if (!$json || !empty($json['errcode'])) {
  2809. $this->errCode = $json['errcode'];
  2810. $this->errMsg = $json['errmsg'];
  2811. return false;
  2812. }
  2813. return true;
  2814. }
  2815. return false;
  2816. }
  2817. /**
  2818. * 查询卡券详情
  2819. * @param string $card_id
  2820. * @return boolean|array 返回数组信息比较复杂,请参看卡券接口文档
  2821. */
  2822. public function getCardInfo($card_id) {
  2823. $data = array(
  2824. 'card_id' => $card_id,
  2825. );
  2826. if (!$this->access_token && !$this->checkAuth()) return false;
  2827. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_GET . 'access_token=' . $this->access_token, self::json_encode($data));
  2828. if ($result) {
  2829. $json = json_decode($result, true);
  2830. if (!$json || !empty($json['errcode'])) {
  2831. $this->errCode = $json['errcode'];
  2832. $this->errMsg = $json['errmsg'];
  2833. return false;
  2834. }
  2835. return $json;
  2836. }
  2837. return false;
  2838. }
  2839. /**
  2840. * 获取颜色列表
  2841. * 获得卡券的最新颜色列表,用于创建卡券
  2842. * @return boolean|array 返回数组请参看 微信卡券接口文档 的json格式
  2843. */
  2844. public function getCardColors() {
  2845. if (!$this->access_token && !$this->checkAuth()) return false;
  2846. $result = $this->http_get(self::API_BASE_URL_PREFIX . self::CARD_GETCOLORS . 'access_token=' . $this->access_token);
  2847. if ($result) {
  2848. $json = json_decode($result, true);
  2849. if (!$json || !empty($json['errcode'])) {
  2850. $this->errCode = $json['errcode'];
  2851. $this->errMsg = $json['errmsg'];
  2852. return false;
  2853. }
  2854. return $json;
  2855. }
  2856. return false;
  2857. }
  2858. /**
  2859. * 拉取门店列表
  2860. * 获取在公众平台上申请创建的门店列表
  2861. * @param int $offset 开始拉取的偏移,默认为0从头开始
  2862. * @param int $count 拉取的数量,默认为0拉取全部
  2863. * @return boolean|array 返回数组请参看 微信卡券接口文档 的json格式
  2864. */
  2865. public function getCardLocations($offset=0,$count=0) {
  2866. $data=array(
  2867. 'offset'=>$offset,
  2868. 'count'=>$count
  2869. );
  2870. if (!$this->access_token && !$this->checkAuth()) return false;
  2871. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_LOCATION_BATCHGET . 'access_token=' . $this->access_token, self::json_encode($data));
  2872. if ($result) {
  2873. $json = json_decode($result, true);
  2874. if (!$json || !empty($json['errcode'])) {
  2875. $this->errCode = $json['errcode'];
  2876. $this->errMsg = $json['errmsg'];
  2877. return false;
  2878. }
  2879. return $json;
  2880. }
  2881. return false;
  2882. }
  2883. /**
  2884. * 批量导入门店信息
  2885. * @tutorial 返回插入的门店id列表,以逗号分隔。如果有插入失败的,则为-1,请自行核查是哪个插入失败
  2886. * @param array $data 数组形式的json数据,由于内容较多,具体内容格式请查看 微信卡券接口文档
  2887. * @return boolean|string 成功返回插入的门店id列表
  2888. */
  2889. public function addCardLocations($data) {
  2890. if (!$this->access_token && !$this->checkAuth()) return false;
  2891. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_LOCATION_BATCHADD . 'access_token=' . $this->access_token, self::json_encode($data));
  2892. if ($result) {
  2893. $json = json_decode($result, true);
  2894. if (!$json || !empty($json['errcode'])) {
  2895. $this->errCode = $json['errcode'];
  2896. $this->errMsg = $json['errmsg'];
  2897. return false;
  2898. }
  2899. return $json;
  2900. }
  2901. return false;
  2902. }
  2903. /**
  2904. * 生成卡券二维码
  2905. * 成功则直接返回ticket值,可以用 getQRUrl($ticket) 换取二维码url
  2906. *
  2907. * @param string $cardid 卡券ID 必须
  2908. * @param string $code 指定卡券 code 码,只能被领一次。use_custom_code 字段为 true 的卡券必须填写,非自定义 code 不必填写。
  2909. * @param string $openid 指定领取者的 openid,只有该用户能领取。bind_openid 字段为 true 的卡券必须填写,非自定义 openid 不必填写。
  2910. * @param int $expire_seconds 指定二维码的有效时间,范围是 60 ~ 1800 秒。不填默认为永久有效。
  2911. * @param boolean $is_unique_code 指定下发二维码,生成的二维码随机分配一个 code,领取后不可再次扫描。填写 true 或 false。默认 false。
  2912. * @param string $balance 红包余额,以分为单位。红包类型必填(LUCKY_MONEY),其他卡券类型不填。
  2913. * @return boolean|string
  2914. */
  2915. public function createCardQrcode($card_id,$code='',$openid='',$expire_seconds=0,$is_unique_code=false,$balance='') {
  2916. $card = array(
  2917. 'card_id' => $card_id
  2918. );
  2919. if ($code)
  2920. $card['code'] = $code;
  2921. if ($openid)
  2922. $card['openid'] = $openid;
  2923. if ($expire_seconds)
  2924. $card['expire_seconds'] = $expire_seconds;
  2925. if ($is_unique_code)
  2926. $card['is_unique_code'] = $is_unique_code;
  2927. if ($balance)
  2928. $card['balance'] = $balance;
  2929. $data = array(
  2930. 'action_name' => "QR_CARD",
  2931. 'action_info' => array('card' => $card)
  2932. );
  2933. if (!$this->access_token && !$this->checkAuth()) return false;
  2934. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_QRCODE_CREATE . '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 $json;
  2943. }
  2944. return false;
  2945. }
  2946. /**
  2947. * 消耗 code
  2948. * 自定义 code(use_custom_code 为 true)的优惠券,在 code 被核销时,必须调用此接口。
  2949. *
  2950. * @param string $code 要消耗的序列号
  2951. * @param string $card_id 要消耗序列号所述的 card_id,创建卡券时use_custom_code 填写 true 时必填。
  2952. * @return boolean|array
  2953. * {
  2954. * "errcode":0,
  2955. * "errmsg":"ok",
  2956. * "card":{"card_id":"pFS7Fjg8kV1IdDz01r4SQwMkuCKc"},
  2957. * "openid":"oFS7Fjl0WsZ9AMZqrI80nbIq8xrA"
  2958. * }
  2959. */
  2960. public function consumeCardCode($code,$card_id='') {
  2961. $data = array('code' => $code);
  2962. if ($card_id)
  2963. $data['card_id'] = $card_id;
  2964. if (!$this->access_token && !$this->checkAuth()) return false;
  2965. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_CONSUME . 'access_token=' . $this->access_token, self::json_encode($data));
  2966. if ($result) {
  2967. $json = json_decode($result, true);
  2968. if (!$json || !empty($json['errcode'])) {
  2969. $this->errCode = $json['errcode'];
  2970. $this->errMsg = $json['errmsg'];
  2971. return false;
  2972. }
  2973. return $json;
  2974. }
  2975. return false;
  2976. }
  2977. /**
  2978. * code 解码
  2979. * @param string $encrypt_code 通过 choose_card_info 获取的加密字符串
  2980. * @return boolean|array
  2981. * {
  2982. * "errcode":0,
  2983. * "errmsg":"ok",
  2984. * "code":"751234212312"
  2985. * }
  2986. */
  2987. public function decryptCardCode($encrypt_code) {
  2988. $data = array(
  2989. 'encrypt_code' => $encrypt_code,
  2990. );
  2991. if (!$this->access_token && !$this->checkAuth()) return false;
  2992. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_DECRYPT . 'access_token=' . $this->access_token, self::json_encode($data));
  2993. if ($result) {
  2994. $json = json_decode($result, true);
  2995. if (!$json || !empty($json['errcode'])) {
  2996. $this->errCode = $json['errcode'];
  2997. $this->errMsg = $json['errmsg'];
  2998. return false;
  2999. }
  3000. return $json;
  3001. }
  3002. return false;
  3003. }
  3004. /**
  3005. * 查询 code 的有效性(非自定义 code)
  3006. * @param string $code
  3007. * @return boolean|array
  3008. * {
  3009. * "errcode":0,
  3010. * "errmsg":"ok",
  3011. * "openid":"oFS7Fjl0WsZ9AMZqrI80nbIq8xrA", //用户 openid
  3012. * "card":{
  3013. * "card_id":"pFS7Fjg8kV1IdDz01r4SQwMkuCKc",
  3014. * "begin_time": 1404205036, //起始使用时间
  3015. * "end_time": 1404205036, //结束时间
  3016. * }
  3017. * }
  3018. */
  3019. public function checkCardCode($code) {
  3020. $data = array(
  3021. 'code' => $code,
  3022. );
  3023. if (!$this->access_token && !$this->checkAuth()) return false;
  3024. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_GET . 'access_token=' . $this->access_token, self::json_encode($data));
  3025. if ($result) {
  3026. $json = json_decode($result, true);
  3027. if (!$json || !empty($json['errcode'])) {
  3028. $this->errCode = $json['errcode'];
  3029. $this->errMsg = $json['errmsg'];
  3030. return false;
  3031. }
  3032. return $json;
  3033. }
  3034. return false;
  3035. }
  3036. /**
  3037. * 批量查询卡列表
  3038. * @param $offset 开始拉取的偏移,默认为0从头开始
  3039. * @param $count 需要查询的卡片的数量(数量最大50,默认50)
  3040. * @return boolean|array
  3041. * {
  3042. * "errcode":0,
  3043. * "errmsg":"ok",
  3044. * "card_id_list":["ph_gmt7cUVrlRk8swPwx7aDyF-pg"], //卡 id 列表
  3045. * "total_num":1 //该商户名下 card_id 总数
  3046. * }
  3047. */
  3048. public function getCardIdList($offset=0,$count=50) {
  3049. if ($count>50)
  3050. $count = 50;
  3051. $data = array(
  3052. 'offset' => $offset,
  3053. 'count' => $count,
  3054. );
  3055. if (!$this->access_token && !$this->checkAuth()) return false;
  3056. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_BATCHGET . 'access_token=' . $this->access_token, self::json_encode($data));
  3057. if ($result) {
  3058. $json = json_decode($result, true);
  3059. if (!$json || !empty($json['errcode'])) {
  3060. $this->errCode = $json['errcode'];
  3061. $this->errMsg = $json['errmsg'];
  3062. return false;
  3063. }
  3064. return $json;
  3065. }
  3066. return false;
  3067. }
  3068. /**
  3069. * 更改 code
  3070. * 为确保转赠后的安全性,微信允许自定义code的商户对已下发的code进行更改。
  3071. * 注:为避免用户疑惑,建议仅在发生转赠行为后(发生转赠后,微信会通过事件推送的方式告知商户被转赠的卡券code)对用户的code进行更改。
  3072. * @param string $code 卡券的 code 编码
  3073. * @param string $card_id 卡券 ID
  3074. * @param string $new_code 新的卡券 code 编码
  3075. * @return boolean
  3076. */
  3077. public function updateCardCode($code,$card_id,$new_code) {
  3078. $data = array(
  3079. 'code' => $code,
  3080. 'card_id' => $card_id,
  3081. 'new_code' => $new_code,
  3082. );
  3083. if (!$this->access_token && !$this->checkAuth()) return false;
  3084. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3085. if ($result) {
  3086. $json = json_decode($result, true);
  3087. if (!$json || !empty($json['errcode'])) {
  3088. $this->errCode = $json['errcode'];
  3089. $this->errMsg = $json['errmsg'];
  3090. return false;
  3091. }
  3092. return true;
  3093. }
  3094. return false;
  3095. }
  3096. /**
  3097. * 设置卡券失效
  3098. * 设置卡券失效的操作不可逆
  3099. * @param string $code 需要设置为失效的 code
  3100. * @param string $card_id 自定义 code 的卡券必填。非自定义 code 的卡券不填。
  3101. * @return boolean
  3102. */
  3103. public function unavailableCardCode($code,$card_id='') {
  3104. $data = array(
  3105. 'code' => $code,
  3106. );
  3107. if ($card_id)
  3108. $data['card_id'] = $card_id;
  3109. if (!$this->access_token && !$this->checkAuth()) return false;
  3110. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_UNAVAILABLE . 'access_token=' . $this->access_token, self::json_encode($data));
  3111. if ($result) {
  3112. $json = json_decode($result, true);
  3113. if (!$json || !empty($json['errcode'])) {
  3114. $this->errCode = $json['errcode'];
  3115. $this->errMsg = $json['errmsg'];
  3116. return false;
  3117. }
  3118. return true;
  3119. }
  3120. return false;
  3121. }
  3122. /**
  3123. * 库存修改
  3124. * @param string $data
  3125. * @return boolean
  3126. */
  3127. public function modifyCardStock($data) {
  3128. if (!$this->access_token && !$this->checkAuth()) return false;
  3129. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_MODIFY_STOCK . 'access_token=' . $this->access_token, self::json_encode($data));
  3130. if ($result) {
  3131. $json = json_decode($result, true);
  3132. if (!$json || !empty($json['errcode'])) {
  3133. $this->errCode = $json['errcode'];
  3134. $this->errMsg = $json['errmsg'];
  3135. return false;
  3136. }
  3137. return true;
  3138. }
  3139. return false;
  3140. }
  3141. /**
  3142. * 激活/绑定会员卡
  3143. * @param string $data 具体结构请参看卡券开发文档(6.1.1 激活/绑定会员卡)章节
  3144. * @return boolean
  3145. */
  3146. public function activateMemberCard($data) {
  3147. if (!$this->access_token && !$this->checkAuth()) return false;
  3148. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_MEMBERCARD_ACTIVATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3149. if ($result) {
  3150. $json = json_decode($result, true);
  3151. if (!$json || !empty($json['errcode'])) {
  3152. $this->errCode = $json['errcode'];
  3153. $this->errMsg = $json['errmsg'];
  3154. return false;
  3155. }
  3156. return true;
  3157. }
  3158. return false;
  3159. }
  3160. /**
  3161. * 会员卡交易
  3162. * 会员卡交易后每次积分及余额变更需通过接口通知微信,便于后续消息通知及其他扩展功能。
  3163. * @param string $data 具体结构请参看卡券开发文档(6.1.2 会员卡交易)章节
  3164. * @return boolean|array
  3165. */
  3166. public function updateMemberCard($data) {
  3167. if (!$this->access_token && !$this->checkAuth()) return false;
  3168. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_MEMBERCARD_UPDATEUSER . 'access_token=' . $this->access_token, self::json_encode($data));
  3169. if ($result) {
  3170. $json = json_decode($result, true);
  3171. if (!$json || !empty($json['errcode'])) {
  3172. $this->errCode = $json['errcode'];
  3173. $this->errMsg = $json['errmsg'];
  3174. return false;
  3175. }
  3176. return $json;
  3177. }
  3178. return false;
  3179. }
  3180. /**
  3181. * 更新红包金额
  3182. * @param string $code 红包的序列号
  3183. * @param $balance 红包余额
  3184. * @param string $card_id 自定义 code 的卡券必填。非自定义 code 可不填。
  3185. * @return boolean|array
  3186. */
  3187. public function updateLuckyMoney($code,$balance,$card_id='') {
  3188. $data = array(
  3189. 'code' => $code,
  3190. 'balance' => $balance
  3191. );
  3192. if ($card_id)
  3193. $data['card_id'] = $card_id;
  3194. if (!$this->access_token && !$this->checkAuth()) return false;
  3195. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_LUCKYMONEY_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3196. if ($result) {
  3197. $json = json_decode($result, true);
  3198. if (!$json || !empty($json['errcode'])) {
  3199. $this->errCode = $json['errcode'];
  3200. $this->errMsg = $json['errmsg'];
  3201. return false;
  3202. }
  3203. return true;
  3204. }
  3205. return false;
  3206. }
  3207. /**
  3208. * 设置卡券测试白名单
  3209. * @param string $openid 测试的 openid 列表
  3210. * @param string $user 测试的微信号列表
  3211. * @return boolean
  3212. */
  3213. public function setCardTestWhiteList($openid=array(),$user=array()) {
  3214. $data = array();
  3215. if (count($openid) > 0)
  3216. $data['openid'] = $openid;
  3217. if (count($user) > 0)
  3218. $data['username'] = $user;
  3219. if (!$this->access_token && !$this->checkAuth()) return false;
  3220. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_TESTWHILELIST_SET . 'access_token=' . $this->access_token, self::json_encode($data));
  3221. if ($result) {
  3222. $json = json_decode($result, true);
  3223. if (!$json || !empty($json['errcode'])) {
  3224. $this->errCode = $json['errcode'];
  3225. $this->errMsg = $json['errmsg'];
  3226. return false;
  3227. }
  3228. return true;
  3229. }
  3230. return false;
  3231. }
  3232. /**
  3233. * 申请设备ID
  3234. * [applyShakeAroundDevice 申请配置设备所需的UUID、Major、Minor。
  3235. * 若激活率小于50%,不能新增设备。单次新增设备超过500 个,需走人工审核流程。
  3236. * 审核通过后,可用迒回的批次ID 用“查询设备列表”接口拉取本次申请的设备ID]
  3237. * @param array $data
  3238. * array(
  3239. * "quantity" => 3, //申请的设备ID 的数量,单次新增设备超过500 个,需走人工审核流程(必填)
  3240. * "apply_reason" => "测试",//申请理由(必填)
  3241. * "comment" => "测试专用", //备注(非必填)
  3242. * "poi_id" => 1234 //设备关联的门店ID(非必填)
  3243. * )
  3244. * @return boolean|mixed
  3245. * {
  3246. "data": {
  3247. "apply_id": 123,
  3248. "device_identifiers":[
  3249. {
  3250. "device_id":10100,
  3251. "uuid":"FDA50693-A4E2-4FB1-AFCF-C6EB07647825",
  3252. "major":10001,
  3253. "minor":10002
  3254. }
  3255. ]
  3256. },
  3257. "errcode": 0,
  3258. "errmsg": "success."
  3259. }
  3260. apply_id:申请的批次ID,可用在“查询设备列表”接口按批次查询本次申请成功的设备ID
  3261. device_identifiers:指定的设备ID 列表
  3262. device_id:设备编号
  3263. uuid、major、minor
  3264. audit_status:审核状态。0:审核未通过、1:审核中、2:审核已通过;审核会在三个工作日内完成
  3265. audit_comment:审核备注,包括审核不通过的原因
  3266. * @access public
  3267. * @author polo<gao.bo168@gmail.com>
  3268. * @version 2015-3-25 下午1:24:06
  3269. * @copyright Show More
  3270. */
  3271. public function applyShakeAroundDevice($data){
  3272. if (!$this->access_token && !$this->checkAuth()) return false;
  3273. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_APPLYID . 'access_token=' . $this->access_token, self::json_encode($data));
  3274. $this->log($result);
  3275. if ($result) {
  3276. $json = json_decode($result, true);
  3277. if (!$json || !empty($json['errcode'])) {
  3278. $this->errCode = $json['errcode'];
  3279. $this->errMsg = $json['errmsg'];
  3280. return false;
  3281. }
  3282. return $json;
  3283. }
  3284. return false;
  3285. }
  3286. /**
  3287. * 编辑设备信息
  3288. * [updateShakeAroundDevice 编辑设备的备注信息。可用设备ID或完整的UUID、Major、Minor指定设备,二者选其一。]
  3289. * @param array $data
  3290. * array(
  3291. * "device_identifier" => array(
  3292. * "device_id" => 10011, //当提供了device_id则不需要使用uuid、major、minor,反之亦然
  3293. * "uuid" => "FDA50693-A4E2-4FB1-AFCF-C6EB07647825",
  3294. * "major" => 1002,
  3295. * "minor" => 1223
  3296. * ),
  3297. * "comment" => "测试专用", //备注(非必填)
  3298. * )
  3299. * {
  3300. "data": {
  3301. },
  3302. "errcode": 0,
  3303. "errmsg": "success."
  3304. }
  3305. * @return boolean
  3306. * @author binsee<binsee@163.com>
  3307. * @version 2015-4-20 23:45:00
  3308. */
  3309. public function updateShakeAroundDevice($data){
  3310. if (!$this->access_token && !$this->checkAuth()) return false;
  3311. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3312. $this->log($result);
  3313. if ($result) {
  3314. $json = json_decode($result, true);
  3315. if (!$json || !empty($json['errcode'])) {
  3316. $this->errCode = $json['errcode'];
  3317. $this->errMsg = $json['errmsg'];
  3318. return false;
  3319. }
  3320. return true;
  3321. }
  3322. return false;
  3323. }
  3324. /**
  3325. * 查询设备列表
  3326. * [searchShakeAroundDevice 查询已有的设备ID、UUID、Major、Minor、激活状态、备注信息、关联门店、关联页面等信息。
  3327. * 可指定设备ID 或完整的UUID、Major、Minor 查询,也可批量拉取设备信息列表。]
  3328. * @param array $data
  3329. * $data 三种格式:
  3330. * ①查询指定设备时:$data = array(
  3331. * "device_identifiers" => array(
  3332. * array(
  3333. * "device_id" => 10100,
  3334. * "uuid" => "FDA50693-A4E2-4FB1-AFCF-C6EB07647825",
  3335. * "major" => 10001,
  3336. * "minor" => 10002
  3337. * )
  3338. * )
  3339. * );
  3340. * device_identifiers:指定的设备
  3341. * device_id:设备编号,若填了UUID、major、minor,则可不填设备编号,若二者都填,则以设备编号为优先
  3342. * uuid、major、minor:三个信息需填写完整,若填了设备编号,则可不填此信息
  3343. * +-------------------------------------------------------------------------------------------------------------
  3344. * ②需要分页查询或者指定范围内的设备时: $data = array(
  3345. * "begin" => 0,
  3346. * "count" => 3
  3347. * );
  3348. * begin:设备列表的起始索引值
  3349. * count:待查询的设备个数
  3350. * +-------------------------------------------------------------------------------------------------------------
  3351. * ③当需要根据批次ID 查询时: $data = array(
  3352. * "apply_id" => 1231,
  3353. * "begin" => 0,
  3354. * "count" => 3
  3355. * );
  3356. * apply_id:批次ID
  3357. * +-------------------------------------------------------------------------------------------------------------
  3358. * @return boolean|mixed
  3359. *正确迒回JSON 数据示例:
  3360. *字段说明
  3361. {
  3362. "data": {
  3363. "devices": [ //指定的设备信息列表
  3364. {
  3365. "comment": "", //设备的备注信息
  3366. "device_id": 10097, //设备编号
  3367. "major": 10001,
  3368. "minor": 12102,
  3369. "page_ids": "15369", //与此设备关联的页面ID 列表,用逗号隔开
  3370. "status": 1, //激活状态,0:未激活,1:已激活(但不活跃),2:活跃
  3371. "poi_id": 0, //门店ID
  3372. "uuid": "FDA50693-A4E2-4FB1-AFCF-C6EB07647825"
  3373. },
  3374. {
  3375. "comment": "", //设备的备注信息
  3376. "device_id": 10098, //设备编号
  3377. "major": 10001,
  3378. "minor": 12103,
  3379. "page_ids": "15368", //与此设备关联的页面ID 列表,用逗号隔开
  3380. "status": 1, //激活状态,0:未激活,1:已激活(但不活跃),2:活跃
  3381. "poi_id": 0, //门店ID
  3382. "uuid": "FDA50693-A4E2-4FB1-AFCF-C6EB07647825"
  3383. }
  3384. ],
  3385. "total_count": 151 //商户名下的设备总量
  3386. },
  3387. "errcode": 0,
  3388. "errmsg": "success."
  3389. }
  3390. * @access public
  3391. * @author polo<gao.bo168@gmail.com>
  3392. * @version 2015-3-25 下午1:45:42
  3393. * @copyright Show More
  3394. */
  3395. public function searchShakeAroundDevice($data){
  3396. if (!$this->access_token && !$this->checkAuth()) return false;
  3397. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_SEARCH . 'access_token=' . $this->access_token, self::json_encode($data));
  3398. $this->log($result);
  3399. if ($result) {
  3400. $json = json_decode($result, true);
  3401. if (!$json || !empty($json['errcode'])) {
  3402. $this->errCode = $json['errcode'];
  3403. $this->errMsg = $json['errmsg'];
  3404. return false;
  3405. }
  3406. return $json;
  3407. }
  3408. return false;
  3409. }
  3410. /**
  3411. * [bindLocationShakeAroundDevice 配置设备与门店的关联关系]
  3412. * @param string $device_id 设备编号,若填了UUID、major、minor,则可不填设备编号,若二者都填,则以设备编号为优先
  3413. * @param int $poi_id 待关联的门店ID
  3414. * @param string $uuid UUID、major、minor,三个信息需填写完整,若填了设备编号,则可不填此信息
  3415. * @param int $major
  3416. * @param int $minor
  3417. * @return boolean|mixed
  3418. * 正确返回JSON 数据示例:
  3419. * {
  3420. "data": {
  3421. },
  3422. "errcode": 0,
  3423. "errmsg": "success."
  3424. }
  3425. * @access public
  3426. * @author polo<gao.bo168@gmail.com>
  3427. * @version 2015-4-21 00:14:00
  3428. * @copyright Show More
  3429. */
  3430. public function bindLocationShakeAroundDevice($device_id,$poi_id,$uuid='',$major=0,$minor=0){
  3431. if (!$this->access_token && !$this->checkAuth()) return false;
  3432. if(!$device_id){
  3433. if(!$uuid || !$major || !$minor){
  3434. return false;
  3435. }
  3436. $device_identifier = array(
  3437. 'uuid' => $uuid,
  3438. 'major' => $major,
  3439. 'minor' => $minor
  3440. );
  3441. }else{
  3442. $device_identifier = array(
  3443. 'device_id' => $device_id
  3444. );
  3445. }
  3446. $data = array(
  3447. 'device_identifier' => $device_identifier,
  3448. 'poi_id' => $poi_id
  3449. );
  3450. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_BINDLOCATION . 'access_token=' . $this->access_token, self::json_encode($data));
  3451. $this->log($result);
  3452. if ($result) {
  3453. $json = json_decode($result, true);
  3454. if (!$json || !empty($json['errcode'])) {
  3455. $this->errCode = $json['errcode'];
  3456. $this->errMsg = $json['errmsg'];
  3457. return false;
  3458. }
  3459. return $json; //这个可以更改为返回true
  3460. }
  3461. return false;
  3462. }
  3463. /**
  3464. * [bindPageShakeAroundDevice 配置设备与页面的关联关系。
  3465. * 支持建立或解除关联关系,也支持新增页面或覆盖页面等操作。
  3466. * 配置完成后,在此设备的信号范围内,即可摇出关联的页面信息。
  3467. * 若设备配置多个页面,则随机出现页面信息]
  3468. * @param string $device_id 设备编号,若填了UUID、major、minor,则可不填设备编号,若二者都填,则以设备编号为优先
  3469. * @param array $page_ids 待关联的页面列表
  3470. * @param number $bind 关联操作标志位, 0 为解除关联关系,1 为建立关联关系
  3471. * @param number $append 新增操作标志位, 0 为覆盖,1 为新增
  3472. * @param string $uuid UUID、major、minor,三个信息需填写完整,若填了设备编号,则可不填此信息
  3473. * @param int $major
  3474. * @param int $minor
  3475. * @return boolean|mixed
  3476. * 正确返回JSON 数据示例:
  3477. * {
  3478. "data": {
  3479. },
  3480. "errcode": 0,
  3481. "errmsg": "success."
  3482. }
  3483. * @access public
  3484. * @author polo<gao.bo168@gmail.com>
  3485. * @version 2015-4-21 00:31:00
  3486. * @copyright Show More
  3487. */
  3488. public function bindPageShakeAroundDevice($device_id,$page_ids=array(),$bind=1,$append=1,$uuid='',$major=0,$minor=0){
  3489. if (!$this->access_token && !$this->checkAuth()) return false;
  3490. if(!$device_id){
  3491. if(!$uuid || !$major || !$minor){
  3492. return false;
  3493. }
  3494. $device_identifier = array(
  3495. 'uuid' => $uuid,
  3496. 'major' => $major,
  3497. 'minor' => $minor
  3498. );
  3499. }else{
  3500. $device_identifier = array(
  3501. 'device_id' => $device_id
  3502. );
  3503. }
  3504. $data = array(
  3505. 'device_identifier' => $device_identifier,
  3506. 'page_ids' => $page_ids,
  3507. 'bind' => $bind,
  3508. 'append' => $append
  3509. );
  3510. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_BINDPAGE . 'access_token=' . $this->access_token, self::json_encode($data));
  3511. $this->log($result);
  3512. if ($result) {
  3513. $json = json_decode($result, true);
  3514. if (!$json || !empty($json['errcode'])) {
  3515. $this->errCode = $json['errcode'];
  3516. $this->errMsg = $json['errmsg'];
  3517. return false;
  3518. }
  3519. return $json;
  3520. }
  3521. return false;
  3522. }
  3523. /**
  3524. * 上传在摇一摇页面展示的图片素材
  3525. * 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
  3526. * @param array $data {"media":'@Path\filename.jpg'} 格式限定为:jpg,jpeg,png,gif,图片大小建议120px*120 px,限制不超过200 px *200 px,图片需为正方形。
  3527. * @return boolean|array
  3528. * {
  3529. "data": {
  3530. "pic_url":"http://shp.qpic.cn/wechat_shakearound_pic/0/1428377032e9dd2797018cad79186e03e8c5aec8dc/120"
  3531. },
  3532. "errcode": 0,
  3533. "errmsg": "success."
  3534. }
  3535. }
  3536. * @author binsee<binsee@163.com>
  3537. * @version 2015-4-21 00:51:00
  3538. */
  3539. public function uploadShakeAroundMedia($data){
  3540. if (!$this->access_token && !$this->checkAuth()) return false;
  3541. $result = $this->http_post(self::API_URL_PREFIX.self::SHAKEAROUND_MATERIAL_ADD.'access_token='.$this->access_token,$data,true);
  3542. if ($result)
  3543. {
  3544. $json = json_decode($result,true);
  3545. if (!$json || !empty($json['errcode'])) {
  3546. $this->errCode = $json['errcode'];
  3547. $this->errMsg = $json['errmsg'];
  3548. return false;
  3549. }
  3550. return $json;
  3551. }
  3552. return false;
  3553. }
  3554. /**
  3555. * [addShakeAroundPage 增加摇一摇出来的页面信息,包括在摇一摇页面出现的主标题、副标题、图片和点击进去的超链接。]
  3556. * @param string $title 在摇一摇页面展示的主标题,不超过6 个字
  3557. * @param string $description 在摇一摇页面展示的副标题,不超过7 个字
  3558. * @param sting $icon_url 在摇一摇页面展示的图片, 格式限定为:jpg,jpeg,png,gif; 建议120*120 , 限制不超过200*200
  3559. * @param string $page_url 跳转链接
  3560. * @param string $comment 页面的备注信息,不超过15 个字,可不填
  3561. * @return boolean|mixed
  3562. * 正确返回JSON 数据示例:
  3563. * {
  3564. "data": {
  3565. "page_id": 28840 //新增页面的页面id
  3566. }
  3567. "errcode": 0,
  3568. "errmsg": "success."
  3569. }
  3570. * @access public
  3571. * @author polo<gao.bo168@gmail.com>
  3572. * @version 2015-3-25 下午2:57:09
  3573. * @copyright Show More
  3574. */
  3575. public function addShakeAroundPage($title,$description,$icon_url,$page_url,$comment=''){
  3576. if (!$this->access_token && !$this->checkAuth()) return false;
  3577. $data = array(
  3578. "title" => $title,
  3579. "description" => $description,
  3580. "icon_url" => $icon_url,
  3581. "page_url" => $page_url,
  3582. "comment" => $comment
  3583. );
  3584. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_PAGE_ADD . 'access_token=' . $this->access_token, self::json_encode($data));
  3585. $this->log($result);
  3586. if ($result) {
  3587. $json = json_decode($result, true);
  3588. if (!$json || !empty($json['errcode'])) {
  3589. $this->errCode = $json['errcode'];
  3590. $this->errMsg = $json['errmsg'];
  3591. return false;
  3592. }
  3593. return $json;
  3594. }
  3595. return false;
  3596. }
  3597. /**
  3598. * [updateShakeAroundPage 编辑摇一摇出来的页面信息,包括在摇一摇页面出现的主标题、副标题、图片和点击进去的超链接。]
  3599. * @param int $page_id
  3600. * @param string $title 在摇一摇页面展示的主标题,不超过6 个字
  3601. * @param string $description 在摇一摇页面展示的副标题,不超过7 个字
  3602. * @param sting $icon_url 在摇一摇页面展示的图片, 格式限定为:jpg,jpeg,png,gif; 建议120*120 , 限制不超过200*200
  3603. * @param string $page_url 跳转链接
  3604. * @param string $comment 页面的备注信息,不超过15 个字,可不填
  3605. * @return boolean|mixed
  3606. * 正确返回JSON 数据示例:
  3607. * {
  3608. "data": {
  3609. "page_id": 28840 //编辑页面的页面ID
  3610. }
  3611. "errcode": 0,
  3612. "errmsg": "success."
  3613. }
  3614. * @access public
  3615. * @author polo<gao.bo168@gmail.com>
  3616. * @version 2015-3-25 下午3:02:51
  3617. * @copyright Show More
  3618. */
  3619. public function updateShakeAroundPage($page_id,$title,$description,$icon_url,$page_url,$comment=''){
  3620. if (!$this->access_token && !$this->checkAuth()) return false;
  3621. $data = array(
  3622. "page_id" => $page_id,
  3623. "title" => $title,
  3624. "description" => $description,
  3625. "icon_url" => $icon_url,
  3626. "page_url" => $page_url,
  3627. "comment" => $comment
  3628. );
  3629. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_PAGE_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3630. $this->log($result);
  3631. if ($result) {
  3632. $json = json_decode($result, true);
  3633. if (!$json || !empty($json['errcode'])) {
  3634. $this->errCode = $json['errcode'];
  3635. $this->errMsg = $json['errmsg'];
  3636. return false;
  3637. }
  3638. return $json;
  3639. }
  3640. return false;
  3641. }
  3642. /**
  3643. * [searchShakeAroundPage 查询已有的页面,包括在摇一摇页面出现的主标题、副标题、图片和点击进去的超链接。
  3644. * 提供两种查询方式,①可指定页面ID 查询,②也可批量拉取页面列表。]
  3645. * @param array $page_ids
  3646. * @param int $begin
  3647. * @param int $count
  3648. * ①需要查询指定页面时:
  3649. * {
  3650. "page_ids":[12345, 23456, 34567]
  3651. }
  3652. * +-------------------------------------------------------------------------------------------------------------
  3653. * ②需要分页查询或者指定范围内的页面时:
  3654. * {
  3655. "begin": 0,
  3656. "count": 3
  3657. }
  3658. * +-------------------------------------------------------------------------------------------------------------
  3659. * @return boolean|mixed
  3660. * 正确返回JSON 数据示例:
  3661. {
  3662. "data": {
  3663. "pages": [
  3664. {
  3665. "comment": "just for test",
  3666. "description": "test",
  3667. "icon_url": "https://www.baidu.com/img/bd_logo1.png",
  3668. "page_id": 28840,
  3669. "page_url": "http://xw.qq.com/testapi1",
  3670. "title": "测试1"
  3671. },
  3672. {
  3673. "comment": "just for test",
  3674. "description": "test",
  3675. "icon_url": "https://www.baidu.com/img/bd_logo1.png",
  3676. "page_id": 28842,
  3677. "page_url": "http://xw.qq.com/testapi2",
  3678. "title": "测试2"
  3679. }
  3680. ],
  3681. "total_count": 2
  3682. },
  3683. "errcode": 0,
  3684. "errmsg": "success."
  3685. }
  3686. *字段说明:
  3687. *total_count 商户名下的页面总数
  3688. *page_id 摇周边页面唯一ID
  3689. *title 在摇一摇页面展示的主标题
  3690. *description 在摇一摇页面展示的副标题
  3691. *icon_url 在摇一摇页面展示的图片
  3692. *page_url 跳转链接
  3693. *comment 页面的备注信息
  3694. * @access public
  3695. * @author polo<gao.bo168@gmail.com>
  3696. * @version 2015-3-25 下午3:12:17
  3697. * @copyright Show More
  3698. */
  3699. public function searchShakeAroundPage($page_ids=array(),$begin=0,$count=1){
  3700. if (!$this->access_token && !$this->checkAuth()) return false;
  3701. if(!empty($page_ids)){
  3702. $data = array(
  3703. 'page_ids' => $page_ids
  3704. );
  3705. }else{
  3706. $data = array(
  3707. 'begin' => $begin,
  3708. 'count' => $count
  3709. );
  3710. }
  3711. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_PAGE_SEARCH . 'access_token=' . $this->access_token, self::json_encode($data));
  3712. $this->log($result);
  3713. if ($result) {
  3714. $json = json_decode($result, true);
  3715. if (!$json || !empty($json['errcode'])) {
  3716. $this->errCode = $json['errcode'];
  3717. $this->errMsg = $json['errmsg'];
  3718. return false;
  3719. }
  3720. return $json;
  3721. }
  3722. return false;
  3723. }
  3724. /**
  3725. * [deleteShakeAroundPage 删除已有的页面,包括在摇一摇页面出现的主标题、副标题、图片和点击进去的超链接。
  3726. * 只有页面与设备没有关联关系时,才可被删除。]
  3727. * @param array $page_ids
  3728. * {
  3729. "page_ids":[12345,23456,34567]
  3730. }
  3731. * @return boolean|mixed
  3732. * 正确返回JSON 数据示例:
  3733. * {
  3734. "data": {
  3735. },
  3736. "errcode": 0,
  3737. "errmsg": "success."
  3738. }
  3739. * @access public
  3740. * @author polo<gao.bo168@gmail.com>
  3741. * @version 2015-3-25 下午3:23:00
  3742. * @copyright Show More
  3743. */
  3744. public function deleteShakeAroundPage($page_ids=array()){
  3745. if (!$this->access_token && !$this->checkAuth()) return false;
  3746. $data = array(
  3747. 'page_ids' => $page_ids
  3748. );
  3749. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_PAGE_DELETE . 'access_token=' . $this->access_token, self::json_encode($data));
  3750. $this->log($result);
  3751. if ($result) {
  3752. $json = json_decode($result, true);
  3753. if (!$json || !empty($json['errcode'])) {
  3754. $this->errCode = $json['errcode'];
  3755. $this->errMsg = $json['errmsg'];
  3756. return false;
  3757. }
  3758. return $json;
  3759. }
  3760. return false;
  3761. }
  3762. /**
  3763. * [getShakeInfoShakeAroundUser 获取设备信息,包括UUID、major、minor,以及距离、openID 等信息。]
  3764. * @param string $ticket 摇周边业务的ticket,可在摇到的URL 中得到,ticket生效时间为30 分钟
  3765. * @return boolean|mixed
  3766. * 正确返回JSON 数据示例:
  3767. * {
  3768. "data": {
  3769. "page_id ": 14211,
  3770. "beacon_info": {
  3771. "distance": 55.00620700469034,
  3772. "major": 10001,
  3773. "minor": 19007,
  3774. "uuid": "FDA50693-A4E2-4FB1-AFCF-C6EB07647825"
  3775. },
  3776. "openid": "oVDmXjp7y8aG2AlBuRpMZTb1-cmA"
  3777. },
  3778. "errcode": 0,
  3779. "errmsg": "success."
  3780. }
  3781. * 字段说明:
  3782. * beacon_info 设备信息,包括UUID、major、minor,以及距离
  3783. * UUID、major、minor UUID、major、minor
  3784. * distance Beacon 信号与手机的距离
  3785. * page_id 摇周边页面唯一ID
  3786. * openid 商户AppID 下用户的唯一标识
  3787. * poi_id 门店ID,有的话则返回,没有的话不会在JSON 格式内
  3788. * @access public
  3789. * @author polo<gao.bo168@gmail.com>
  3790. * @version 2015-3-25 下午3:28:20
  3791. * @copyright Show More
  3792. */
  3793. public function getShakeInfoShakeAroundUser($ticket){
  3794. if (!$this->access_token && !$this->checkAuth()) return false;
  3795. $data = array('ticket' => $ticket);
  3796. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_USER_GETSHAKEINFO . 'access_token=' . $this->access_token, self::json_encode($data));
  3797. $this->log($result);
  3798. if ($result) {
  3799. $json = json_decode($result, true);
  3800. if (!$json || !empty($json['errcode'])) {
  3801. $this->errCode = $json['errcode'];
  3802. $this->errMsg = $json['errmsg'];
  3803. return false;
  3804. }
  3805. return $json;
  3806. }
  3807. return false;
  3808. }
  3809. /**
  3810. * [deviceShakeAroundStatistics 以设备为维度的数据统计接口。
  3811. * 查询单个设备进行摇周边操作的人数、次数,点击摇周边消息的人数、次数;查询的最长时间跨度为30天。]
  3812. * @param int $device_id 设备编号,若填了UUID、major、minor,即可不填设备编号,二者选其一
  3813. * @param int $begin_date 起始日期时间戳,最长时间跨度为30 天
  3814. * @param int $end_date 结束日期时间戳,最长时间跨度为30 天
  3815. * @param string $uuid UUID、major、minor,三个信息需填写完成,若填了设备编辑,即可不填此信息,二者选其一
  3816. * @param int $major
  3817. * @param int $minor
  3818. * @return boolean|mixed
  3819. * 正确返回JSON 数据示例:
  3820. * {
  3821. "data": [
  3822. {
  3823. "click_pv": 0,
  3824. "click_uv": 0,
  3825. "ftime": 1425052800,
  3826. "shake_pv": 0,
  3827. "shake_uv": 0
  3828. },
  3829. {
  3830. "click_pv": 0,
  3831. "click_uv": 0,
  3832. "ftime": 1425139200,
  3833. "shake_pv": 0,
  3834. "shake_uv": 0
  3835. }
  3836. ],
  3837. "errcode": 0,
  3838. "errmsg": "success."
  3839. }
  3840. * 字段说明:
  3841. * ftime 当天0 点对应的时间戳
  3842. * click_pv 点击摇周边消息的次数
  3843. * click_uv 点击摇周边消息的人数
  3844. * shake_pv 摇周边的次数
  3845. * shake_uv 摇周边的人数
  3846. * @access public
  3847. * @author polo<gao.bo168@gmail.com>
  3848. * @version 2015-4-21 00:39:00
  3849. * @copyright Show More
  3850. */
  3851. public function deviceShakeAroundStatistics($device_id,$begin_date,$end_date,$uuid='',$major=0,$minor=0){
  3852. if (!$this->access_token && !$this->checkAuth()) return false;
  3853. if(!$device_id){
  3854. if(!$uuid || !$major || !$minor){
  3855. return false;
  3856. }
  3857. $device_identifier = array(
  3858. 'uuid' => $uuid,
  3859. 'major' => $major,
  3860. 'minor' => $minor
  3861. );
  3862. }else{
  3863. $device_identifier = array(
  3864. 'device_id' => $device_id
  3865. );
  3866. }
  3867. $data = array(
  3868. 'device_identifier' => $device_identifier,
  3869. 'begin_date' => $begin_date,
  3870. 'end_date' => $end_date
  3871. );
  3872. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_STATISTICS_DEVICE . 'access_token=' . $this->access_token, self::json_encode($data));
  3873. $this->log($result);
  3874. if ($result) {
  3875. $json = json_decode($result, true);
  3876. if (!$json || !empty($json['errcode'])) {
  3877. $this->errCode = $json['errcode'];
  3878. $this->errMsg = $json['errmsg'];
  3879. return false;
  3880. }
  3881. return $json;
  3882. }
  3883. return false;
  3884. }
  3885. /**
  3886. * [pageShakeAroundStatistics 以页面为维度的数据统计接口。
  3887. * 查询单个页面通过摇周边摇出来的人数、次数,点击摇周边页面的人数、次数;查询的最长时间跨度为30天。]
  3888. * @param int $page_id 指定页面的ID
  3889. * @param int $begin_date 起始日期时间戳,最长时间跨度为30 天
  3890. * @param int $end_date 结束日期时间戳,最长时间跨度为30 天
  3891. * @return boolean|mixed
  3892. * 正确返回JSON 数据示例:
  3893. * {
  3894. "data": [
  3895. {
  3896. "click_pv": 0,
  3897. "click_uv": 0,
  3898. "ftime": 1425052800,
  3899. "shake_pv": 0,
  3900. "shake_uv": 0
  3901. },
  3902. {
  3903. "click_pv": 0,
  3904. "click_uv": 0,
  3905. "ftime": 1425139200,
  3906. "shake_pv": 0,
  3907. "shake_uv": 0
  3908. }
  3909. ],
  3910. "errcode": 0,
  3911. "errmsg": "success."
  3912. }
  3913. * 字段说明:
  3914. * ftime 当天0 点对应的时间戳
  3915. * click_pv 点击摇周边消息的次数
  3916. * click_uv 点击摇周边消息的人数
  3917. * shake_pv 摇周边的次数
  3918. * shake_uv 摇周边的人数
  3919. * @author binsee<binsee@163.com>
  3920. * @version 2015-4-21 00:43:00
  3921. */
  3922. public function pageShakeAroundStatistics($page_id,$begin_date,$end_date){
  3923. if (!$this->access_token && !$this->checkAuth()) return false;
  3924. $data = array(
  3925. 'page_id' => $page_id,
  3926. 'begin_date' => $begin_date,
  3927. 'end_date' => $end_date
  3928. );
  3929. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_STATISTICS_DEVICE . 'access_token=' . $this->access_token, self::json_encode($data));
  3930. $this->log($result);
  3931. if ($result) {
  3932. $json = json_decode($result, true);
  3933. if (!$json || !empty($json['errcode'])) {
  3934. $this->errCode = $json['errcode'];
  3935. $this->errMsg = $json['errmsg'];
  3936. return false;
  3937. }
  3938. return $json;
  3939. }
  3940. return false;
  3941. }
  3942. }
  3943. /**
  3944. * PKCS7Encoder class
  3945. *
  3946. * 提供基于PKCS7算法的加解密接口.
  3947. */
  3948. class PKCS7Encoder
  3949. {
  3950. public static $block_size = 32;
  3951. /**
  3952. * 对需要加密的明文进行填充补位
  3953. * @param $text 需要进行填充补位操作的明文
  3954. * @return 补齐明文字符串
  3955. */
  3956. function encode($text)
  3957. {
  3958. $block_size = PKCS7Encoder::$block_size;
  3959. $text_length = strlen($text);
  3960. //计算需要填充的位数
  3961. $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
  3962. if ($amount_to_pad == 0) {
  3963. $amount_to_pad = PKCS7Encoder::block_size;
  3964. }
  3965. //获得补位所用的字符
  3966. $pad_chr = chr($amount_to_pad);
  3967. $tmp = "";
  3968. for ($index = 0; $index < $amount_to_pad; $index++) {
  3969. $tmp .= $pad_chr;
  3970. }
  3971. return $text . $tmp;
  3972. }
  3973. /**
  3974. * 对解密后的明文进行补位删除
  3975. * @param decrypted 解密后的明文
  3976. * @return 删除填充补位后的明文
  3977. */
  3978. function decode($text)
  3979. {
  3980. $pad = ord(substr($text, -1));
  3981. if ($pad < 1 || $pad > PKCS7Encoder::$block_size) {
  3982. $pad = 0;
  3983. }
  3984. return substr($text, 0, (strlen($text) - $pad));
  3985. }
  3986. }
  3987. /**
  3988. * Prpcrypt class
  3989. *
  3990. * 提供接收和推送给公众平台消息的加解密接口.
  3991. */
  3992. class Prpcrypt
  3993. {
  3994. public $key;
  3995. function __construct($k) {
  3996. $this->key = base64_decode($k . "=");
  3997. }
  3998. /**
  3999. * 兼容老版本php构造函数,不能在 __construct() 方法前边,否则报错
  4000. */
  4001. function Prpcrypt($k)
  4002. {
  4003. $this->key = base64_decode($k . "=");
  4004. }
  4005. /**
  4006. * 对明文进行加密
  4007. * @param string $text 需要加密的明文
  4008. * @return string 加密后的密文
  4009. */
  4010. public function encrypt($text, $appid)
  4011. {
  4012. try {
  4013. //获得16位随机字符串,填充到明文之前
  4014. $random = $this->getRandomStr();//"aaaabbbbccccdddd";
  4015. $text = $random . pack("N", strlen($text)) . $text . $appid;
  4016. // 网络字节序
  4017. $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  4018. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  4019. $iv = substr($this->key, 0, 16);
  4020. //使用自定义的填充方式对明文进行补位填充
  4021. $pkc_encoder = new PKCS7Encoder;
  4022. $text = $pkc_encoder->encode($text);
  4023. mcrypt_generic_init($module, $this->key, $iv);
  4024. //加密
  4025. $encrypted = mcrypt_generic($module, $text);
  4026. mcrypt_generic_deinit($module);
  4027. mcrypt_module_close($module);
  4028. // print(base64_encode($encrypted));
  4029. //使用BASE64对加密后的字符串进行编码
  4030. return array(ErrorCode::$OK, base64_encode($encrypted));
  4031. } catch (Exception $e) {
  4032. //print $e;
  4033. return array(ErrorCode::$EncryptAESError, null);
  4034. }
  4035. }
  4036. /**
  4037. * 对密文进行解密
  4038. * @param string $encrypted 需要解密的密文
  4039. * @return string 解密得到的明文
  4040. */
  4041. public function decrypt($encrypted, $appid)
  4042. {
  4043. try {
  4044. //使用BASE64对需要解密的字符串进行解码
  4045. $ciphertext_dec = base64_decode($encrypted);
  4046. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  4047. $iv = substr($this->key, 0, 16);
  4048. mcrypt_generic_init($module, $this->key, $iv);
  4049. //解密
  4050. $decrypted = mdecrypt_generic($module, $ciphertext_dec);
  4051. mcrypt_generic_deinit($module);
  4052. mcrypt_module_close($module);
  4053. } catch (Exception $e) {
  4054. return array(ErrorCode::$DecryptAESError, null);
  4055. }
  4056. try {
  4057. //去除补位字符
  4058. $pkc_encoder = new PKCS7Encoder;
  4059. $result = $pkc_encoder->decode($decrypted);
  4060. //去除16位随机字符串,网络字节序和AppId
  4061. if (strlen($result) < 16)
  4062. return "";
  4063. $content = substr($result, 16, strlen($result));
  4064. $len_list = unpack("N", substr($content, 0, 4));
  4065. $xml_len = $len_list[1];
  4066. $xml_content = substr($content, 4, $xml_len);
  4067. $from_appid = substr($content, $xml_len + 4);
  4068. if (!$appid)
  4069. $appid = $from_appid;
  4070. //如果传入的appid是空的,则认为是订阅号,使用数据中提取出来的appid
  4071. } catch (Exception $e) {
  4072. //print $e;
  4073. return array(ErrorCode::$IllegalBuffer, null);
  4074. }
  4075. if ($from_appid != $appid)
  4076. return array(ErrorCode::$ValidateAppidError, null);
  4077. //不注释上边两行,避免传入appid是错误的情况
  4078. return array(0, $xml_content, $from_appid); //增加appid,为了解决后面加密回复消息的时候没有appid的订阅号会无法回复
  4079. }
  4080. /**
  4081. * 随机生成16位字符串
  4082. * @return string 生成的字符串
  4083. */
  4084. function getRandomStr()
  4085. {
  4086. $str = "";
  4087. $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  4088. $max = strlen($str_pol) - 1;
  4089. for ($i = 0; $i < 16; $i++) {
  4090. $str .= $str_pol[mt_rand(0, $max)];
  4091. }
  4092. return $str;
  4093. }
  4094. }
  4095. /**
  4096. * error code
  4097. * 仅用作类内部使用,不用于官方API接口的errCode码
  4098. */
  4099. class ErrorCode
  4100. {
  4101. public static $OK = 0;
  4102. public static $ValidateSignatureError = 40001;
  4103. public static $ParseXmlError = 40002;
  4104. public static $ComputeSignatureError = 40003;
  4105. public static $IllegalAesKey = 40004;
  4106. public static $ValidateAppidError = 40005;
  4107. public static $EncryptAESError = 40006;
  4108. public static $DecryptAESError = 40007;
  4109. public static $IllegalBuffer = 40008;
  4110. public static $EncodeBase64Error = 40009;
  4111. public static $DecodeBase64Error = 40010;
  4112. public static $GenReturnXmlError = 40011;
  4113. public static $errCode=array(
  4114. '0' => '处理成功',
  4115. '40001' => '校验签名失败',
  4116. '40002' => '解析xml失败',
  4117. '40003' => '计算签名失败',
  4118. '40004' => '不合法的AESKey',
  4119. '40005' => '校验AppID失败',
  4120. '40006' => 'AES加密失败',
  4121. '40007' => 'AES解密失败',
  4122. '40008' => '公众平台发送的xml不合法',
  4123. '40009' => 'Base64编码失败',
  4124. '40010' => 'Base64解码失败',
  4125. '40011' => '公众帐号生成回包xml失败'
  4126. );
  4127. public static function getErrText($err) {
  4128. if (isset(self::$errCode[$err])) {
  4129. return self::$errCode[$err];
  4130. }else {
  4131. return false;
  4132. };
  4133. }
  4134. }