wechat.class.php 165 KB

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