wechat.class.php 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776
  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. if(PHP_VERSION_ID >= 50500){
  1091. curl_setopt($oCurl, CURLOPT_SAFE_UPLOAD, FALSE);
  1092. }
  1093. $sContent = curl_exec($oCurl);
  1094. $aStatus = curl_getinfo($oCurl);
  1095. curl_close($oCurl);
  1096. if(intval($aStatus["http_code"])==200){
  1097. return $sContent;
  1098. }else{
  1099. return false;
  1100. }
  1101. }
  1102. /**
  1103. * 设置缓存,按需重载
  1104. * @param string $cachename
  1105. * @param mixed $value
  1106. * @param int $expired
  1107. * @return boolean
  1108. */
  1109. protected function setCache($cachename,$value,$expired){
  1110. //TODO: set cache implementation
  1111. return false;
  1112. }
  1113. /**
  1114. * 获取缓存,按需重载
  1115. * @param string $cachename
  1116. * @return mixed
  1117. */
  1118. protected function getCache($cachename){
  1119. //TODO: get cache implementation
  1120. return false;
  1121. }
  1122. /**
  1123. * 清除缓存,按需重载
  1124. * @param string $cachename
  1125. * @return boolean
  1126. */
  1127. protected function removeCache($cachename){
  1128. //TODO: remove cache implementation
  1129. return false;
  1130. }
  1131. /**
  1132. * 获取access_token
  1133. * @param string $appid 如在类初始化时已提供,则可为空
  1134. * @param string $appsecret 如在类初始化时已提供,则可为空
  1135. * @param string $token 手动指定access_token,非必要情况不建议用
  1136. */
  1137. public function checkAuth($appid='',$appsecret='',$token=''){
  1138. if (!$appid || !$appsecret) {
  1139. $appid = $this->appid;
  1140. $appsecret = $this->appsecret;
  1141. }
  1142. if ($token) { //手动指定token,优先使用
  1143. $this->access_token=$token;
  1144. return $this->access_token;
  1145. }
  1146. $authname = 'wechat_access_token'.$appid;
  1147. if ($rs = $this->getCache($authname)) {
  1148. $this->access_token = $rs;
  1149. return $rs;
  1150. }
  1151. $result = $this->http_get(self::API_URL_PREFIX.self::AUTH_URL.'appid='.$appid.'&secret='.$appsecret);
  1152. if ($result)
  1153. {
  1154. $json = json_decode($result,true);
  1155. if (!$json || isset($json['errcode'])) {
  1156. $this->errCode = $json['errcode'];
  1157. $this->errMsg = $json['errmsg'];
  1158. return false;
  1159. }
  1160. $this->access_token = $json['access_token'];
  1161. $expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
  1162. $this->setCache($authname,$this->access_token,$expire);
  1163. return $this->access_token;
  1164. }
  1165. return false;
  1166. }
  1167. /**
  1168. * 删除验证数据
  1169. * @param string $appid
  1170. */
  1171. public function resetAuth($appid=''){
  1172. if (!$appid) $appid = $this->appid;
  1173. $this->access_token = '';
  1174. $authname = 'wechat_access_token'.$appid;
  1175. $this->removeCache($authname);
  1176. return true;
  1177. }
  1178. /**
  1179. * 删除JSAPI授权TICKET
  1180. * @param string $appid 用于多个appid时使用
  1181. */
  1182. public function resetJsTicket($appid=''){
  1183. if (!$appid) $appid = $this->appid;
  1184. $this->jsapi_ticket = '';
  1185. $authname = 'wechat_jsapi_ticket'.$appid;
  1186. $this->removeCache($authname);
  1187. return true;
  1188. }
  1189. /**
  1190. * 获取JSAPI授权TICKET
  1191. * @param string $appid 用于多个appid时使用,可空
  1192. * @param string $jsapi_ticket 手动指定jsapi_ticket,非必要情况不建议用
  1193. */
  1194. public function getJsTicket($appid='',$jsapi_ticket=''){
  1195. if (!$this->access_token && !$this->checkAuth()) return false;
  1196. if (!$appid) $appid = $this->appid;
  1197. if ($jsapi_ticket) { //手动指定token,优先使用
  1198. $this->jsapi_ticket = $jsapi_ticket;
  1199. return $this->jsapi_ticket;
  1200. }
  1201. $authname = 'wechat_jsapi_ticket'.$appid;
  1202. if ($rs = $this->getCache($authname)) {
  1203. $this->jsapi_ticket = $rs;
  1204. return $rs;
  1205. }
  1206. $result = $this->http_get(self::API_URL_PREFIX.self::GET_TICKET_URL.'access_token='.$this->access_token.'&type=jsapi');
  1207. if ($result)
  1208. {
  1209. $json = json_decode($result,true);
  1210. if (!$json || !empty($json['errcode'])) {
  1211. $this->errCode = $json['errcode'];
  1212. $this->errMsg = $json['errmsg'];
  1213. return false;
  1214. }
  1215. $this->jsapi_ticket = $json['ticket'];
  1216. $expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
  1217. $this->setCache($authname,$this->jsapi_ticket,$expire);
  1218. return $this->jsapi_ticket;
  1219. }
  1220. return false;
  1221. }
  1222. /**
  1223. * 获取JsApi使用签名
  1224. * @param string $url 网页的URL,自动处理#及其后面部分
  1225. * @param string $timestamp 当前时间戳 (为空则自动生成)
  1226. * @param string $noncestr 随机串 (为空则自动生成)
  1227. * @param string $appid 用于多个appid时使用,可空
  1228. * @return array|bool 返回签名字串
  1229. */
  1230. public function getJsSign($url, $timestamp=0, $noncestr='', $appid=''){
  1231. if (!$this->jsapi_ticket && !$this->getJsTicket($appid) || !$url) return false;
  1232. if (!$timestamp)
  1233. $timestamp = time();
  1234. if (!$noncestr)
  1235. $noncestr = $this->generateNonceStr();
  1236. $ret = strpos($url,'#');
  1237. if ($ret)
  1238. $url = substr($url,0,$ret);
  1239. $url = trim($url);
  1240. if (empty($url))
  1241. return false;
  1242. $arrdata = array("timestamp" => $timestamp, "noncestr" => $noncestr, "url" => $url, "jsapi_ticket" => $this->jsapi_ticket);
  1243. $sign = $this->getSignature($arrdata);
  1244. if (!$sign)
  1245. return false;
  1246. $signPackage = array(
  1247. "appId" => $this->appid,
  1248. "nonceStr" => $noncestr,
  1249. "timestamp" => $timestamp,
  1250. "url" => $url,
  1251. "signature" => $sign
  1252. );
  1253. return $signPackage;
  1254. }
  1255. /**
  1256. * 获取卡券签名cardSign
  1257. * @param string $card_type 卡券的类型,不可为空,官方jssdk文档说这个值可空,但签名验证工具又必填这个值,官方文档到处是坑,
  1258. * @param string $card_id 卡券的ID,可空
  1259. * @param string $location_id 卡券的适用门店ID,可空
  1260. * @param string $timestamp 当前时间戳 (为空则自动生成)
  1261. * @param string $noncestr 随机串 (为空则自动生成)
  1262. * @param string $appid 用于多个appid时使用,可空
  1263. * @return array|bool 返回签名字串
  1264. */
  1265. public function getCardSign($card_type='',$card_id='',$code='',$location_id='',$timestamp=0, $noncestr='', $appid=''){
  1266. if (!$this->api_ticket && !$this->getJsCardTicket($appid)) return false;
  1267. if (!$timestamp)
  1268. $timestamp = time();
  1269. if (!$noncestr)
  1270. $noncestr = $this->generateNonceStr();
  1271. $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 );
  1272. $sign = $this->getTicketSignature($arrdata);
  1273. if (!$sign)
  1274. return false;
  1275. $signPackage = array(
  1276. "cardType" => $card_type,
  1277. "cardId" => $card_id,
  1278. "shopId" => $location_id, //location_id就是shopId
  1279. "nonceStr" => $noncestr,
  1280. "timestamp" => $timestamp,
  1281. "cardSign" => $sign
  1282. );
  1283. return $signPackage;
  1284. }
  1285. /**
  1286. * 微信api不支持中文转义的json结构
  1287. * @param array $arr
  1288. */
  1289. static function json_encode($arr) {
  1290. if (count($arr) == 0) return "[]";
  1291. $parts = array ();
  1292. $is_list = false;
  1293. //Find out if the given array is a numerical array
  1294. $keys = array_keys ( $arr );
  1295. $max_length = count ( $arr ) - 1;
  1296. if (($keys [0] === 0) && ($keys [$max_length] === $max_length )) { //See if the first key is 0 and last key is length - 1
  1297. $is_list = true;
  1298. for($i = 0; $i < count ( $keys ); $i ++) { //See if each key correspondes to its position
  1299. if ($i != $keys [$i]) { //A key fails at position check.
  1300. $is_list = false; //It is an associative array.
  1301. break;
  1302. }
  1303. }
  1304. }
  1305. foreach ( $arr as $key => $value ) {
  1306. if (is_array ( $value )) { //Custom handling for arrays
  1307. if ($is_list)
  1308. $parts [] = self::json_encode ( $value ); /* :RECURSION: */
  1309. else
  1310. $parts [] = '"' . $key . '":' . self::json_encode ( $value ); /* :RECURSION: */
  1311. } else {
  1312. $str = '';
  1313. if (! $is_list)
  1314. $str = '"' . $key . '":';
  1315. //Custom handling for multiple data types
  1316. if (!is_string ( $value ) && is_numeric ( $value ) && $value<2000000000)
  1317. $str .= $value; //Numbers
  1318. elseif ($value === false)
  1319. $str .= 'false'; //The booleans
  1320. elseif ($value === true)
  1321. $str .= 'true';
  1322. else
  1323. $str .= '"' . addslashes ( $value ) . '"'; //All other things
  1324. // :TODO: Is there any more datatype we should be in the lookout for? (Object?)
  1325. $parts [] = $str;
  1326. }
  1327. }
  1328. $json = implode ( ',', $parts );
  1329. if ($is_list)
  1330. return '[' . $json . ']'; //Return numerical JSON
  1331. return '{' . $json . '}'; //Return associative JSON
  1332. }
  1333. /**
  1334. * 获取签名
  1335. * @param array $arrdata 签名数组
  1336. * @param string $method 签名方法
  1337. * @return boolean|string 签名值
  1338. */
  1339. public function getSignature($arrdata,$method="sha1") {
  1340. if (!function_exists($method)) return false;
  1341. ksort($arrdata);
  1342. $paramstring = "";
  1343. foreach($arrdata as $key => $value)
  1344. {
  1345. if(strlen($paramstring) == 0)
  1346. $paramstring .= $key . "=" . $value;
  1347. else
  1348. $paramstring .= "&" . $key . "=" . $value;
  1349. }
  1350. $Sign = $method($paramstring);
  1351. return $Sign;
  1352. }
  1353. /**
  1354. * 获取微信卡券api_ticket
  1355. * @param string $appid 用于多个appid时使用,可空
  1356. * @param string $api_ticket 手动指定api_ticket,非必要情况不建议用
  1357. */
  1358. public function getJsCardTicket($appid='',$api_ticket=''){
  1359. if (!$this->access_token && !$this->checkAuth()) return false;
  1360. if (!$appid) $appid = $this->appid;
  1361. if ($api_ticket) { //手动指定token,优先使用
  1362. $this->api_ticket = $api_ticket;
  1363. return $this->api_ticket;
  1364. }
  1365. $authname = 'wechat_api_ticket_wxcard'.$appid;
  1366. if ($rs = $this->getCache($authname)) {
  1367. $this->api_ticket = $rs;
  1368. return $rs;
  1369. }
  1370. $result = $this->http_get(self::API_URL_PREFIX.self::GET_TICKET_URL.'access_token='.$this->access_token.'&type=wx_card');
  1371. if ($result)
  1372. {
  1373. $json = json_decode($result,true);
  1374. if (!$json || !empty($json['errcode'])) {
  1375. $this->errCode = $json['errcode'];
  1376. $this->errMsg = $json['errmsg'];
  1377. return false;
  1378. }
  1379. $this->api_ticket = $json['ticket'];
  1380. $expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
  1381. $this->setCache($authname,$this->api_ticket,$expire);
  1382. return $this->api_ticket;
  1383. }
  1384. return false;
  1385. }
  1386. /**
  1387. * 获取微信卡券签名
  1388. * @param array $arrdata 签名数组
  1389. * @param string $method 签名方法
  1390. * @return boolean|string 签名值
  1391. */
  1392. public function getTicketSignature($arrdata,$method="sha1") {
  1393. if (!function_exists($method)) return false;
  1394. $newArray = array();
  1395. foreach($arrdata as $key => $value)
  1396. {
  1397. array_push($newArray,(string)$value);
  1398. }
  1399. sort($newArray,SORT_STRING);
  1400. return $method(implode($newArray));
  1401. }
  1402. /**
  1403. * 生成随机字串
  1404. * @param number $length 长度,默认为16,最长为32字节
  1405. * @return string
  1406. */
  1407. public function generateNonceStr($length=16){
  1408. // 密码字符集,可任意添加你需要的字符
  1409. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  1410. $str = "";
  1411. for($i = 0; $i < $length; $i++)
  1412. {
  1413. $str .= $chars[mt_rand(0, strlen($chars) - 1)];
  1414. }
  1415. return $str;
  1416. }
  1417. /**
  1418. * 获取微信服务器IP地址列表
  1419. * @return array('127.0.0.1','127.0.0.1')
  1420. */
  1421. public function getServerIp(){
  1422. if (!$this->access_token && !$this->checkAuth()) return false;
  1423. $result = $this->http_get(self::API_URL_PREFIX.self::CALLBACKSERVER_GET_URL.'access_token='.$this->access_token);
  1424. if ($result)
  1425. {
  1426. $json = json_decode($result,true);
  1427. if (!$json || isset($json['errcode'])) {
  1428. $this->errCode = $json['errcode'];
  1429. $this->errMsg = $json['errmsg'];
  1430. return false;
  1431. }
  1432. return $json['ip_list'];
  1433. }
  1434. return false;
  1435. }
  1436. /**
  1437. * 创建菜单(认证后的订阅号可用)
  1438. * @param array $data 菜单数组数据
  1439. * example:
  1440. * array (
  1441. * 'button' => array (
  1442. * 0 => array (
  1443. * 'name' => '扫码',
  1444. * 'sub_button' => array (
  1445. * 0 => array (
  1446. * 'type' => 'scancode_waitmsg',
  1447. * 'name' => '扫码带提示',
  1448. * 'key' => 'rselfmenu_0_0',
  1449. * ),
  1450. * 1 => array (
  1451. * 'type' => 'scancode_push',
  1452. * 'name' => '扫码推事件',
  1453. * 'key' => 'rselfmenu_0_1',
  1454. * ),
  1455. * ),
  1456. * ),
  1457. * 1 => array (
  1458. * 'name' => '发图',
  1459. * 'sub_button' => array (
  1460. * 0 => array (
  1461. * 'type' => 'pic_sysphoto',
  1462. * 'name' => '系统拍照发图',
  1463. * 'key' => 'rselfmenu_1_0',
  1464. * ),
  1465. * 1 => array (
  1466. * 'type' => 'pic_photo_or_album',
  1467. * 'name' => '拍照或者相册发图',
  1468. * 'key' => 'rselfmenu_1_1',
  1469. * )
  1470. * ),
  1471. * ),
  1472. * 2 => array (
  1473. * 'type' => 'location_select',
  1474. * 'name' => '发送位置',
  1475. * 'key' => 'rselfmenu_2_0'
  1476. * ),
  1477. * ),
  1478. * )
  1479. * type可以选择为以下几种,其中5-8除了收到菜单事件以外,还会单独收到对应类型的信息。
  1480. * 1、click:点击推事件
  1481. * 2、view:跳转URL
  1482. * 3、scancode_push:扫码推事件
  1483. * 4、scancode_waitmsg:扫码推事件且弹出“消息接收中”提示框
  1484. * 5、pic_sysphoto:弹出系统拍照发图
  1485. * 6、pic_photo_or_album:弹出拍照或者相册发图
  1486. * 7、pic_weixin:弹出微信相册发图器
  1487. * 8、location_select:弹出地理位置选择器
  1488. */
  1489. public function createMenu($data){
  1490. if (!$this->access_token && !$this->checkAuth()) return false;
  1491. $result = $this->http_post(self::API_URL_PREFIX.self::MENU_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1492. if ($result)
  1493. {
  1494. $json = json_decode($result,true);
  1495. if (!$json || !empty($json['errcode'])) {
  1496. $this->errCode = $json['errcode'];
  1497. $this->errMsg = $json['errmsg'];
  1498. return false;
  1499. }
  1500. return true;
  1501. }
  1502. return false;
  1503. }
  1504. /**
  1505. * 获取菜单(认证后的订阅号可用)
  1506. * @return array('menu'=>array(....s))
  1507. */
  1508. public function getMenu(){
  1509. if (!$this->access_token && !$this->checkAuth()) return false;
  1510. $result = $this->http_get(self::API_URL_PREFIX.self::MENU_GET_URL.'access_token='.$this->access_token);
  1511. if ($result)
  1512. {
  1513. $json = json_decode($result,true);
  1514. if (!$json || isset($json['errcode'])) {
  1515. $this->errCode = $json['errcode'];
  1516. $this->errMsg = $json['errmsg'];
  1517. return false;
  1518. }
  1519. return $json;
  1520. }
  1521. return false;
  1522. }
  1523. /**
  1524. * 删除菜单(认证后的订阅号可用)
  1525. * @return boolean
  1526. */
  1527. public function deleteMenu(){
  1528. if (!$this->access_token && !$this->checkAuth()) return false;
  1529. $result = $this->http_get(self::API_URL_PREFIX.self::MENU_DELETE_URL.'access_token='.$this->access_token);
  1530. if ($result)
  1531. {
  1532. $json = json_decode($result,true);
  1533. if (!$json || !empty($json['errcode'])) {
  1534. $this->errCode = $json['errcode'];
  1535. $this->errMsg = $json['errmsg'];
  1536. return false;
  1537. }
  1538. return true;
  1539. }
  1540. return false;
  1541. }
  1542. /**
  1543. * 创建个性化菜单(认证后的订阅号可用)
  1544. * @param array $data
  1545. * @return bool
  1546. *
  1547. */
  1548. public function addconditionalMenu($data){
  1549. if (!$this->access_token && !$this->checkAuth()) return false;
  1550. $result = $this->http_post(self::API_URL_PREFIX.self::MENU_ADDCONDITIONAL_URL.'access_token='.$this->access_token,self::json_encode($data));
  1551. if ($result)
  1552. {
  1553. $json = json_decode($result,true);
  1554. if (!$json || !empty($json['errcode'])) {
  1555. $this->errCode = $json['errcode'];
  1556. $this->errMsg = $json['errmsg'];
  1557. return false;
  1558. }
  1559. return true;
  1560. }
  1561. return false;
  1562. }
  1563. /**
  1564. * 删除个性化菜单(认证后的订阅号可用)
  1565. * @param $data {"menuid":"208379533"}
  1566. *
  1567. * @return bool
  1568. */
  1569. public function delconditionalMenu($data){
  1570. if (!$this->access_token && !$this->checkAuth()) return false;
  1571. $result = $this->http_post(self::API_URL_PREFIX.self::MENU_DELCONDITIONAL_URL.'access_token='.$this->access_token,self::json_encode($data));
  1572. if ($result)
  1573. {
  1574. $json = json_decode($result,true);
  1575. if (!$json || !empty($json['errcode'])) {
  1576. $this->errCode = $json['errcode'];
  1577. $this->errMsg = $json['errmsg'];
  1578. return false;
  1579. }
  1580. return true;
  1581. }
  1582. return false;
  1583. }
  1584. /**
  1585. * 测试个性化菜单匹配结果(认证后的订阅号可用)
  1586. * @param $data {"user_id":"weixin"} user_id可以是粉丝的OpenID,也可以是粉丝的微信号
  1587. *
  1588. * @return bool|array('button'=>array(....s))
  1589. */
  1590. public function trymatchMenu($data){
  1591. if (!$this->access_token && !$this->checkAuth()) return false;
  1592. $result = $this->http_post(self::API_URL_PREFIX.self::MENU_TRYMATCH_URL.'access_token='.$this->access_token,self::json_encode($data));
  1593. if ($result)
  1594. {
  1595. $json = json_decode($result,true);
  1596. if (!$json || !empty($json['errcode'])) {
  1597. $this->errCode = $json['errcode'];
  1598. $this->errMsg = $json['errmsg'];
  1599. return false;
  1600. }
  1601. return $json;
  1602. }
  1603. return false;
  1604. }
  1605. /**
  1606. * 上传临时素材,有效期为3天(认证后的订阅号可用)
  1607. * 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
  1608. * 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
  1609. * 注意:临时素材的media_id是可复用的!
  1610. * @param array $data {"media":'@Path\filename.jpg'}
  1611. * @param type 类型:图片:image 语音:voice 视频:video 缩略图:thumb
  1612. * @return boolean|array
  1613. */
  1614. public function uploadMedia($data, $type){
  1615. if (!$this->access_token && !$this->checkAuth()) return false;
  1616. //原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
  1617. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOAD_URL.'access_token='.$this->access_token.'&type='.$type,$data,true);
  1618. if ($result)
  1619. {
  1620. $json = json_decode($result,true);
  1621. if (!$json || !empty($json['errcode'])) {
  1622. $this->errCode = $json['errcode'];
  1623. $this->errMsg = $json['errmsg'];
  1624. return false;
  1625. }
  1626. return $json;
  1627. }
  1628. return false;
  1629. }
  1630. /**
  1631. * 获取临时素材(认证后的订阅号可用)
  1632. * @param string $media_id 媒体文件id
  1633. * @param boolean $is_video 是否为视频文件,默认为否
  1634. * @return raw data
  1635. */
  1636. public function getMedia($media_id,$is_video=false){
  1637. if (!$this->access_token && !$this->checkAuth()) return false;
  1638. //原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
  1639. //如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
  1640. $url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
  1641. $result = $this->http_get($url_prefix.self::MEDIA_GET_URL.'access_token='.$this->access_token.'&media_id='.$media_id);
  1642. if ($result)
  1643. {
  1644. if (is_string($result)) {
  1645. $json = json_decode($result,true);
  1646. if (isset($json['errcode'])) {
  1647. $this->errCode = $json['errcode'];
  1648. $this->errMsg = $json['errmsg'];
  1649. return false;
  1650. }
  1651. }
  1652. return $result;
  1653. }
  1654. return false;
  1655. }
  1656. /**
  1657. * 上传图片,本接口所上传的图片不占用公众号的素材库中图片数量的5000个的限制。图片仅支持jpg/png格式,大小必须在1MB以下。 (认证后的订阅号可用)
  1658. * 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
  1659. * 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
  1660. * @param array $data {"media":'@Path\filename.jpg'}
  1661. *
  1662. * @return boolean|array
  1663. */
  1664. public function uploadImg($data){
  1665. if (!$this->access_token && !$this->checkAuth()) return false;
  1666. //原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
  1667. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOADIMG_URL.'access_token='.$this->access_token,$data,true);
  1668. if ($result)
  1669. {
  1670. $json = json_decode($result,true);
  1671. if (!$json || !empty($json['errcode'])) {
  1672. $this->errCode = $json['errcode'];
  1673. $this->errMsg = $json['errmsg'];
  1674. return false;
  1675. }
  1676. return $json;
  1677. }
  1678. return false;
  1679. }
  1680. /**
  1681. * 上传永久素材(认证后的订阅号可用)
  1682. * 新增的永久素材也可以在公众平台官网素材管理模块中看到
  1683. * 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
  1684. * 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
  1685. * @param array $data {"media":'@Path\filename.jpg'}
  1686. * @param type 类型:图片:image 语音:voice 视频:video 缩略图:thumb
  1687. * @param boolean $is_video 是否为视频文件,默认为否
  1688. * @param array $video_info 视频信息数组,非视频素材不需要提供 array('title'=>'视频标题','introduction'=>'描述')
  1689. * @return boolean|array
  1690. */
  1691. public function uploadForeverMedia($data, $type,$is_video=false,$video_info=array()){
  1692. if (!$this->access_token && !$this->checkAuth()) return false;
  1693. //#TODO 暂不确定此接口是否需要让视频文件走http协议
  1694. //如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
  1695. //$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
  1696. //当上传视频文件时,附加视频文件信息
  1697. if ($is_video) $data['description'] = self::json_encode($video_info);
  1698. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_UPLOAD_URL.'access_token='.$this->access_token.'&type='.$type,$data,true);
  1699. if ($result)
  1700. {
  1701. $json = json_decode($result,true);
  1702. if (!$json || !empty($json['errcode'])) {
  1703. $this->errCode = $json['errcode'];
  1704. $this->errMsg = $json['errmsg'];
  1705. return false;
  1706. }
  1707. return $json;
  1708. }
  1709. return false;
  1710. }
  1711. /**
  1712. * 上传永久图文素材(认证后的订阅号可用)
  1713. * 新增的永久素材也可以在公众平台官网素材管理模块中看到
  1714. * @param array $data 消息结构{"articles":[{...}]}
  1715. * @return boolean|array
  1716. */
  1717. public function uploadForeverArticles($data){
  1718. if (!$this->access_token && !$this->checkAuth()) return false;
  1719. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPLOAD_URL.'access_token='.$this->access_token,self::json_encode($data));
  1720. if ($result)
  1721. {
  1722. $json = json_decode($result,true);
  1723. if (!$json || !empty($json['errcode'])) {
  1724. $this->errCode = $json['errcode'];
  1725. $this->errMsg = $json['errmsg'];
  1726. return false;
  1727. }
  1728. return $json;
  1729. }
  1730. return false;
  1731. }
  1732. /**
  1733. * 修改永久图文素材(认证后的订阅号可用)
  1734. * 永久素材也可以在公众平台官网素材管理模块中看到
  1735. * @param string $media_id 图文素材id
  1736. * @param array $data 消息结构{"articles":[{...}]}
  1737. * @param int $index 更新的文章在图文素材的位置,第一篇为0,仅多图文使用
  1738. * @return boolean|array
  1739. */
  1740. public function updateForeverArticles($media_id,$data,$index=0){
  1741. if (!$this->access_token && !$this->checkAuth()) return false;
  1742. if (!isset($data['media_id'])) $data['media_id'] = $media_id;
  1743. if (!isset($data['index'])) $data['index'] = $index;
  1744. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  1745. if ($result)
  1746. {
  1747. $json = json_decode($result,true);
  1748. if (!$json || !empty($json['errcode'])) {
  1749. $this->errCode = $json['errcode'];
  1750. $this->errMsg = $json['errmsg'];
  1751. return false;
  1752. }
  1753. return $json;
  1754. }
  1755. return false;
  1756. }
  1757. /**
  1758. * 获取永久素材(认证后的订阅号可用)
  1759. * 返回图文消息数组或二进制数据,失败返回false
  1760. * @param string $media_id 媒体文件id
  1761. * @param boolean $is_video 是否为视频文件,默认为否
  1762. * @return boolean|array|raw data
  1763. */
  1764. public function getForeverMedia($media_id,$is_video=false){
  1765. if (!$this->access_token && !$this->checkAuth()) return false;
  1766. $data = array('media_id' => $media_id);
  1767. //#TODO 暂不确定此接口是否需要让视频文件走http协议
  1768. //如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
  1769. //$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
  1770. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_GET_URL.'access_token='.$this->access_token,self::json_encode($data));
  1771. if ($result)
  1772. {
  1773. if (is_string($result)) {
  1774. $json = json_decode($result,true);
  1775. if ($json) {
  1776. if (isset($json['errcode'])) {
  1777. $this->errCode = $json['errcode'];
  1778. $this->errMsg = $json['errmsg'];
  1779. return false;
  1780. }
  1781. return $json;
  1782. } else {
  1783. return $result;
  1784. }
  1785. }
  1786. return $result;
  1787. }
  1788. return false;
  1789. }
  1790. /**
  1791. * 删除永久素材(认证后的订阅号可用)
  1792. * @param string $media_id 媒体文件id
  1793. * @return boolean
  1794. */
  1795. public function delForeverMedia($media_id){
  1796. if (!$this->access_token && !$this->checkAuth()) return false;
  1797. $data = array('media_id' => $media_id);
  1798. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_DEL_URL.'access_token='.$this->access_token,self::json_encode($data));
  1799. if ($result)
  1800. {
  1801. $json = json_decode($result,true);
  1802. if (!$json || !empty($json['errcode'])) {
  1803. $this->errCode = $json['errcode'];
  1804. $this->errMsg = $json['errmsg'];
  1805. return false;
  1806. }
  1807. return true;
  1808. }
  1809. return false;
  1810. }
  1811. /**
  1812. * 获取永久素材列表(认证后的订阅号可用)
  1813. * @param string $type 素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
  1814. * @param int $offset 全部素材的偏移位置,0表示从第一个素材
  1815. * @param int $count 返回素材的数量,取值在1到20之间
  1816. * @return boolean|array
  1817. * 返回数组格式:
  1818. * array(
  1819. * 'total_count'=>0, //该类型的素材的总数
  1820. * 'item_count'=>0, //本次调用获取的素材的数量
  1821. * 'item'=>array() //素材列表数组,内容定义请参考官方文档
  1822. * )
  1823. */
  1824. public function getForeverList($type,$offset,$count){
  1825. if (!$this->access_token && !$this->checkAuth()) return false;
  1826. $data = array(
  1827. 'type' => $type,
  1828. 'offset' => $offset,
  1829. 'count' => $count,
  1830. );
  1831. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_BATCHGET_URL.'access_token='.$this->access_token,self::json_encode($data));
  1832. if ($result)
  1833. {
  1834. $json = json_decode($result,true);
  1835. if (isset($json['errcode'])) {
  1836. $this->errCode = $json['errcode'];
  1837. $this->errMsg = $json['errmsg'];
  1838. return false;
  1839. }
  1840. return $json;
  1841. }
  1842. return false;
  1843. }
  1844. /**
  1845. * 获取永久素材总数(认证后的订阅号可用)
  1846. * @return boolean|array
  1847. * 返回数组格式:
  1848. * array(
  1849. * 'voice_count'=>0, //语音总数量
  1850. * 'video_count'=>0, //视频总数量
  1851. * 'image_count'=>0, //图片总数量
  1852. * 'news_count'=>0 //图文总数量
  1853. * )
  1854. */
  1855. public function getForeverCount(){
  1856. if (!$this->access_token && !$this->checkAuth()) return false;
  1857. $result = $this->http_get(self::API_URL_PREFIX.self::MEDIA_FOREVER_COUNT_URL.'access_token='.$this->access_token);
  1858. if ($result)
  1859. {
  1860. $json = json_decode($result,true);
  1861. if (isset($json['errcode'])) {
  1862. $this->errCode = $json['errcode'];
  1863. $this->errMsg = $json['errmsg'];
  1864. return false;
  1865. }
  1866. return $json;
  1867. }
  1868. return false;
  1869. }
  1870. /**
  1871. * 上传图文消息素材,用于群发(认证后的订阅号可用)
  1872. * @param array $data 消息结构{"articles":[{...}]}
  1873. * @return boolean|array
  1874. */
  1875. public function uploadArticles($data){
  1876. if (!$this->access_token && !$this->checkAuth()) return false;
  1877. $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOADNEWS_URL.'access_token='.$this->access_token,self::json_encode($data));
  1878. if ($result)
  1879. {
  1880. $json = json_decode($result,true);
  1881. if (!$json || !empty($json['errcode'])) {
  1882. $this->errCode = $json['errcode'];
  1883. $this->errMsg = $json['errmsg'];
  1884. return false;
  1885. }
  1886. return $json;
  1887. }
  1888. return false;
  1889. }
  1890. /**
  1891. * 上传视频素材(认证后的订阅号可用)
  1892. * @param array $data 消息结构
  1893. * {
  1894. * "media_id"=>"", //通过上传媒体接口得到的MediaId
  1895. * "title"=>"TITLE", //视频标题
  1896. * "description"=>"Description" //视频描述
  1897. * }
  1898. * @return boolean|array
  1899. * {
  1900. * "type":"video",
  1901. * "media_id":"mediaid",
  1902. * "created_at":1398848981
  1903. * }
  1904. */
  1905. public function uploadMpVideo($data){
  1906. if (!$this->access_token && !$this->checkAuth()) return false;
  1907. $result = $this->http_post(self::UPLOAD_MEDIA_URL.self::MEDIA_VIDEO_UPLOAD.'access_token='.$this->access_token,self::json_encode($data));
  1908. if ($result)
  1909. {
  1910. $json = json_decode($result,true);
  1911. if (!$json || !empty($json['errcode'])) {
  1912. $this->errCode = $json['errcode'];
  1913. $this->errMsg = $json['errmsg'];
  1914. return false;
  1915. }
  1916. return $json;
  1917. }
  1918. return false;
  1919. }
  1920. /**
  1921. * 高级群发消息, 根据OpenID列表群发图文消息(订阅号不可用)
  1922. * 注意:视频需要在调用uploadMedia()方法后,再使用 uploadMpVideo() 方法生成,
  1923. * 然后获得的 mediaid 才能用于群发,且消息类型为 mpvideo 类型。
  1924. * @param array $data 消息结构
  1925. * {
  1926. * "touser"=>array(
  1927. * "OPENID1",
  1928. * "OPENID2"
  1929. * ),
  1930. * "msgtype"=>"mpvideo",
  1931. * // 在下面5种类型中选择对应的参数内容
  1932. * // mpnews | voice | image | mpvideo => array( "media_id"=>"MediaId")
  1933. * // text => array ( "content" => "hello")
  1934. * }
  1935. * @return boolean|array
  1936. */
  1937. public function sendMassMessage($data){
  1938. if (!$this->access_token && !$this->checkAuth()) return false;
  1939. $result = $this->http_post(self::API_URL_PREFIX.self::MASS_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
  1940. if ($result)
  1941. {
  1942. $json = json_decode($result,true);
  1943. if (!$json || !empty($json['errcode'])) {
  1944. $this->errCode = $json['errcode'];
  1945. $this->errMsg = $json['errmsg'];
  1946. return false;
  1947. }
  1948. return $json;
  1949. }
  1950. return false;
  1951. }
  1952. /**
  1953. * 高级群发消息, 根据群组id群发图文消息(认证后的订阅号可用)
  1954. * 注意:视频需要在调用uploadMedia()方法后,再使用 uploadMpVideo() 方法生成,
  1955. * 然后获得的 mediaid 才能用于群发,且消息类型为 mpvideo 类型。
  1956. * @param array $data 消息结构
  1957. * {
  1958. * "filter"=>array(
  1959. * "is_to_all"=>False, //是否群发给所有用户.True不用分组id,False需填写分组id
  1960. * "group_id"=>"2" //群发的分组id
  1961. * ),
  1962. * "msgtype"=>"mpvideo",
  1963. * // 在下面5种类型中选择对应的参数内容
  1964. * // mpnews | voice | image | mpvideo => array( "media_id"=>"MediaId")
  1965. * // text => array ( "content" => "hello")
  1966. * }
  1967. * @return boolean|array
  1968. */
  1969. public function sendGroupMassMessage($data){
  1970. if (!$this->access_token && !$this->checkAuth()) return false;
  1971. $result = $this->http_post(self::API_URL_PREFIX.self::MASS_SEND_GROUP_URL.'access_token='.$this->access_token,self::json_encode($data));
  1972. if ($result)
  1973. {
  1974. $json = json_decode($result,true);
  1975. if (!$json || !empty($json['errcode'])) {
  1976. $this->errCode = $json['errcode'];
  1977. $this->errMsg = $json['errmsg'];
  1978. return false;
  1979. }
  1980. return $json;
  1981. }
  1982. return false;
  1983. }
  1984. /**
  1985. * 高级群发消息, 删除群发图文消息(认证后的订阅号可用)
  1986. * @param int $msg_id 消息id
  1987. * @return boolean|array
  1988. */
  1989. public function deleteMassMessage($msg_id){
  1990. if (!$this->access_token && !$this->checkAuth()) return false;
  1991. $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)));
  1992. if ($result)
  1993. {
  1994. $json = json_decode($result,true);
  1995. if (!$json || !empty($json['errcode'])) {
  1996. $this->errCode = $json['errcode'];
  1997. $this->errMsg = $json['errmsg'];
  1998. return false;
  1999. }
  2000. return true;
  2001. }
  2002. return false;
  2003. }
  2004. /**
  2005. * 高级群发消息, 预览群发消息(认证后的订阅号可用)
  2006. * 注意:视频需要在调用uploadMedia()方法后,再使用 uploadMpVideo() 方法生成,
  2007. * 然后获得的 mediaid 才能用于群发,且消息类型为 mpvideo 类型。
  2008. * @param array $data 消息结构
  2009. * {
  2010. * "touser"=>"OPENID",
  2011. * "msgtype"=>"mpvideo",
  2012. * // 在下面5种类型中选择对应的参数内容
  2013. * // mpnews | voice | image | mpvideo => array( "media_id"=>"MediaId")
  2014. * // text => array ( "content" => "hello")
  2015. * }
  2016. * @return boolean|array
  2017. */
  2018. public function previewMassMessage($data){
  2019. if (!$this->access_token && !$this->checkAuth()) return false;
  2020. $result = $this->http_post(self::API_URL_PREFIX.self::MASS_PREVIEW_URL.'access_token='.$this->access_token,self::json_encode($data));
  2021. if ($result)
  2022. {
  2023. $json = json_decode($result,true);
  2024. if (!$json || !empty($json['errcode'])) {
  2025. $this->errCode = $json['errcode'];
  2026. $this->errMsg = $json['errmsg'];
  2027. return false;
  2028. }
  2029. return $json;
  2030. }
  2031. return false;
  2032. }
  2033. /**
  2034. * 高级群发消息, 查询群发消息发送状态(认证后的订阅号可用)
  2035. * @param int $msg_id 消息id
  2036. * @return boolean|array
  2037. * {
  2038. * "msg_id":201053012, //群发消息后返回的消息id
  2039. * "msg_status":"SEND_SUCCESS" //消息发送后的状态,SENDING表示正在发送 SEND_SUCCESS表示发送成功
  2040. * }
  2041. */
  2042. public function queryMassMessage($msg_id){
  2043. if (!$this->access_token && !$this->checkAuth()) return false;
  2044. $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)));
  2045. if ($result)
  2046. {
  2047. $json = json_decode($result,true);
  2048. if (!$json || !empty($json['errcode'])) {
  2049. $this->errCode = $json['errcode'];
  2050. $this->errMsg = $json['errmsg'];
  2051. return false;
  2052. }
  2053. return $json;
  2054. }
  2055. return false;
  2056. }
  2057. /**
  2058. * 创建二维码ticket
  2059. * @param int|string $scene_id 自定义追踪id,临时二维码只能用数值型
  2060. * @param int $type 0:临时二维码;1:数值型永久二维码(此时expire参数无效);2:字符串型永久二维码(此时expire参数无效)
  2061. * @param int $expire 临时二维码有效期,最大为604800秒
  2062. * @return array('ticket'=>'qrcode字串','expire_seconds'=>604800,'url'=>'二维码图片解析后的地址')
  2063. */
  2064. public function getQRCode($scene_id,$type=0,$expire=604800){
  2065. if (!$this->access_token && !$this->checkAuth()) return false;
  2066. if (!isset($scene_id)) return false;
  2067. switch ($type) {
  2068. case '0':
  2069. if (!is_numeric($scene_id))
  2070. return false;
  2071. $action_name = 'QR_SCENE';
  2072. $action_info = array('scene'=>(array('scene_id'=>$scene_id)));
  2073. break;
  2074. case '1':
  2075. if (!is_numeric($scene_id))
  2076. return false;
  2077. $action_name = 'QR_LIMIT_SCENE';
  2078. $action_info = array('scene'=>(array('scene_id'=>$scene_id)));
  2079. break;
  2080. case '2':
  2081. if (!is_string($scene_id))
  2082. return false;
  2083. $action_name = 'QR_LIMIT_STR_SCENE';
  2084. $action_info = array('scene'=>(array('scene_str'=>$scene_id)));
  2085. break;
  2086. default:
  2087. return false;
  2088. }
  2089. $data = array(
  2090. 'action_name' => $action_name,
  2091. 'expire_seconds' => $expire,
  2092. 'action_info' => $action_info
  2093. );
  2094. if ($type) {
  2095. unset($data['expire_seconds']);
  2096. }
  2097. $result = $this->http_post(self::API_URL_PREFIX.self::QRCODE_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  2098. if ($result) {
  2099. $json = json_decode($result,true);
  2100. if (!$json || !empty($json['errcode'])) {
  2101. $this->errCode = $json['errcode'];
  2102. $this->errMsg = $json['errmsg'];
  2103. return false;
  2104. }
  2105. return $json;
  2106. }
  2107. return false;
  2108. }
  2109. /**
  2110. * 获取二维码图片
  2111. * @param string $ticket 传入由getQRCode方法生成的ticket参数
  2112. * @return string url 返回http地址
  2113. */
  2114. public function getQRUrl($ticket) {
  2115. return self::QRCODE_IMG_URL.urlencode($ticket);
  2116. }
  2117. /**
  2118. * 长链接转短链接接口
  2119. * @param string $long_url 传入要转换的长url
  2120. * @return boolean|string url 成功则返回转换后的短url
  2121. */
  2122. public function getShortUrl($long_url){
  2123. if (!$this->access_token && !$this->checkAuth()) return false;
  2124. $data = array(
  2125. 'action'=>'long2short',
  2126. 'long_url'=>$long_url
  2127. );
  2128. $result = $this->http_post(self::API_URL_PREFIX.self::SHORT_URL.'access_token='.$this->access_token,self::json_encode($data));
  2129. if ($result)
  2130. {
  2131. $json = json_decode($result,true);
  2132. if (!$json || !empty($json['errcode'])) {
  2133. $this->errCode = $json['errcode'];
  2134. $this->errMsg = $json['errmsg'];
  2135. return false;
  2136. }
  2137. return $json['short_url'];
  2138. }
  2139. return false;
  2140. }
  2141. /**
  2142. * 获取统计数据
  2143. * @param string $type 数据分类(user|article|upstreammsg|interface)分别为(用户分析|图文分析|消息分析|接口分析)
  2144. * @param string $subtype 数据子分类,参考 DATACUBE_URL_ARR 常量定义部分 或者README.md说明文档
  2145. * @param string $begin_date 开始时间
  2146. * @param string $end_date 结束时间
  2147. * @return boolean|array 成功返回查询结果数组,其定义请看官方文档
  2148. */
  2149. public function getDatacube($type,$subtype,$begin_date,$end_date=''){
  2150. if (!$this->access_token && !$this->checkAuth()) return false;
  2151. if (!isset(self::$DATACUBE_URL_ARR[$type]) || !isset(self::$DATACUBE_URL_ARR[$type][$subtype]))
  2152. return false;
  2153. $data = array(
  2154. 'begin_date'=>$begin_date,
  2155. 'end_date'=>$end_date?$end_date:$begin_date
  2156. );
  2157. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::$DATACUBE_URL_ARR[$type][$subtype].'access_token='.$this->access_token,self::json_encode($data));
  2158. if ($result)
  2159. {
  2160. $json = json_decode($result,true);
  2161. if (!$json || !empty($json['errcode'])) {
  2162. $this->errCode = $json['errcode'];
  2163. $this->errMsg = $json['errmsg'];
  2164. return false;
  2165. }
  2166. return isset($json['list'])?$json['list']:$json;
  2167. }
  2168. return false;
  2169. }
  2170. /**
  2171. * 批量获取关注用户列表
  2172. * @param unknown $next_openid
  2173. */
  2174. public function getUserList($next_openid=''){
  2175. if (!$this->access_token && !$this->checkAuth()) return false;
  2176. $result = $this->http_get(self::API_URL_PREFIX.self::USER_GET_URL.'access_token='.$this->access_token.'&next_openid='.$next_openid);
  2177. if ($result)
  2178. {
  2179. $json = json_decode($result,true);
  2180. if (isset($json['errcode'])) {
  2181. $this->errCode = $json['errcode'];
  2182. $this->errMsg = $json['errmsg'];
  2183. return false;
  2184. }
  2185. return $json;
  2186. }
  2187. return false;
  2188. }
  2189. /**
  2190. * 获取关注者详细信息
  2191. * @param string $openid
  2192. * @param string $lang 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语
  2193. * @return array {subscribe,openid,nickname,sex,city,province,country,language,headimgurl,subscribe_time,[unionid]}
  2194. * 注意:unionid字段 只有在用户将公众号绑定到微信开放平台账号后,才会出现。建议调用前用isset()检测一下
  2195. */
  2196. public function getUserInfo($openid, $lang = 'zh_CN'){
  2197. if (!$this->access_token && !$this->checkAuth()) return false;
  2198. $result = $this->http_get(self::API_URL_PREFIX.self::USER_INFO_URL.'access_token='.$this->access_token.'&openid='.$openid.'&lang='.$lang);
  2199. if ($result)
  2200. {
  2201. $json = json_decode($result,true);
  2202. if (isset($json['errcode'])) {
  2203. $this->errCode = $json['errcode'];
  2204. $this->errMsg = $json['errmsg'];
  2205. return false;
  2206. }
  2207. return $json;
  2208. }
  2209. return false;
  2210. }
  2211. /**
  2212. * 批量获取关注者详细信息
  2213. * @param array $openids user_list{{'openid:xxxxxx'},{},{}}
  2214. * @return array user_info_list{subscribe,openid,nickname,sex,city,province,country,language,headimgurl,subscribe_time,[unionid]}{}{}...
  2215. * 注意:unionid字段 只有在用户将公众号绑定到微信开放平台账号后,才会出现。建议调用前用isset()检测一下
  2216. */
  2217. public function getUsersInfo($openids){
  2218. if (!$this->access_token && !$this->checkAuth()) return false;
  2219. $result = $this->http_post(self::API_URL_PREFIX.self::USERS_INFO_URL.'access_token='.$this->access_token,json_encode($openids));
  2220. if ($result)
  2221. {
  2222. $json = json_decode($result,true);
  2223. if (isset($json['errcode'])) {
  2224. $this->errCode = $json['errcode'];
  2225. $this->errMsg = $json['errmsg'];
  2226. return false;
  2227. }
  2228. return $json;
  2229. }
  2230. return false;
  2231. }
  2232. /**
  2233. * 设置用户备注名
  2234. * @param string $openid
  2235. * @param string $remark 备注名
  2236. * @return boolean|array
  2237. */
  2238. public function updateUserRemark($openid,$remark){
  2239. if (!$this->access_token && !$this->checkAuth()) return false;
  2240. $data = array(
  2241. 'openid'=>$openid,
  2242. 'remark'=>$remark
  2243. );
  2244. $result = $this->http_post(self::API_URL_PREFIX.self::USER_UPDATEREMARK_URL.'access_token='.$this->access_token,self::json_encode($data));
  2245. if ($result)
  2246. {
  2247. $json = json_decode($result,true);
  2248. if (!$json || !empty($json['errcode'])) {
  2249. $this->errCode = $json['errcode'];
  2250. $this->errMsg = $json['errmsg'];
  2251. return false;
  2252. }
  2253. return $json;
  2254. }
  2255. return false;
  2256. }
  2257. /**
  2258. * 获取用户分组列表
  2259. * @return boolean|array
  2260. */
  2261. public function getGroup(){
  2262. if (!$this->access_token && !$this->checkAuth()) return false;
  2263. $result = $this->http_get(self::API_URL_PREFIX.self::GROUP_GET_URL.'access_token='.$this->access_token);
  2264. if ($result)
  2265. {
  2266. $json = json_decode($result,true);
  2267. if (isset($json['errcode'])) {
  2268. $this->errCode = $json['errcode'];
  2269. $this->errMsg = $json['errmsg'];
  2270. return false;
  2271. }
  2272. return $json;
  2273. }
  2274. return false;
  2275. }
  2276. /**
  2277. * 获取用户所在分组
  2278. * @param string $openid
  2279. * @return boolean|int 成功则返回用户分组id
  2280. */
  2281. public function getUserGroup($openid){
  2282. if (!$this->access_token && !$this->checkAuth()) return false;
  2283. $data = array(
  2284. 'openid'=>$openid
  2285. );
  2286. $result = $this->http_post(self::API_URL_PREFIX.self::USER_GROUP_URL.'access_token='.$this->access_token,self::json_encode($data));
  2287. if ($result)
  2288. {
  2289. $json = json_decode($result,true);
  2290. if (!$json || !empty($json['errcode'])) {
  2291. $this->errCode = $json['errcode'];
  2292. $this->errMsg = $json['errmsg'];
  2293. return false;
  2294. } else
  2295. if (isset($json['groupid'])) return $json['groupid'];
  2296. }
  2297. return false;
  2298. }
  2299. /**
  2300. * 新增自定分组
  2301. * @param string $name 分组名称
  2302. * @return boolean|array
  2303. */
  2304. public function createGroup($name){
  2305. if (!$this->access_token && !$this->checkAuth()) return false;
  2306. $data = array(
  2307. 'group'=>array('name'=>$name)
  2308. );
  2309. $result = $this->http_post(self::API_URL_PREFIX.self::GROUP_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  2310. if ($result)
  2311. {
  2312. $json = json_decode($result,true);
  2313. if (!$json || !empty($json['errcode'])) {
  2314. $this->errCode = $json['errcode'];
  2315. $this->errMsg = $json['errmsg'];
  2316. return false;
  2317. }
  2318. return $json;
  2319. }
  2320. return false;
  2321. }
  2322. /**
  2323. * 更改分组名称
  2324. * @param int $groupid 分组id
  2325. * @param string $name 分组名称
  2326. * @return boolean|array
  2327. */
  2328. public function updateGroup($groupid,$name){
  2329. if (!$this->access_token && !$this->checkAuth()) return false;
  2330. $data = array(
  2331. 'group'=>array('id'=>$groupid,'name'=>$name)
  2332. );
  2333. $result = $this->http_post(self::API_URL_PREFIX.self::GROUP_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  2334. if ($result)
  2335. {
  2336. $json = json_decode($result,true);
  2337. if (!$json || !empty($json['errcode'])) {
  2338. $this->errCode = $json['errcode'];
  2339. $this->errMsg = $json['errmsg'];
  2340. return false;
  2341. }
  2342. return $json;
  2343. }
  2344. return false;
  2345. }
  2346. /**
  2347. * 移动用户分组
  2348. * @param int $groupid 分组id
  2349. * @param string $openid 用户openid
  2350. * @return boolean|array
  2351. */
  2352. public function updateGroupMembers($groupid,$openid){
  2353. if (!$this->access_token && !$this->checkAuth()) return false;
  2354. $data = array(
  2355. 'openid'=>$openid,
  2356. 'to_groupid'=>$groupid
  2357. );
  2358. $result = $this->http_post(self::API_URL_PREFIX.self::GROUP_MEMBER_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  2359. if ($result)
  2360. {
  2361. $json = json_decode($result,true);
  2362. if (!$json || !empty($json['errcode'])) {
  2363. $this->errCode = $json['errcode'];
  2364. $this->errMsg = $json['errmsg'];
  2365. return false;
  2366. }
  2367. return $json;
  2368. }
  2369. return false;
  2370. }
  2371. /**
  2372. * 批量移动用户分组
  2373. * @param int $groupid 分组id
  2374. * @param string $openid_list 用户openid数组,一次不能超过50个
  2375. * @return boolean|array
  2376. */
  2377. public function batchUpdateGroupMembers($groupid,$openid_list){
  2378. if (!$this->access_token && !$this->checkAuth()) return false;
  2379. $data = array(
  2380. 'openid_list'=>$openid_list,
  2381. 'to_groupid'=>$groupid
  2382. );
  2383. $result = $this->http_post(self::API_URL_PREFIX.self::GROUP_MEMBER_BATCHUPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  2384. if ($result)
  2385. {
  2386. $json = json_decode($result,true);
  2387. if (!$json || !empty($json['errcode'])) {
  2388. $this->errCode = $json['errcode'];
  2389. $this->errMsg = $json['errmsg'];
  2390. return false;
  2391. }
  2392. return $json;
  2393. }
  2394. return false;
  2395. }
  2396. /**
  2397. * 发送客服消息
  2398. * @param array $data 消息结构{"touser":"OPENID","msgtype":"news","news":{...}}
  2399. * @return boolean|array
  2400. */
  2401. public function sendCustomMessage($data){
  2402. if (!$this->access_token && !$this->checkAuth()) return false;
  2403. $result = $this->http_post(self::API_URL_PREFIX.self::CUSTOM_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
  2404. if ($result)
  2405. {
  2406. $json = json_decode($result,true);
  2407. if (!$json || !empty($json['errcode'])) {
  2408. $this->errCode = $json['errcode'];
  2409. $this->errMsg = $json['errmsg'];
  2410. return false;
  2411. }
  2412. return $json;
  2413. }
  2414. return false;
  2415. }
  2416. /**
  2417. * oauth 授权跳转接口
  2418. * @param string $callback 回调URI
  2419. * @return string
  2420. */
  2421. public function getOauthRedirect($callback,$state='',$scope='snsapi_userinfo'){
  2422. return self::OAUTH_PREFIX.self::OAUTH_AUTHORIZE_URL.'appid='.$this->appid.'&redirect_uri='.urlencode($callback).'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';
  2423. }
  2424. /**
  2425. * 通过code获取Access Token
  2426. * @return array {access_token,expires_in,refresh_token,openid,scope}
  2427. */
  2428. public function getOauthAccessToken(){
  2429. $code = isset($_GET['code'])?$_GET['code']:'';
  2430. if (!$code) return false;
  2431. $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');
  2432. if ($result)
  2433. {
  2434. $json = json_decode($result,true);
  2435. if (!$json || !empty($json['errcode'])) {
  2436. $this->errCode = $json['errcode'];
  2437. $this->errMsg = $json['errmsg'];
  2438. return false;
  2439. }
  2440. $this->user_token = $json['access_token'];
  2441. return $json;
  2442. }
  2443. return false;
  2444. }
  2445. /**
  2446. * 刷新access token并续期
  2447. * @param string $refresh_token
  2448. * @return boolean|mixed
  2449. */
  2450. public function getOauthRefreshToken($refresh_token){
  2451. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::OAUTH_REFRESH_URL.'appid='.$this->appid.'&grant_type=refresh_token&refresh_token='.$refresh_token);
  2452. if ($result)
  2453. {
  2454. $json = json_decode($result,true);
  2455. if (!$json || !empty($json['errcode'])) {
  2456. $this->errCode = $json['errcode'];
  2457. $this->errMsg = $json['errmsg'];
  2458. return false;
  2459. }
  2460. $this->user_token = $json['access_token'];
  2461. return $json;
  2462. }
  2463. return false;
  2464. }
  2465. /**
  2466. * 获取授权后的用户资料
  2467. * @param string $access_token
  2468. * @param string $openid
  2469. * @return array {openid,nickname,sex,province,city,country,headimgurl,privilege,[unionid]}
  2470. * 注意:unionid字段 只有在用户将公众号绑定到微信开放平台账号后,才会出现。建议调用前用isset()检测一下
  2471. */
  2472. public function getOauthUserinfo($access_token,$openid){
  2473. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::OAUTH_USERINFO_URL.'access_token='.$access_token.'&openid='.$openid);
  2474. if ($result)
  2475. {
  2476. $json = json_decode($result,true);
  2477. if (!$json || !empty($json['errcode'])) {
  2478. $this->errCode = $json['errcode'];
  2479. $this->errMsg = $json['errmsg'];
  2480. return false;
  2481. }
  2482. return $json;
  2483. }
  2484. return false;
  2485. }
  2486. /**
  2487. * 检验授权凭证是否有效
  2488. * @param string $access_token
  2489. * @param string $openid
  2490. * @return boolean 是否有效
  2491. */
  2492. public function getOauthAuth($access_token,$openid){
  2493. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::OAUTH_AUTH_URL.'access_token='.$access_token.'&openid='.$openid);
  2494. if ($result)
  2495. {
  2496. $json = json_decode($result,true);
  2497. if (!$json || !empty($json['errcode'])) {
  2498. $this->errCode = $json['errcode'];
  2499. $this->errMsg = $json['errmsg'];
  2500. return false;
  2501. } else
  2502. if ($json['errcode']==0) return true;
  2503. }
  2504. return false;
  2505. }
  2506. /**
  2507. * 模板消息 设置所属行业
  2508. * @param int $id1 公众号模板消息所属行业编号,参看官方开发文档 行业代码
  2509. * @param int $id2 同$id1。但如果只有一个行业,此参数可省略
  2510. * @return boolean|array
  2511. */
  2512. public function setTMIndustry($id1,$id2=''){
  2513. if ($id1) $data['industry_id1'] = $id1;
  2514. if ($id2) $data['industry_id2'] = $id2;
  2515. if (!$this->access_token && !$this->checkAuth()) return false;
  2516. $result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_SET_INDUSTRY_URL.'access_token='.$this->access_token,self::json_encode($data));
  2517. if($result){
  2518. $json = json_decode($result,true);
  2519. if (!$json || !empty($json['errcode'])) {
  2520. $this->errCode = $json['errcode'];
  2521. $this->errMsg = $json['errmsg'];
  2522. return false;
  2523. }
  2524. return $json;
  2525. }
  2526. return false;
  2527. }
  2528. /**
  2529. * 模板消息 添加消息模板
  2530. * 成功返回消息模板的调用id
  2531. * @param string $tpl_id 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
  2532. * @return boolean|string
  2533. */
  2534. public function addTemplateMessage($tpl_id){
  2535. $data = array ('template_id_short' =>$tpl_id);
  2536. if (!$this->access_token && !$this->checkAuth()) return false;
  2537. $result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_ADD_TPL_URL.'access_token='.$this->access_token,self::json_encode($data));
  2538. if($result){
  2539. $json = json_decode($result,true);
  2540. if (!$json || !empty($json['errcode'])) {
  2541. $this->errCode = $json['errcode'];
  2542. $this->errMsg = $json['errmsg'];
  2543. return false;
  2544. }
  2545. return $json['template_id'];
  2546. }
  2547. return false;
  2548. }
  2549. /**
  2550. * 发送模板消息
  2551. * @param array $data 消息结构
  2552. * {
  2553. "touser":"OPENID",
  2554. "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
  2555. "url":"http://weixin.qq.com/download",
  2556. "topcolor":"#FF0000",
  2557. "data":{
  2558. "参数名1": {
  2559. "value":"参数",
  2560. "color":"#173177" //参数颜色
  2561. },
  2562. "Date":{
  2563. "value":"06月07日 19时24分",
  2564. "color":"#173177"
  2565. },
  2566. "CardNumber":{
  2567. "value":"0426",
  2568. "color":"#173177"
  2569. },
  2570. "Type":{
  2571. "value":"消费",
  2572. "color":"#173177"
  2573. }
  2574. }
  2575. }
  2576. * @return boolean|array
  2577. */
  2578. public function sendTemplateMessage($data){
  2579. if (!$this->access_token && !$this->checkAuth()) return false;
  2580. $result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
  2581. if($result){
  2582. $json = json_decode($result,true);
  2583. if (!$json || !empty($json['errcode'])) {
  2584. $this->errCode = $json['errcode'];
  2585. $this->errMsg = $json['errmsg'];
  2586. return false;
  2587. }
  2588. return $json;
  2589. }
  2590. return false;
  2591. }
  2592. /**
  2593. * 获取多客服会话记录
  2594. * @param array $data 数据结构{"starttime":123456789,"endtime":987654321,"openid":"OPENID","pagesize":10,"pageindex":1,}
  2595. * @return boolean|array
  2596. */
  2597. public function getCustomServiceMessage($data){
  2598. if (!$this->access_token && !$this->checkAuth()) return false;
  2599. $result = $this->http_post(self::API_URL_PREFIX.self::CUSTOM_SERVICE_GET_RECORD.'access_token='.$this->access_token,self::json_encode($data));
  2600. if ($result)
  2601. {
  2602. $json = json_decode($result,true);
  2603. if (!$json || !empty($json['errcode'])) {
  2604. $this->errCode = $json['errcode'];
  2605. $this->errMsg = $json['errmsg'];
  2606. return false;
  2607. }
  2608. return $json;
  2609. }
  2610. return false;
  2611. }
  2612. /**
  2613. * 转发多客服消息
  2614. * Example: $obj->transfer_customer_service($customer_account)->reply();
  2615. * @param string $customer_account 转发到指定客服帐号:test1@test
  2616. */
  2617. public function transfer_customer_service($customer_account = '')
  2618. {
  2619. $msg = array(
  2620. 'ToUserName' => $this->getRevFrom(),
  2621. 'FromUserName'=>$this->getRevTo(),
  2622. 'CreateTime'=>time(),
  2623. 'MsgType'=>'transfer_customer_service',
  2624. );
  2625. if ($customer_account) {
  2626. $msg['TransInfo'] = array('KfAccount'=>$customer_account);
  2627. }
  2628. $this->Message($msg);
  2629. return $this;
  2630. }
  2631. /**
  2632. * 获取多客服客服基本信息
  2633. *
  2634. * @return boolean|array
  2635. */
  2636. public function getCustomServiceKFlist(){
  2637. if (!$this->access_token && !$this->checkAuth()) return false;
  2638. $result = $this->http_get(self::API_URL_PREFIX.self::CUSTOM_SERVICE_GET_KFLIST.'access_token='.$this->access_token);
  2639. if ($result)
  2640. {
  2641. $json = json_decode($result,true);
  2642. if (!$json || !empty($json['errcode'])) {
  2643. $this->errCode = $json['errcode'];
  2644. $this->errMsg = $json['errmsg'];
  2645. return false;
  2646. }
  2647. return $json;
  2648. }
  2649. return false;
  2650. }
  2651. /**
  2652. * 获取多客服在线客服接待信息
  2653. *
  2654. * @return boolean|array {
  2655. "kf_online_list": [
  2656. {
  2657. "kf_account": "test1@test", //客服账号@微信别名
  2658. "status": 1, //客服在线状态 1:pc在线,2:手机在线,若pc和手机同时在线则为 1+2=3
  2659. "kf_id": "1001", //客服工号
  2660. "auto_accept": 0, //客服设置的最大自动接入数
  2661. "accepted_case": 1 //客服当前正在接待的会话数
  2662. }
  2663. ]
  2664. }
  2665. */
  2666. public function getCustomServiceOnlineKFlist(){
  2667. if (!$this->access_token && !$this->checkAuth()) return false;
  2668. $result = $this->http_get(self::API_URL_PREFIX.self::CUSTOM_SERVICE_GET_ONLINEKFLIST.'access_token='.$this->access_token);
  2669. if ($result)
  2670. {
  2671. $json = json_decode($result,true);
  2672. if (!$json || !empty($json['errcode'])) {
  2673. $this->errCode = $json['errcode'];
  2674. $this->errMsg = $json['errmsg'];
  2675. return false;
  2676. }
  2677. return $json;
  2678. }
  2679. return false;
  2680. }
  2681. /**
  2682. * 创建指定多客服会话
  2683. * @tutorial 当用户已被其他客服接待或指定客服不在线则会失败
  2684. * @param string $openid //用户openid
  2685. * @param string $kf_account //客服账号
  2686. * @param string $text //附加信息,文本会展示在客服人员的多客服客户端,可为空
  2687. * @return boolean | array //成功返回json数组
  2688. * {
  2689. * "errcode": 0,
  2690. * "errmsg": "ok",
  2691. * }
  2692. */
  2693. public function createKFSession($openid,$kf_account,$text=''){
  2694. $data=array(
  2695. "openid" =>$openid,
  2696. "kf_account" => $kf_account
  2697. );
  2698. if ($text) $data["text"] = $text;
  2699. if (!$this->access_token && !$this->checkAuth()) return false;
  2700. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::CUSTOM_SESSION_CREATE.'access_token='.$this->access_token,self::json_encode($data));
  2701. if ($result)
  2702. {
  2703. $json = json_decode($result,true);
  2704. if (!$json || !empty($json['errcode'])) {
  2705. $this->errCode = $json['errcode'];
  2706. $this->errMsg = $json['errmsg'];
  2707. return false;
  2708. }
  2709. return $json;
  2710. }
  2711. return false;
  2712. }
  2713. /**
  2714. * 关闭指定多客服会话
  2715. * @tutorial 当用户被其他客服接待时则会失败
  2716. * @param string $openid //用户openid
  2717. * @param string $kf_account //客服账号
  2718. * @param string $text //附加信息,文本会展示在客服人员的多客服客户端,可为空
  2719. * @return boolean | array //成功返回json数组
  2720. * {
  2721. * "errcode": 0,
  2722. * "errmsg": "ok",
  2723. * }
  2724. */
  2725. public function closeKFSession($openid,$kf_account,$text=''){
  2726. $data=array(
  2727. "openid" =>$openid,
  2728. "kf_account" => $kf_account
  2729. );
  2730. if ($text) $data["text"] = $text;
  2731. if (!$this->access_token && !$this->checkAuth()) return false;
  2732. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::CUSTOM_SESSION_CLOSE .'access_token='.$this->access_token,self::json_encode($data));
  2733. if ($result)
  2734. {
  2735. $json = json_decode($result,true);
  2736. if (!$json || !empty($json['errcode'])) {
  2737. $this->errCode = $json['errcode'];
  2738. $this->errMsg = $json['errmsg'];
  2739. return false;
  2740. }
  2741. return $json;
  2742. }
  2743. return false;
  2744. }
  2745. /**
  2746. * 获取用户会话状态
  2747. * @param string $openid //用户openid
  2748. * @return boolean | array //成功返回json数组
  2749. * {
  2750. * "errcode" : 0,
  2751. * "errmsg" : "ok",
  2752. * "kf_account" : "test1@test", //正在接待的客服
  2753. * "createtime": 123456789, //会话接入时间
  2754. * }
  2755. */
  2756. public function getKFSession($openid){
  2757. if (!$this->access_token && !$this->checkAuth()) return false;
  2758. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::CUSTOM_SESSION_GET .'access_token='.$this->access_token.'&openid='.$openid);
  2759. if ($result)
  2760. {
  2761. $json = json_decode($result,true);
  2762. if (!$json || !empty($json['errcode'])) {
  2763. $this->errCode = $json['errcode'];
  2764. $this->errMsg = $json['errmsg'];
  2765. return false;
  2766. }
  2767. return $json;
  2768. }
  2769. return false;
  2770. }
  2771. /**
  2772. * 获取指定客服的会话列表
  2773. * @param string $openid //用户openid
  2774. * @return boolean | array //成功返回json数组
  2775. * array(
  2776. * 'sessionlist' => array (
  2777. * array (
  2778. * 'openid'=>'OPENID', //客户 openid
  2779. * 'createtime'=>123456789, //会话创建时间,UNIX 时间戳
  2780. * ),
  2781. * array (
  2782. * 'openid'=>'OPENID', //客户 openid
  2783. * 'createtime'=>123456789, //会话创建时间,UNIX 时间戳
  2784. * ),
  2785. * )
  2786. * )
  2787. */
  2788. public function getKFSessionlist($kf_account){
  2789. if (!$this->access_token && !$this->checkAuth()) return false;
  2790. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::CUSTOM_SESSION_GET_LIST .'access_token='.$this->access_token.'&kf_account='.$kf_account);
  2791. if ($result)
  2792. {
  2793. $json = json_decode($result,true);
  2794. if (!$json || !empty($json['errcode'])) {
  2795. $this->errCode = $json['errcode'];
  2796. $this->errMsg = $json['errmsg'];
  2797. return false;
  2798. }
  2799. return $json;
  2800. }
  2801. return false;
  2802. }
  2803. /**
  2804. * 获取未接入会话列表
  2805. * @param string $openid //用户openid
  2806. * @return boolean | array //成功返回json数组
  2807. * array (
  2808. * 'count' => 150 , //未接入会话数量
  2809. * 'waitcaselist' => array (
  2810. * array (
  2811. * 'openid'=>'OPENID', //客户 openid
  2812. * 'kf_account ' =>'', //指定接待的客服,为空则未指定
  2813. * 'createtime'=>123456789, //会话创建时间,UNIX 时间戳
  2814. * ),
  2815. * array (
  2816. * 'openid'=>'OPENID', //客户 openid
  2817. * 'kf_account ' =>'', //指定接待的客服,为空则未指定
  2818. * 'createtime'=>123456789, //会话创建时间,UNIX 时间戳
  2819. * )
  2820. * )
  2821. * )
  2822. */
  2823. public function getKFSessionWait(){
  2824. if (!$this->access_token && !$this->checkAuth()) return false;
  2825. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::CUSTOM_SESSION_GET_WAIT .'access_token='.$this->access_token);
  2826. if ($result)
  2827. {
  2828. $json = json_decode($result,true);
  2829. if (!$json || !empty($json['errcode'])) {
  2830. $this->errCode = $json['errcode'];
  2831. $this->errMsg = $json['errmsg'];
  2832. return false;
  2833. }
  2834. return $json;
  2835. }
  2836. return false;
  2837. }
  2838. /**
  2839. * 添加客服账号
  2840. *
  2841. * @param string $account //完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符
  2842. * @param string $nickname //客服昵称,最长6个汉字或12个英文字符
  2843. * @param string $password //客服账号明文登录密码,会自动加密
  2844. * @return boolean|array
  2845. * 成功返回结果
  2846. * {
  2847. * "errcode": 0,
  2848. * "errmsg": "ok",
  2849. * }
  2850. */
  2851. public function addKFAccount($account,$nickname,$password){
  2852. $data=array(
  2853. "kf_account" =>$account,
  2854. "nickname" => $nickname,
  2855. "password" => md5($password)
  2856. );
  2857. if (!$this->access_token && !$this->checkAuth()) return false;
  2858. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::CS_KF_ACCOUNT_ADD_URL.'access_token='.$this->access_token,self::json_encode($data));
  2859. if ($result)
  2860. {
  2861. $json = json_decode($result,true);
  2862. if (!$json || !empty($json['errcode'])) {
  2863. $this->errCode = $json['errcode'];
  2864. $this->errMsg = $json['errmsg'];
  2865. return false;
  2866. }
  2867. return $json;
  2868. }
  2869. return false;
  2870. }
  2871. /**
  2872. * 修改客服账号信息
  2873. *
  2874. * @param string $account //完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符
  2875. * @param string $nickname //客服昵称,最长6个汉字或12个英文字符
  2876. * @param string $password //客服账号明文登录密码,会自动加密
  2877. * @return boolean|array
  2878. * 成功返回结果
  2879. * {
  2880. * "errcode": 0,
  2881. * "errmsg": "ok",
  2882. * }
  2883. */
  2884. public function updateKFAccount($account,$nickname,$password){
  2885. $data=array(
  2886. "kf_account" =>$account,
  2887. "nickname" => $nickname,
  2888. "password" => md5($password)
  2889. );
  2890. if (!$this->access_token && !$this->checkAuth()) return false;
  2891. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::CS_KF_ACCOUNT_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
  2892. if ($result)
  2893. {
  2894. $json = json_decode($result,true);
  2895. if (!$json || !empty($json['errcode'])) {
  2896. $this->errCode = $json['errcode'];
  2897. $this->errMsg = $json['errmsg'];
  2898. return false;
  2899. }
  2900. return $json;
  2901. }
  2902. return false;
  2903. }
  2904. /**
  2905. * 删除客服账号
  2906. *
  2907. * @param string $account //完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符
  2908. * @return boolean|array
  2909. * 成功返回结果
  2910. * {
  2911. * "errcode": 0,
  2912. * "errmsg": "ok",
  2913. * }
  2914. */
  2915. public function deleteKFAccount($account){
  2916. if (!$this->access_token && !$this->checkAuth()) return false;
  2917. $result = $this->http_get(self::API_BASE_URL_PREFIX.self::CS_KF_ACCOUNT_DEL_URL.'access_token='.$this->access_token.'&kf_account='.$account);
  2918. if ($result)
  2919. {
  2920. $json = json_decode($result,true);
  2921. if (!$json || !empty($json['errcode'])) {
  2922. $this->errCode = $json['errcode'];
  2923. $this->errMsg = $json['errmsg'];
  2924. return false;
  2925. }
  2926. return $json;
  2927. }
  2928. return false;
  2929. }
  2930. /**
  2931. * 上传客服头像
  2932. *
  2933. * @param string $account //完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符
  2934. * @param string $imgfile //头像文件完整路径,如:'D:\user.jpg'。头像文件必须JPG格式,像素建议640*640
  2935. * @return boolean|array
  2936. * 成功返回结果
  2937. * {
  2938. * "errcode": 0,
  2939. * "errmsg": "ok",
  2940. * }
  2941. */
  2942. public function setKFHeadImg($account,$imgfile){
  2943. if (!$this->access_token && !$this->checkAuth()) return false;
  2944. $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);
  2945. if ($result)
  2946. {
  2947. $json = json_decode($result,true);
  2948. if (!$json || !empty($json['errcode'])) {
  2949. $this->errCode = $json['errcode'];
  2950. $this->errMsg = $json['errmsg'];
  2951. return false;
  2952. }
  2953. return $json;
  2954. }
  2955. return false;
  2956. }
  2957. /**
  2958. * 语义理解接口
  2959. * @param String $uid 用户唯一id(非开发者id),用户区分公众号下的不同用户(建议填入用户openid)
  2960. * @param String $query 输入文本串
  2961. * @param String $category 需要使用的服务类型,多个用“,”隔开,不能为空
  2962. * @param Float $latitude 纬度坐标,与经度同时传入;与城市二选一传入
  2963. * @param Float $longitude 经度坐标,与纬度同时传入;与城市二选一传入
  2964. * @param String $city 城市名称,与经纬度二选一传入
  2965. * @param String $region 区域名称,在城市存在的情况下可省略;与经纬度二选一传入
  2966. * @return boolean|array
  2967. */
  2968. public function querySemantic($uid,$query,$category,$latitude=0,$longitude=0,$city="",$region=""){
  2969. if (!$this->access_token && !$this->checkAuth()) return false;
  2970. $data=array(
  2971. 'query' => $query,
  2972. 'category' => $category,
  2973. 'appid' => $this->appid,
  2974. 'uid' => ''
  2975. );
  2976. //地理坐标或城市名称二选一
  2977. if ($latitude) {
  2978. $data['latitude'] = $latitude;
  2979. $data['longitude'] = $longitude;
  2980. } elseif ($city) {
  2981. $data['city'] = $city;
  2982. } elseif ($region) {
  2983. $data['region'] = $region;
  2984. }
  2985. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::SEMANTIC_API_URL.'access_token='.$this->access_token,self::json_encode($data));
  2986. if ($result)
  2987. {
  2988. $json = json_decode($result,true);
  2989. if (!$json || !empty($json['errcode'])) {
  2990. $this->errCode = $json['errcode'];
  2991. $this->errMsg = $json['errmsg'];
  2992. return false;
  2993. }
  2994. return $json;
  2995. }
  2996. return false;
  2997. }
  2998. /**
  2999. * 创建卡券
  3000. * @param Array $data 卡券数据
  3001. * @return array|boolean 返回数组中card_id为卡券ID
  3002. */
  3003. public function createCard($data) {
  3004. if (!$this->access_token && !$this->checkAuth()) return false;
  3005. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CREATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3006. if ($result) {
  3007. $json = json_decode($result, true);
  3008. if (!$json || !empty($json['errcode'])) {
  3009. $this->errCode = $json['errcode'];
  3010. $this->errMsg = $json['errmsg'];
  3011. return false;
  3012. }
  3013. return $json;
  3014. }
  3015. return false;
  3016. }
  3017. /**
  3018. * 更改卡券信息
  3019. * 调用该接口更新信息后会重新送审,卡券状态变更为待审核。已被用户领取的卡券会实时更新票面信息。
  3020. * @param string $data
  3021. * @return boolean
  3022. */
  3023. public function updateCard($data) {
  3024. if (!$this->access_token && !$this->checkAuth()) return false;
  3025. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3026. if ($result) {
  3027. $json = json_decode($result, true);
  3028. if (!$json || !empty($json['errcode'])) {
  3029. $this->errCode = $json['errcode'];
  3030. $this->errMsg = $json['errmsg'];
  3031. return false;
  3032. }
  3033. return true;
  3034. }
  3035. return false;
  3036. }
  3037. /**
  3038. * 删除卡券
  3039. * 允许商户删除任意一类卡券。删除卡券后,该卡券对应已生成的领取用二维码、添加到卡包 JS API 均会失效。
  3040. * 注意:删除卡券不能删除已被用户领取,保存在微信客户端中的卡券,已领取的卡券依旧有效。
  3041. * @param string $card_id 卡券ID
  3042. * @return boolean
  3043. */
  3044. public function delCard($card_id) {
  3045. $data = array(
  3046. 'card_id' => $card_id,
  3047. );
  3048. if (!$this->access_token && !$this->checkAuth()) return false;
  3049. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_DELETE . 'access_token=' . $this->access_token, self::json_encode($data));
  3050. if ($result) {
  3051. $json = json_decode($result, true);
  3052. if (!$json || !empty($json['errcode'])) {
  3053. $this->errCode = $json['errcode'];
  3054. $this->errMsg = $json['errmsg'];
  3055. return false;
  3056. }
  3057. return true;
  3058. }
  3059. return false;
  3060. }
  3061. /**
  3062. * 查询卡券详情
  3063. * @param string $card_id
  3064. * @return boolean|array 返回数组信息比较复杂,请参看卡券接口文档
  3065. */
  3066. public function getCardInfo($card_id) {
  3067. $data = array(
  3068. 'card_id' => $card_id,
  3069. );
  3070. if (!$this->access_token && !$this->checkAuth()) return false;
  3071. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_GET . 'access_token=' . $this->access_token, self::json_encode($data));
  3072. if ($result) {
  3073. $json = json_decode($result, true);
  3074. if (!$json || !empty($json['errcode'])) {
  3075. $this->errCode = $json['errcode'];
  3076. $this->errMsg = $json['errmsg'];
  3077. return false;
  3078. }
  3079. return $json;
  3080. }
  3081. return false;
  3082. }
  3083. /**
  3084. * 获取用户已领取卡券接口
  3085. * @param string $openid
  3086. * @param string $card_id
  3087. * @return boolean|array 返回数组信息比较复杂,请参看卡券接口文档
  3088. * 成功返回结果
  3089. * {
  3090. * "errcode":0,
  3091. * "errmsg":"ok",
  3092. * "card_list": [
  3093. * {"code": "xxx1434079154", "card_id": "xxxxxxxxxx"},
  3094. * {"code": "xxx1434079155", "card_id": "xxxxxxxxxx"}
  3095. * ]
  3096. * }
  3097. */
  3098. public function getUserCardList($openid,$card_id) {
  3099. $data = array(
  3100. 'openid' => $openid,
  3101. 'card_id' => $card_id
  3102. );
  3103. if (!$this->access_token && !$this->checkAuth()) return false;
  3104. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_USER_GETCARDLIST . 'access_token=' . $this->access_token, self::json_encode($data));
  3105. if ($result) {
  3106. $json = json_decode($result, true);
  3107. if (!$json || !empty($json['errcode'])) {
  3108. $this->errCode = $json['errcode'];
  3109. $this->errMsg = $json['errmsg'];
  3110. return false;
  3111. }
  3112. return $json;
  3113. }
  3114. return false;
  3115. }
  3116. /**
  3117. * 获取颜色列表
  3118. * 获得卡券的最新颜色列表,用于创建卡券
  3119. * @return boolean|array 返回数组请参看 微信卡券接口文档 的json格式
  3120. */
  3121. public function getCardColors() {
  3122. if (!$this->access_token && !$this->checkAuth()) return false;
  3123. $result = $this->http_get(self::API_BASE_URL_PREFIX . self::CARD_GETCOLORS . 'access_token=' . $this->access_token);
  3124. if ($result) {
  3125. $json = json_decode($result, true);
  3126. if (!$json || !empty($json['errcode'])) {
  3127. $this->errCode = $json['errcode'];
  3128. $this->errMsg = $json['errmsg'];
  3129. return false;
  3130. }
  3131. return $json;
  3132. }
  3133. return false;
  3134. }
  3135. /**
  3136. * 拉取门店列表
  3137. * 获取在公众平台上申请创建的门店列表
  3138. * @param int $offset 开始拉取的偏移,默认为0从头开始
  3139. * @param int $count 拉取的数量,默认为0拉取全部
  3140. * @return boolean|array 返回数组请参看 微信卡券接口文档 的json格式
  3141. */
  3142. public function getCardLocations($offset=0,$count=0) {
  3143. $data=array(
  3144. 'offset'=>$offset,
  3145. 'count'=>$count
  3146. );
  3147. if (!$this->access_token && !$this->checkAuth()) return false;
  3148. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_LOCATION_BATCHGET . 'access_token=' . $this->access_token, self::json_encode($data));
  3149. if ($result) {
  3150. $json = json_decode($result, true);
  3151. if (!$json || !empty($json['errcode'])) {
  3152. $this->errCode = $json['errcode'];
  3153. $this->errMsg = $json['errmsg'];
  3154. return false;
  3155. }
  3156. return $json;
  3157. }
  3158. return false;
  3159. }
  3160. /**
  3161. * 批量导入门店信息
  3162. * @tutorial 返回插入的门店id列表,以逗号分隔。如果有插入失败的,则为-1,请自行核查是哪个插入失败
  3163. * @param array $data 数组形式的json数据,由于内容较多,具体内容格式请查看 微信卡券接口文档
  3164. * @return boolean|string 成功返回插入的门店id列表
  3165. */
  3166. public function addCardLocations($data) {
  3167. if (!$this->access_token && !$this->checkAuth()) return false;
  3168. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_LOCATION_BATCHADD . 'access_token=' . $this->access_token, self::json_encode($data));
  3169. if ($result) {
  3170. $json = json_decode($result, true);
  3171. if (!$json || !empty($json['errcode'])) {
  3172. $this->errCode = $json['errcode'];
  3173. $this->errMsg = $json['errmsg'];
  3174. return false;
  3175. }
  3176. return $json;
  3177. }
  3178. return false;
  3179. }
  3180. /**
  3181. * 生成卡券二维码
  3182. * 成功则直接返回ticket值,可以用 getQRUrl($ticket) 换取二维码url
  3183. *
  3184. * @param string $cardid 卡券ID 必须
  3185. * @param string $code 指定卡券 code 码,只能被领一次。use_custom_code 字段为 true 的卡券必须填写,非自定义 code 不必填写。
  3186. * @param string $openid 指定领取者的 openid,只有该用户能领取。bind_openid 字段为 true 的卡券必须填写,非自定义 openid 不必填写。
  3187. * @param int $expire_seconds 指定二维码的有效时间,范围是 60 ~ 1800 秒。不填默认为永久有效。
  3188. * @param boolean $is_unique_code 指定下发二维码,生成的二维码随机分配一个 code,领取后不可再次扫描。填写 true 或 false。默认 false。
  3189. * @param string $balance 红包余额,以分为单位。红包类型必填(LUCKY_MONEY),其他卡券类型不填。
  3190. * @return boolean|string
  3191. */
  3192. public function createCardQrcode($card_id,$code='',$openid='',$expire_seconds=0,$is_unique_code=false,$balance='') {
  3193. $card = array(
  3194. 'card_id' => $card_id
  3195. );
  3196. $data = array(
  3197. 'action_name' => "QR_CARD"
  3198. );
  3199. if ($code)
  3200. $card['code'] = $code;
  3201. if ($openid)
  3202. $card['openid'] = $openid;
  3203. if ($is_unique_code)
  3204. $card['is_unique_code'] = $is_unique_code;
  3205. if ($balance)
  3206. $card['balance'] = $balance;
  3207. if ($expire_seconds)
  3208. $data['expire_seconds'] = $expire_seconds;
  3209. $data['action_info'] = array('card' => $card);
  3210. if (!$this->access_token && !$this->checkAuth()) return false;
  3211. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_QRCODE_CREATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3212. if ($result) {
  3213. $json = json_decode($result, true);
  3214. if (!$json || !empty($json['errcode'])) {
  3215. $this->errCode = $json['errcode'];
  3216. $this->errMsg = $json['errmsg'];
  3217. return false;
  3218. }
  3219. return $json;
  3220. }
  3221. return false;
  3222. }
  3223. /**
  3224. * 消耗 code
  3225. * 自定义 code(use_custom_code 为 true)的优惠券,在 code 被核销时,必须调用此接口。
  3226. *
  3227. * @param string $code 要消耗的序列号
  3228. * @param string $card_id 要消耗序列号所述的 card_id,创建卡券时use_custom_code 填写 true 时必填。
  3229. * @return boolean|array
  3230. * {
  3231. * "errcode":0,
  3232. * "errmsg":"ok",
  3233. * "card":{"card_id":"pFS7Fjg8kV1IdDz01r4SQwMkuCKc"},
  3234. * "openid":"oFS7Fjl0WsZ9AMZqrI80nbIq8xrA"
  3235. * }
  3236. */
  3237. public function consumeCardCode($code,$card_id='') {
  3238. $data = array('code' => $code);
  3239. if ($card_id)
  3240. $data['card_id'] = $card_id;
  3241. if (!$this->access_token && !$this->checkAuth()) return false;
  3242. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_CONSUME . 'access_token=' . $this->access_token, self::json_encode($data));
  3243. if ($result) {
  3244. $json = json_decode($result, true);
  3245. if (!$json || !empty($json['errcode'])) {
  3246. $this->errCode = $json['errcode'];
  3247. $this->errMsg = $json['errmsg'];
  3248. return false;
  3249. }
  3250. return $json;
  3251. }
  3252. return false;
  3253. }
  3254. /**
  3255. * code 解码
  3256. * @param string $encrypt_code 通过 choose_card_info 获取的加密字符串
  3257. * @return boolean|array
  3258. * {
  3259. * "errcode":0,
  3260. * "errmsg":"ok",
  3261. * "code":"751234212312"
  3262. * }
  3263. */
  3264. public function decryptCardCode($encrypt_code) {
  3265. $data = array(
  3266. 'encrypt_code' => $encrypt_code,
  3267. );
  3268. if (!$this->access_token && !$this->checkAuth()) return false;
  3269. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_DECRYPT . 'access_token=' . $this->access_token, self::json_encode($data));
  3270. if ($result) {
  3271. $json = json_decode($result, true);
  3272. if (!$json || !empty($json['errcode'])) {
  3273. $this->errCode = $json['errcode'];
  3274. $this->errMsg = $json['errmsg'];
  3275. return false;
  3276. }
  3277. return $json;
  3278. }
  3279. return false;
  3280. }
  3281. /**
  3282. * 查询 code 的有效性(非自定义 code)
  3283. * @param string $code
  3284. * @return boolean|array
  3285. * {
  3286. * "errcode":0,
  3287. * "errmsg":"ok",
  3288. * "openid":"oFS7Fjl0WsZ9AMZqrI80nbIq8xrA", //用户 openid
  3289. * "card":{
  3290. * "card_id":"pFS7Fjg8kV1IdDz01r4SQwMkuCKc",
  3291. * "begin_time": 1404205036, //起始使用时间
  3292. * "end_time": 1404205036, //结束时间
  3293. * }
  3294. * }
  3295. */
  3296. public function checkCardCode($code) {
  3297. $data = array(
  3298. 'code' => $code,
  3299. );
  3300. if (!$this->access_token && !$this->checkAuth()) return false;
  3301. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_GET . 'access_token=' . $this->access_token, self::json_encode($data));
  3302. if ($result) {
  3303. $json = json_decode($result, true);
  3304. if (!$json || !empty($json['errcode'])) {
  3305. $this->errCode = $json['errcode'];
  3306. $this->errMsg = $json['errmsg'];
  3307. return false;
  3308. }
  3309. return $json;
  3310. }
  3311. return false;
  3312. }
  3313. /**
  3314. * 批量查询卡列表
  3315. * @param $offset 开始拉取的偏移,默认为0从头开始
  3316. * @param $count 需要查询的卡片的数量(数量最大50,默认50)
  3317. * @return boolean|array
  3318. * {
  3319. * "errcode":0,
  3320. * "errmsg":"ok",
  3321. * "card_id_list":["ph_gmt7cUVrlRk8swPwx7aDyF-pg"], //卡 id 列表
  3322. * "total_num":1 //该商户名下 card_id 总数
  3323. * }
  3324. */
  3325. public function getCardIdList($offset=0,$count=50) {
  3326. if ($count>50)
  3327. $count = 50;
  3328. $data = array(
  3329. 'offset' => $offset,
  3330. 'count' => $count,
  3331. );
  3332. if (!$this->access_token && !$this->checkAuth()) return false;
  3333. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_BATCHGET . 'access_token=' . $this->access_token, self::json_encode($data));
  3334. if ($result) {
  3335. $json = json_decode($result, true);
  3336. if (!$json || !empty($json['errcode'])) {
  3337. $this->errCode = $json['errcode'];
  3338. $this->errMsg = $json['errmsg'];
  3339. return false;
  3340. }
  3341. return $json;
  3342. }
  3343. return false;
  3344. }
  3345. /**
  3346. * 更改 code
  3347. * 为确保转赠后的安全性,微信允许自定义code的商户对已下发的code进行更改。
  3348. * 注:为避免用户疑惑,建议仅在发生转赠行为后(发生转赠后,微信会通过事件推送的方式告知商户被转赠的卡券code)对用户的code进行更改。
  3349. * @param string $code 卡券的 code 编码
  3350. * @param string $card_id 卡券 ID
  3351. * @param string $new_code 新的卡券 code 编码
  3352. * @return boolean
  3353. */
  3354. public function updateCardCode($code,$card_id,$new_code) {
  3355. $data = array(
  3356. 'code' => $code,
  3357. 'card_id' => $card_id,
  3358. 'new_code' => $new_code,
  3359. );
  3360. if (!$this->access_token && !$this->checkAuth()) return false;
  3361. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3362. if ($result) {
  3363. $json = json_decode($result, true);
  3364. if (!$json || !empty($json['errcode'])) {
  3365. $this->errCode = $json['errcode'];
  3366. $this->errMsg = $json['errmsg'];
  3367. return false;
  3368. }
  3369. return true;
  3370. }
  3371. return false;
  3372. }
  3373. /**
  3374. * 设置卡券失效
  3375. * 设置卡券失效的操作不可逆
  3376. * @param string $code 需要设置为失效的 code
  3377. * @param string $card_id 自定义 code 的卡券必填。非自定义 code 的卡券不填。
  3378. * @return boolean
  3379. */
  3380. public function unavailableCardCode($code,$card_id='') {
  3381. $data = array(
  3382. 'code' => $code,
  3383. );
  3384. if ($card_id)
  3385. $data['card_id'] = $card_id;
  3386. if (!$this->access_token && !$this->checkAuth()) return false;
  3387. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_UNAVAILABLE . 'access_token=' . $this->access_token, self::json_encode($data));
  3388. if ($result) {
  3389. $json = json_decode($result, true);
  3390. if (!$json || !empty($json['errcode'])) {
  3391. $this->errCode = $json['errcode'];
  3392. $this->errMsg = $json['errmsg'];
  3393. return false;
  3394. }
  3395. return true;
  3396. }
  3397. return false;
  3398. }
  3399. /**
  3400. * 库存修改
  3401. * @param string $data
  3402. * @return boolean
  3403. */
  3404. public function modifyCardStock($data) {
  3405. if (!$this->access_token && !$this->checkAuth()) return false;
  3406. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_MODIFY_STOCK . 'access_token=' . $this->access_token, self::json_encode($data));
  3407. if ($result) {
  3408. $json = json_decode($result, true);
  3409. if (!$json || !empty($json['errcode'])) {
  3410. $this->errCode = $json['errcode'];
  3411. $this->errMsg = $json['errmsg'];
  3412. return false;
  3413. }
  3414. return true;
  3415. }
  3416. return false;
  3417. }
  3418. /**
  3419. * 更新门票
  3420. * @param string $data
  3421. * @return boolean
  3422. */
  3423. public function updateMeetingCard($data) {
  3424. if (!$this->access_token && !$this->checkAuth()) return false;
  3425. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_MEETINGCARD_UPDATEUSER . 'access_token=' . $this->access_token, self::json_encode($data));
  3426. if ($result) {
  3427. $json = json_decode($result, true);
  3428. if (!$json || !empty($json['errcode'])) {
  3429. $this->errCode = $json['errcode'];
  3430. $this->errMsg = $json['errmsg'];
  3431. return false;
  3432. }
  3433. return true;
  3434. }
  3435. return false;
  3436. }
  3437. /**
  3438. * 激活/绑定会员卡
  3439. * @param string $data 具体结构请参看卡券开发文档(6.1.1 激活/绑定会员卡)章节
  3440. * @return boolean
  3441. */
  3442. public function activateMemberCard($data) {
  3443. if (!$this->access_token && !$this->checkAuth()) return false;
  3444. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_MEMBERCARD_ACTIVATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3445. if ($result) {
  3446. $json = json_decode($result, true);
  3447. if (!$json || !empty($json['errcode'])) {
  3448. $this->errCode = $json['errcode'];
  3449. $this->errMsg = $json['errmsg'];
  3450. return false;
  3451. }
  3452. return true;
  3453. }
  3454. return false;
  3455. }
  3456. /**
  3457. * 会员卡交易
  3458. * 会员卡交易后每次积分及余额变更需通过接口通知微信,便于后续消息通知及其他扩展功能。
  3459. * @param string $data 具体结构请参看卡券开发文档(6.1.2 会员卡交易)章节
  3460. * @return boolean|array
  3461. */
  3462. public function updateMemberCard($data) {
  3463. if (!$this->access_token && !$this->checkAuth()) return false;
  3464. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_MEMBERCARD_UPDATEUSER . 'access_token=' . $this->access_token, self::json_encode($data));
  3465. if ($result) {
  3466. $json = json_decode($result, true);
  3467. if (!$json || !empty($json['errcode'])) {
  3468. $this->errCode = $json['errcode'];
  3469. $this->errMsg = $json['errmsg'];
  3470. return false;
  3471. }
  3472. return $json;
  3473. }
  3474. return false;
  3475. }
  3476. /**
  3477. * 更新红包金额
  3478. * @param string $code 红包的序列号
  3479. * @param $balance 红包余额
  3480. * @param string $card_id 自定义 code 的卡券必填。非自定义 code 可不填。
  3481. * @return boolean|array
  3482. */
  3483. public function updateLuckyMoney($code,$balance,$card_id='') {
  3484. $data = array(
  3485. 'code' => $code,
  3486. 'balance' => $balance
  3487. );
  3488. if ($card_id)
  3489. $data['card_id'] = $card_id;
  3490. if (!$this->access_token && !$this->checkAuth()) return false;
  3491. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_LUCKYMONEY_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3492. if ($result) {
  3493. $json = json_decode($result, true);
  3494. if (!$json || !empty($json['errcode'])) {
  3495. $this->errCode = $json['errcode'];
  3496. $this->errMsg = $json['errmsg'];
  3497. return false;
  3498. }
  3499. return true;
  3500. }
  3501. return false;
  3502. }
  3503. /**
  3504. * 设置卡券测试白名单
  3505. * @param string $openid 测试的 openid 列表
  3506. * @param string $user 测试的微信号列表
  3507. * @return boolean
  3508. */
  3509. public function setCardTestWhiteList($openid=array(),$user=array()) {
  3510. $data = array();
  3511. if (count($openid) > 0)
  3512. $data['openid'] = $openid;
  3513. if (count($user) > 0)
  3514. $data['username'] = $user;
  3515. if (!$this->access_token && !$this->checkAuth()) return false;
  3516. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_TESTWHILELIST_SET . 'access_token=' . $this->access_token, self::json_encode($data));
  3517. if ($result) {
  3518. $json = json_decode($result, true);
  3519. if (!$json || !empty($json['errcode'])) {
  3520. $this->errCode = $json['errcode'];
  3521. $this->errMsg = $json['errmsg'];
  3522. return false;
  3523. }
  3524. return true;
  3525. }
  3526. return false;
  3527. }
  3528. /**
  3529. * 申请设备ID
  3530. * [applyShakeAroundDevice 申请配置设备所需的UUID、Major、Minor。
  3531. * 若激活率小于50%,不能新增设备。单次新增设备超过500 个,需走人工审核流程。
  3532. * 审核通过后,可用迒回的批次ID 用“查询设备列表”接口拉取本次申请的设备ID]
  3533. * @param array $data
  3534. * array(
  3535. * "quantity" => 3, //申请的设备ID 的数量,单次新增设备超过500 个,需走人工审核流程(必填)
  3536. * "apply_reason" => "测试",//申请理由(必填)
  3537. * "comment" => "测试专用", //备注(非必填)
  3538. * "poi_id" => 1234 //设备关联的门店ID(非必填)
  3539. * )
  3540. * @return boolean|mixed
  3541. * {
  3542. "data": {
  3543. "apply_id": 123,
  3544. "device_identifiers":[
  3545. {
  3546. "device_id":10100,
  3547. "uuid":"FDA50693-A4E2-4FB1-AFCF-C6EB07647825",
  3548. "major":10001,
  3549. "minor":10002
  3550. }
  3551. ]
  3552. },
  3553. "errcode": 0,
  3554. "errmsg": "success."
  3555. }
  3556. apply_id:申请的批次ID,可用在“查询设备列表”接口按批次查询本次申请成功的设备ID
  3557. device_identifiers:指定的设备ID 列表
  3558. device_id:设备编号
  3559. uuid、major、minor
  3560. audit_status:审核状态。0:审核未通过、1:审核中、2:审核已通过;审核会在三个工作日内完成
  3561. audit_comment:审核备注,包括审核不通过的原因
  3562. * @access public
  3563. * @author polo<gao.bo168@gmail.com>
  3564. * @version 2015-3-25 下午1:24:06
  3565. * @copyright Show More
  3566. */
  3567. public function applyShakeAroundDevice($data){
  3568. if (!$this->access_token && !$this->checkAuth()) return false;
  3569. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_APPLYID . 'access_token=' . $this->access_token, self::json_encode($data));
  3570. $this->log($result);
  3571. if ($result) {
  3572. $json = json_decode($result, true);
  3573. if (!$json || !empty($json['errcode'])) {
  3574. $this->errCode = $json['errcode'];
  3575. $this->errMsg = $json['errmsg'];
  3576. return false;
  3577. }
  3578. return $json;
  3579. }
  3580. return false;
  3581. }
  3582. /**
  3583. * 编辑设备信息
  3584. * [updateShakeAroundDevice 编辑设备的备注信息。可用设备ID或完整的UUID、Major、Minor指定设备,二者选其一。]
  3585. * @param array $data
  3586. * array(
  3587. * "device_identifier" => array(
  3588. * "device_id" => 10011, //当提供了device_id则不需要使用uuid、major、minor,反之亦然
  3589. * "uuid" => "FDA50693-A4E2-4FB1-AFCF-C6EB07647825",
  3590. * "major" => 1002,
  3591. * "minor" => 1223
  3592. * ),
  3593. * "comment" => "测试专用", //备注(非必填)
  3594. * )
  3595. * {
  3596. "data": {
  3597. },
  3598. "errcode": 0,
  3599. "errmsg": "success."
  3600. }
  3601. * @return boolean
  3602. * @author binsee<binsee@163.com>
  3603. * @version 2015-4-20 23:45:00
  3604. */
  3605. public function updateShakeAroundDevice($data){
  3606. if (!$this->access_token && !$this->checkAuth()) return false;
  3607. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3608. $this->log($result);
  3609. if ($result) {
  3610. $json = json_decode($result, true);
  3611. if (!$json || !empty($json['errcode'])) {
  3612. $this->errCode = $json['errcode'];
  3613. $this->errMsg = $json['errmsg'];
  3614. return false;
  3615. }
  3616. return true;
  3617. }
  3618. return false;
  3619. }
  3620. /**
  3621. * 查询设备列表
  3622. * [searchShakeAroundDevice 查询已有的设备ID、UUID、Major、Minor、激活状态、备注信息、关联门店、关联页面等信息。
  3623. * 可指定设备ID 或完整的UUID、Major、Minor 查询,也可批量拉取设备信息列表。]
  3624. * @param array $data
  3625. * $data 三种格式:
  3626. * ①查询指定设备时:$data = array(
  3627. * "device_identifiers" => array(
  3628. * array(
  3629. * "device_id" => 10100,
  3630. * "uuid" => "FDA50693-A4E2-4FB1-AFCF-C6EB07647825",
  3631. * "major" => 10001,
  3632. * "minor" => 10002
  3633. * )
  3634. * )
  3635. * );
  3636. * device_identifiers:指定的设备
  3637. * device_id:设备编号,若填了UUID、major、minor,则可不填设备编号,若二者都填,则以设备编号为优先
  3638. * uuid、major、minor:三个信息需填写完整,若填了设备编号,则可不填此信息
  3639. * +-------------------------------------------------------------------------------------------------------------
  3640. * ②需要分页查询或者指定范围内的设备时: $data = array(
  3641. * "begin" => 0,
  3642. * "count" => 3
  3643. * );
  3644. * begin:设备列表的起始索引值
  3645. * count:待查询的设备个数
  3646. * +-------------------------------------------------------------------------------------------------------------
  3647. * ③当需要根据批次ID 查询时: $data = array(
  3648. * "apply_id" => 1231,
  3649. * "begin" => 0,
  3650. * "count" => 3
  3651. * );
  3652. * apply_id:批次ID
  3653. * +-------------------------------------------------------------------------------------------------------------
  3654. * @return boolean|mixed
  3655. *正确迒回JSON 数据示例:
  3656. *字段说明
  3657. {
  3658. "data": {
  3659. "devices": [ //指定的设备信息列表
  3660. {
  3661. "comment": "", //设备的备注信息
  3662. "device_id": 10097, //设备编号
  3663. "major": 10001,
  3664. "minor": 12102,
  3665. "page_ids": "15369", //与此设备关联的页面ID 列表,用逗号隔开
  3666. "status": 1, //激活状态,0:未激活,1:已激活(但不活跃),2:活跃
  3667. "poi_id": 0, //门店ID
  3668. "uuid": "FDA50693-A4E2-4FB1-AFCF-C6EB07647825"
  3669. },
  3670. {
  3671. "comment": "", //设备的备注信息
  3672. "device_id": 10098, //设备编号
  3673. "major": 10001,
  3674. "minor": 12103,
  3675. "page_ids": "15368", //与此设备关联的页面ID 列表,用逗号隔开
  3676. "status": 1, //激活状态,0:未激活,1:已激活(但不活跃),2:活跃
  3677. "poi_id": 0, //门店ID
  3678. "uuid": "FDA50693-A4E2-4FB1-AFCF-C6EB07647825"
  3679. }
  3680. ],
  3681. "total_count": 151 //商户名下的设备总量
  3682. },
  3683. "errcode": 0,
  3684. "errmsg": "success."
  3685. }
  3686. * @access public
  3687. * @author polo<gao.bo168@gmail.com>
  3688. * @version 2015-3-25 下午1:45:42
  3689. * @copyright Show More
  3690. */
  3691. public function searchShakeAroundDevice($data){
  3692. if (!$this->access_token && !$this->checkAuth()) return false;
  3693. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_SEARCH . 'access_token=' . $this->access_token, self::json_encode($data));
  3694. $this->log($result);
  3695. if ($result) {
  3696. $json = json_decode($result, true);
  3697. if (!$json || !empty($json['errcode'])) {
  3698. $this->errCode = $json['errcode'];
  3699. $this->errMsg = $json['errmsg'];
  3700. return false;
  3701. }
  3702. return $json;
  3703. }
  3704. return false;
  3705. }
  3706. /**
  3707. * [bindLocationShakeAroundDevice 配置设备与门店的关联关系]
  3708. * @param string $device_id 设备编号,若填了UUID、major、minor,则可不填设备编号,若二者都填,则以设备编号为优先
  3709. * @param int $poi_id 待关联的门店ID
  3710. * @param string $uuid UUID、major、minor,三个信息需填写完整,若填了设备编号,则可不填此信息
  3711. * @param int $major
  3712. * @param int $minor
  3713. * @return boolean|mixed
  3714. * 正确返回JSON 数据示例:
  3715. * {
  3716. "data": {
  3717. },
  3718. "errcode": 0,
  3719. "errmsg": "success."
  3720. }
  3721. * @access public
  3722. * @author polo<gao.bo168@gmail.com>
  3723. * @version 2015-4-21 00:14:00
  3724. * @copyright Show More
  3725. */
  3726. public function bindLocationShakeAroundDevice($device_id,$poi_id,$uuid='',$major=0,$minor=0){
  3727. if (!$this->access_token && !$this->checkAuth()) return false;
  3728. if(!$device_id){
  3729. if(!$uuid || !$major || !$minor){
  3730. return false;
  3731. }
  3732. $device_identifier = array(
  3733. 'uuid' => $uuid,
  3734. 'major' => $major,
  3735. 'minor' => $minor
  3736. );
  3737. }else{
  3738. $device_identifier = array(
  3739. 'device_id' => $device_id
  3740. );
  3741. }
  3742. $data = array(
  3743. 'device_identifier' => $device_identifier,
  3744. 'poi_id' => $poi_id
  3745. );
  3746. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_BINDLOCATION . 'access_token=' . $this->access_token, self::json_encode($data));
  3747. $this->log($result);
  3748. if ($result) {
  3749. $json = json_decode($result, true);
  3750. if (!$json || !empty($json['errcode'])) {
  3751. $this->errCode = $json['errcode'];
  3752. $this->errMsg = $json['errmsg'];
  3753. return false;
  3754. }
  3755. return $json; //这个可以更改为返回true
  3756. }
  3757. return false;
  3758. }
  3759. /**
  3760. * [bindPageShakeAroundDevice 配置设备与页面的关联关系。
  3761. * 支持建立或解除关联关系,也支持新增页面或覆盖页面等操作。
  3762. * 配置完成后,在此设备的信号范围内,即可摇出关联的页面信息。
  3763. * 若设备配置多个页面,则随机出现页面信息]
  3764. * @param string $device_id 设备编号,若填了UUID、major、minor,则可不填设备编号,若二者都填,则以设备编号为优先
  3765. * @param array $page_ids 待关联的页面列表
  3766. * @param number $bind 关联操作标志位, 0 为解除关联关系,1 为建立关联关系
  3767. * @param number $append 新增操作标志位, 0 为覆盖,1 为新增
  3768. * @param string $uuid UUID、major、minor,三个信息需填写完整,若填了设备编号,则可不填此信息
  3769. * @param int $major
  3770. * @param int $minor
  3771. * @return boolean|mixed
  3772. * 正确返回JSON 数据示例:
  3773. * {
  3774. "data": {
  3775. },
  3776. "errcode": 0,
  3777. "errmsg": "success."
  3778. }
  3779. * @access public
  3780. * @author polo<gao.bo168@gmail.com>
  3781. * @version 2015-4-21 00:31:00
  3782. * @copyright Show More
  3783. */
  3784. public function bindPageShakeAroundDevice($device_id,$page_ids=array(),$bind=1,$append=1,$uuid='',$major=0,$minor=0){
  3785. if (!$this->access_token && !$this->checkAuth()) return false;
  3786. if(!$device_id){
  3787. if(!$uuid || !$major || !$minor){
  3788. return false;
  3789. }
  3790. $device_identifier = array(
  3791. 'uuid' => $uuid,
  3792. 'major' => $major,
  3793. 'minor' => $minor
  3794. );
  3795. }else{
  3796. $device_identifier = array(
  3797. 'device_id' => $device_id
  3798. );
  3799. }
  3800. $data = array(
  3801. 'device_identifier' => $device_identifier,
  3802. 'page_ids' => $page_ids,
  3803. 'bind' => $bind,
  3804. 'append' => $append
  3805. );
  3806. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_BINDPAGE . 'access_token=' . $this->access_token, self::json_encode($data));
  3807. $this->log($result);
  3808. if ($result) {
  3809. $json = json_decode($result, true);
  3810. if (!$json || !empty($json['errcode'])) {
  3811. $this->errCode = $json['errcode'];
  3812. $this->errMsg = $json['errmsg'];
  3813. return false;
  3814. }
  3815. return $json;
  3816. }
  3817. return false;
  3818. }
  3819. /**
  3820. * 上传在摇一摇页面展示的图片素材
  3821. * 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
  3822. * @param array $data {"media":'@Path\filename.jpg'} 格式限定为:jpg,jpeg,png,gif,图片大小建议120px*120 px,限制不超过200 px *200 px,图片需为正方形。
  3823. * @return boolean|array
  3824. * {
  3825. "data": {
  3826. "pic_url":"http://shp.qpic.cn/wechat_shakearound_pic/0/1428377032e9dd2797018cad79186e03e8c5aec8dc/120"
  3827. },
  3828. "errcode": 0,
  3829. "errmsg": "success."
  3830. }
  3831. }
  3832. * @author binsee<binsee@163.com>
  3833. * @version 2015-4-21 00:51:00
  3834. */
  3835. public function uploadShakeAroundMedia($data){
  3836. if (!$this->access_token && !$this->checkAuth()) return false;
  3837. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::SHAKEAROUND_MATERIAL_ADD.'access_token='.$this->access_token,$data,true);
  3838. if ($result)
  3839. {
  3840. $json = json_decode($result,true);
  3841. if (!$json || !empty($json['errcode'])) {
  3842. $this->errCode = $json['errcode'];
  3843. $this->errMsg = $json['errmsg'];
  3844. return false;
  3845. }
  3846. return $json;
  3847. }
  3848. return false;
  3849. }
  3850. /**
  3851. * [addShakeAroundPage 增加摇一摇出来的页面信息,包括在摇一摇页面出现的主标题、副标题、图片和点击进去的超链接。]
  3852. * @param string $title 在摇一摇页面展示的主标题,不超过6 个字
  3853. * @param string $description 在摇一摇页面展示的副标题,不超过7 个字
  3854. * @param sting $icon_url 在摇一摇页面展示的图片, 格式限定为:jpg,jpeg,png,gif; 建议120*120 , 限制不超过200*200
  3855. * @param string $page_url 跳转链接
  3856. * @param string $comment 页面的备注信息,不超过15 个字,可不填
  3857. * @return boolean|mixed
  3858. * 正确返回JSON 数据示例:
  3859. * {
  3860. "data": {
  3861. "page_id": 28840 //新增页面的页面id
  3862. }
  3863. "errcode": 0,
  3864. "errmsg": "success."
  3865. }
  3866. * @access public
  3867. * @author polo<gao.bo168@gmail.com>
  3868. * @version 2015-3-25 下午2:57:09
  3869. * @copyright Show More
  3870. */
  3871. public function addShakeAroundPage($title,$description,$icon_url,$page_url,$comment=''){
  3872. if (!$this->access_token && !$this->checkAuth()) return false;
  3873. $data = array(
  3874. "title" => $title,
  3875. "description" => $description,
  3876. "icon_url" => $icon_url,
  3877. "page_url" => $page_url,
  3878. "comment" => $comment
  3879. );
  3880. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_PAGE_ADD . 'access_token=' . $this->access_token, self::json_encode($data));
  3881. $this->log($result);
  3882. if ($result) {
  3883. $json = json_decode($result, true);
  3884. if (!$json || !empty($json['errcode'])) {
  3885. $this->errCode = $json['errcode'];
  3886. $this->errMsg = $json['errmsg'];
  3887. return false;
  3888. }
  3889. return $json;
  3890. }
  3891. return false;
  3892. }
  3893. /**
  3894. * [updateShakeAroundPage 编辑摇一摇出来的页面信息,包括在摇一摇页面出现的主标题、副标题、图片和点击进去的超链接。]
  3895. * @param int $page_id
  3896. * @param string $title 在摇一摇页面展示的主标题,不超过6 个字
  3897. * @param string $description 在摇一摇页面展示的副标题,不超过7 个字
  3898. * @param sting $icon_url 在摇一摇页面展示的图片, 格式限定为:jpg,jpeg,png,gif; 建议120*120 , 限制不超过200*200
  3899. * @param string $page_url 跳转链接
  3900. * @param string $comment 页面的备注信息,不超过15 个字,可不填
  3901. * @return boolean|mixed
  3902. * 正确返回JSON 数据示例:
  3903. * {
  3904. "data": {
  3905. "page_id": 28840 //编辑页面的页面ID
  3906. }
  3907. "errcode": 0,
  3908. "errmsg": "success."
  3909. }
  3910. * @access public
  3911. * @author polo<gao.bo168@gmail.com>
  3912. * @version 2015-3-25 下午3:02:51
  3913. * @copyright Show More
  3914. */
  3915. public function updateShakeAroundPage($page_id,$title,$description,$icon_url,$page_url,$comment=''){
  3916. if (!$this->access_token && !$this->checkAuth()) return false;
  3917. $data = array(
  3918. "page_id" => $page_id,
  3919. "title" => $title,
  3920. "description" => $description,
  3921. "icon_url" => $icon_url,
  3922. "page_url" => $page_url,
  3923. "comment" => $comment
  3924. );
  3925. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_PAGE_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
  3926. $this->log($result);
  3927. if ($result) {
  3928. $json = json_decode($result, true);
  3929. if (!$json || !empty($json['errcode'])) {
  3930. $this->errCode = $json['errcode'];
  3931. $this->errMsg = $json['errmsg'];
  3932. return false;
  3933. }
  3934. return $json;
  3935. }
  3936. return false;
  3937. }
  3938. /**
  3939. * [searchShakeAroundPage 查询已有的页面,包括在摇一摇页面出现的主标题、副标题、图片和点击进去的超链接。
  3940. * 提供两种查询方式,①可指定页面ID 查询,②也可批量拉取页面列表。]
  3941. * @param array $page_ids
  3942. * @param int $begin
  3943. * @param int $count
  3944. * ①需要查询指定页面时:
  3945. * {
  3946. "page_ids":[12345, 23456, 34567]
  3947. }
  3948. * +-------------------------------------------------------------------------------------------------------------
  3949. * ②需要分页查询或者指定范围内的页面时:
  3950. * {
  3951. "begin": 0,
  3952. "count": 3
  3953. }
  3954. * +-------------------------------------------------------------------------------------------------------------
  3955. * @return boolean|mixed
  3956. * 正确返回JSON 数据示例:
  3957. {
  3958. "data": {
  3959. "pages": [
  3960. {
  3961. "comment": "just for test",
  3962. "description": "test",
  3963. "icon_url": "https://www.baidu.com/img/bd_logo1.png",
  3964. "page_id": 28840,
  3965. "page_url": "http://xw.qq.com/testapi1",
  3966. "title": "测试1"
  3967. },
  3968. {
  3969. "comment": "just for test",
  3970. "description": "test",
  3971. "icon_url": "https://www.baidu.com/img/bd_logo1.png",
  3972. "page_id": 28842,
  3973. "page_url": "http://xw.qq.com/testapi2",
  3974. "title": "测试2"
  3975. }
  3976. ],
  3977. "total_count": 2
  3978. },
  3979. "errcode": 0,
  3980. "errmsg": "success."
  3981. }
  3982. *字段说明:
  3983. *total_count 商户名下的页面总数
  3984. *page_id 摇周边页面唯一ID
  3985. *title 在摇一摇页面展示的主标题
  3986. *description 在摇一摇页面展示的副标题
  3987. *icon_url 在摇一摇页面展示的图片
  3988. *page_url 跳转链接
  3989. *comment 页面的备注信息
  3990. * @access public
  3991. * @author polo<gao.bo168@gmail.com>
  3992. * @version 2015-3-25 下午3:12:17
  3993. * @copyright Show More
  3994. */
  3995. public function searchShakeAroundPage($page_ids=array(),$begin=0,$count=1){
  3996. if (!$this->access_token && !$this->checkAuth()) return false;
  3997. if(!empty($page_ids)){
  3998. $data = array(
  3999. 'page_ids' => $page_ids
  4000. );
  4001. }else{
  4002. $data = array(
  4003. 'begin' => $begin,
  4004. 'count' => $count
  4005. );
  4006. }
  4007. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_PAGE_SEARCH . 'access_token=' . $this->access_token, self::json_encode($data));
  4008. $this->log($result);
  4009. if ($result) {
  4010. $json = json_decode($result, true);
  4011. if (!$json || !empty($json['errcode'])) {
  4012. $this->errCode = $json['errcode'];
  4013. $this->errMsg = $json['errmsg'];
  4014. return false;
  4015. }
  4016. return $json;
  4017. }
  4018. return false;
  4019. }
  4020. /**
  4021. * [deleteShakeAroundPage 删除已有的页面,包括在摇一摇页面出现的主标题、副标题、图片和点击进去的超链接。
  4022. * 只有页面与设备没有关联关系时,才可被删除。]
  4023. * @param array $page_ids
  4024. * {
  4025. "page_ids":[12345,23456,34567]
  4026. }
  4027. * @return boolean|mixed
  4028. * 正确返回JSON 数据示例:
  4029. * {
  4030. "data": {
  4031. },
  4032. "errcode": 0,
  4033. "errmsg": "success."
  4034. }
  4035. * @access public
  4036. * @author polo<gao.bo168@gmail.com>
  4037. * @version 2015-3-25 下午3:23:00
  4038. * @copyright Show More
  4039. */
  4040. public function deleteShakeAroundPage($page_ids=array()){
  4041. if (!$this->access_token && !$this->checkAuth()) return false;
  4042. $data = array(
  4043. 'page_ids' => $page_ids
  4044. );
  4045. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_PAGE_DELETE . 'access_token=' . $this->access_token, self::json_encode($data));
  4046. $this->log($result);
  4047. if ($result) {
  4048. $json = json_decode($result, true);
  4049. if (!$json || !empty($json['errcode'])) {
  4050. $this->errCode = $json['errcode'];
  4051. $this->errMsg = $json['errmsg'];
  4052. return false;
  4053. }
  4054. return $json;
  4055. }
  4056. return false;
  4057. }
  4058. /**
  4059. * [getShakeInfoShakeAroundUser 获取设备信息,包括UUID、major、minor,以及距离、openID 等信息。]
  4060. * @param string $ticket 摇周边业务的ticket,可在摇到的URL 中得到,ticket生效时间为30 分钟
  4061. * @return boolean|mixed
  4062. * 正确返回JSON 数据示例:
  4063. * {
  4064. "data": {
  4065. "page_id ": 14211,
  4066. "beacon_info": {
  4067. "distance": 55.00620700469034,
  4068. "major": 10001,
  4069. "minor": 19007,
  4070. "uuid": "FDA50693-A4E2-4FB1-AFCF-C6EB07647825"
  4071. },
  4072. "openid": "oVDmXjp7y8aG2AlBuRpMZTb1-cmA"
  4073. },
  4074. "errcode": 0,
  4075. "errmsg": "success."
  4076. }
  4077. * 字段说明:
  4078. * beacon_info 设备信息,包括UUID、major、minor,以及距离
  4079. * UUID、major、minor UUID、major、minor
  4080. * distance Beacon 信号与手机的距离
  4081. * page_id 摇周边页面唯一ID
  4082. * openid 商户AppID 下用户的唯一标识
  4083. * poi_id 门店ID,有的话则返回,没有的话不会在JSON 格式内
  4084. * @access public
  4085. * @author polo<gao.bo168@gmail.com>
  4086. * @version 2015-3-25 下午3:28:20
  4087. * @copyright Show More
  4088. */
  4089. public function getShakeInfoShakeAroundUser($ticket){
  4090. if (!$this->access_token && !$this->checkAuth()) return false;
  4091. $data = array('ticket' => $ticket);
  4092. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_USER_GETSHAKEINFO . 'access_token=' . $this->access_token, self::json_encode($data));
  4093. $this->log($result);
  4094. if ($result) {
  4095. $json = json_decode($result, true);
  4096. if (!$json || !empty($json['errcode'])) {
  4097. $this->errCode = $json['errcode'];
  4098. $this->errMsg = $json['errmsg'];
  4099. return false;
  4100. }
  4101. return $json;
  4102. }
  4103. return false;
  4104. }
  4105. /**
  4106. * [deviceShakeAroundStatistics 以设备为维度的数据统计接口。
  4107. * 查询单个设备进行摇周边操作的人数、次数,点击摇周边消息的人数、次数;查询的最长时间跨度为30天。]
  4108. * @param int $device_id 设备编号,若填了UUID、major、minor,即可不填设备编号,二者选其一
  4109. * @param int $begin_date 起始日期时间戳,最长时间跨度为30 天
  4110. * @param int $end_date 结束日期时间戳,最长时间跨度为30 天
  4111. * @param string $uuid UUID、major、minor,三个信息需填写完成,若填了设备编辑,即可不填此信息,二者选其一
  4112. * @param int $major
  4113. * @param int $minor
  4114. * @return boolean|mixed
  4115. * 正确返回JSON 数据示例:
  4116. * {
  4117. "data": [
  4118. {
  4119. "click_pv": 0,
  4120. "click_uv": 0,
  4121. "ftime": 1425052800,
  4122. "shake_pv": 0,
  4123. "shake_uv": 0
  4124. },
  4125. {
  4126. "click_pv": 0,
  4127. "click_uv": 0,
  4128. "ftime": 1425139200,
  4129. "shake_pv": 0,
  4130. "shake_uv": 0
  4131. }
  4132. ],
  4133. "errcode": 0,
  4134. "errmsg": "success."
  4135. }
  4136. * 字段说明:
  4137. * ftime 当天0 点对应的时间戳
  4138. * click_pv 点击摇周边消息的次数
  4139. * click_uv 点击摇周边消息的人数
  4140. * shake_pv 摇周边的次数
  4141. * shake_uv 摇周边的人数
  4142. * @access public
  4143. * @author polo<gao.bo168@gmail.com>
  4144. * @version 2015-4-21 00:39:00
  4145. * @copyright Show More
  4146. */
  4147. public function deviceShakeAroundStatistics($device_id,$begin_date,$end_date,$uuid='',$major=0,$minor=0){
  4148. if (!$this->access_token && !$this->checkAuth()) return false;
  4149. if(!$device_id){
  4150. if(!$uuid || !$major || !$minor){
  4151. return false;
  4152. }
  4153. $device_identifier = array(
  4154. 'uuid' => $uuid,
  4155. 'major' => $major,
  4156. 'minor' => $minor
  4157. );
  4158. }else{
  4159. $device_identifier = array(
  4160. 'device_id' => $device_id
  4161. );
  4162. }
  4163. $data = array(
  4164. 'device_identifier' => $device_identifier,
  4165. 'begin_date' => $begin_date,
  4166. 'end_date' => $end_date
  4167. );
  4168. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_STATISTICS_DEVICE . 'access_token=' . $this->access_token, self::json_encode($data));
  4169. $this->log($result);
  4170. if ($result) {
  4171. $json = json_decode($result, true);
  4172. if (!$json || !empty($json['errcode'])) {
  4173. $this->errCode = $json['errcode'];
  4174. $this->errMsg = $json['errmsg'];
  4175. return false;
  4176. }
  4177. return $json;
  4178. }
  4179. return false;
  4180. }
  4181. /**
  4182. * [pageShakeAroundStatistics 以页面为维度的数据统计接口。
  4183. * 查询单个页面通过摇周边摇出来的人数、次数,点击摇周边页面的人数、次数;查询的最长时间跨度为30天。]
  4184. * @param int $page_id 指定页面的ID
  4185. * @param int $begin_date 起始日期时间戳,最长时间跨度为30 天
  4186. * @param int $end_date 结束日期时间戳,最长时间跨度为30 天
  4187. * @return boolean|mixed
  4188. * 正确返回JSON 数据示例:
  4189. * {
  4190. "data": [
  4191. {
  4192. "click_pv": 0,
  4193. "click_uv": 0,
  4194. "ftime": 1425052800,
  4195. "shake_pv": 0,
  4196. "shake_uv": 0
  4197. },
  4198. {
  4199. "click_pv": 0,
  4200. "click_uv": 0,
  4201. "ftime": 1425139200,
  4202. "shake_pv": 0,
  4203. "shake_uv": 0
  4204. }
  4205. ],
  4206. "errcode": 0,
  4207. "errmsg": "success."
  4208. }
  4209. * 字段说明:
  4210. * ftime 当天0 点对应的时间戳
  4211. * click_pv 点击摇周边消息的次数
  4212. * click_uv 点击摇周边消息的人数
  4213. * shake_pv 摇周边的次数
  4214. * shake_uv 摇周边的人数
  4215. * @author binsee<binsee@163.com>
  4216. * @version 2015-4-21 00:43:00
  4217. */
  4218. public function pageShakeAroundStatistics($page_id,$begin_date,$end_date){
  4219. if (!$this->access_token && !$this->checkAuth()) return false;
  4220. $data = array(
  4221. 'page_id' => $page_id,
  4222. 'begin_date' => $begin_date,
  4223. 'end_date' => $end_date
  4224. );
  4225. $result = $this->http_post(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_STATISTICS_DEVICE . 'access_token=' . $this->access_token, self::json_encode($data));
  4226. $this->log($result);
  4227. if ($result) {
  4228. $json = json_decode($result, true);
  4229. if (!$json || !empty($json['errcode'])) {
  4230. $this->errCode = $json['errcode'];
  4231. $this->errMsg = $json['errmsg'];
  4232. return false;
  4233. }
  4234. return $json;
  4235. }
  4236. return false;
  4237. }
  4238. /**
  4239. * 根据订单ID获取订单详情
  4240. * @param string $order_id 订单ID
  4241. * @return order array|bool
  4242. */
  4243. public function getOrderByID($order_id){
  4244. if (!$this->access_token && !$this->checkAuth()) return false;
  4245. if (!$order_id) return false;
  4246. $data = array(
  4247. 'order_id'=>$order_id
  4248. );
  4249. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::MERCHANT_ORDER_GETBYID.'access_token='.$this->access_token, self::json_encode($data));
  4250. if ($result)
  4251. {
  4252. $json = json_decode($result,true);
  4253. if (isset($json['errcode']) && $json['errcode']) {
  4254. $this->errCode = $json['errcode'];
  4255. $this->errMsg = $json['errmsg'];
  4256. return false;
  4257. }
  4258. return $json['order'];
  4259. }
  4260. return false;
  4261. }
  4262. /**
  4263. * 根据订单状态/创建时间获取订单详情
  4264. * @param int $status 订单状态(不带该字段-全部状态, 2-待发货, 3-已发货, 5-已完成, 8-维权中, )
  4265. * @param int $begintime 订单创建时间起始时间(不带该字段则不按照时间做筛选)
  4266. * @param int $endtime 订单创建时间终止时间(不带该字段则不按照时间做筛选)
  4267. * @return order list array|bool
  4268. */
  4269. public function getOrderByFilter($status = null, $begintime = null, $endtime = null){
  4270. if (!$this->access_token && !$this->checkAuth()) return false;
  4271. $data = array();
  4272. $valid_status = array(2, 3, 5, 8);
  4273. if (is_numeric($status) && in_array($status, $valid_status)) {
  4274. $data['status'] = $status;
  4275. }
  4276. if (is_numeric($begintime) && is_numeric($endtime)) {
  4277. $data['begintime'] = $begintime;
  4278. $data['endtime'] = $endtime;
  4279. }
  4280. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::MERCHANT_ORDER_GETBYFILTER.'access_token='.$this->access_token, self::json_encode($data));
  4281. if ($result)
  4282. {
  4283. $json = json_decode($result,true);
  4284. if (isset($json['errcode']) && $json['errcode']) {
  4285. $this->errCode = $json['errcode'];
  4286. $this->errMsg = $json['errmsg'];
  4287. return false;
  4288. }
  4289. return $json['order_list'];
  4290. }
  4291. return false;
  4292. }
  4293. /**
  4294. * 设置订单发货信息
  4295. * @param string $order_id 订单 ID
  4296. * @param int $need_delivery 商品是否需要物流(0-不需要,1-需要)
  4297. * @param string $delivery_company 物流公司 ID
  4298. * @param string $delivery_track_no 运单 ID
  4299. * @param int $is_others 是否为 6.4.5 表之外的其它物流公司(0-否,1-是)
  4300. * @return bool
  4301. */
  4302. public function setOrderDelivery($order_id, $need_delivery = 0, $delivery_company = null, $delivery_track_no = null, $is_others = 0){
  4303. if (!$this->access_token && !$this->checkAuth()) return false;
  4304. if (!$order_id) return false;
  4305. $data = array();
  4306. $data['order_id'] = $order_id;
  4307. if ($need_delivery) {
  4308. $data['delivery_company'] = $delivery_company;
  4309. $data['delivery_track_no'] = $delivery_track_no;
  4310. $data['is_others'] = $is_others;
  4311. }
  4312. else {
  4313. $data['need_delivery'] = $need_delivery;
  4314. }
  4315. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::MERCHANT_ORDER_SETDELIVERY.'access_token='.$this->access_token, self::json_encode($data));
  4316. if ($result)
  4317. {
  4318. $json = json_decode($result,true);
  4319. if (isset($json['errcode']) && $json['errcode']) {
  4320. $this->errCode = $json['errcode'];
  4321. $this->errMsg = $json['errmsg'];
  4322. return false;
  4323. }
  4324. return true;
  4325. }
  4326. return false;
  4327. }
  4328. /**
  4329. * 关闭订单
  4330. * @param string $order_id 订单 ID
  4331. * @return bool
  4332. */
  4333. public function closeOrder($order_id){
  4334. if (!$this->access_token && !$this->checkAuth()) return false;
  4335. if (!$order_id) return false;
  4336. $data = array(
  4337. 'order_id'=>$order_id
  4338. );
  4339. $result = $this->http_post(self::API_BASE_URL_PREFIX.self::MERCHANT_ORDER_CLOSE.'access_token='.$this->access_token, self::json_encode($data));
  4340. if ($result)
  4341. {
  4342. $json = json_decode($result,true);
  4343. if (isset($json['errcode']) && $json['errcode']) {
  4344. $this->errCode = $json['errcode'];
  4345. $this->errMsg = $json['errmsg'];
  4346. return false;
  4347. }
  4348. return true;
  4349. }
  4350. return false;
  4351. }
  4352. private function parseSkuInfo($skuInfo) {
  4353. $skuInfo = str_replace("\$", "", $skuInfo);
  4354. $matches = explode(";", $skuInfo);
  4355. $result = array();
  4356. foreach ($matches as $matche) {
  4357. $arrs = explode(":", $matche);
  4358. $result[$arrs[0]] = $arrs[1];
  4359. }
  4360. return $result;
  4361. }
  4362. /**
  4363. * 获取订单SkuInfo - 订单付款通知
  4364. * 当Event为 merchant_order(订单付款通知)
  4365. * @return array|boolean
  4366. */
  4367. public function getRevOrderSkuInfo(){
  4368. if (isset($this->_receive['SkuInfo'])) //订单 SkuInfo
  4369. return $this->parseSkuInfo($this->_receive['SkuInfo']);
  4370. else
  4371. return false;
  4372. }
  4373. }
  4374. /**
  4375. * PKCS7Encoder class
  4376. *
  4377. * 提供基于PKCS7算法的加解密接口.
  4378. */
  4379. class PKCS7Encoder
  4380. {
  4381. public static $block_size = 32;
  4382. /**
  4383. * 对需要加密的明文进行填充补位
  4384. * @param $text 需要进行填充补位操作的明文
  4385. * @return 补齐明文字符串
  4386. */
  4387. function encode($text)
  4388. {
  4389. $block_size = PKCS7Encoder::$block_size;
  4390. $text_length = strlen($text);
  4391. //计算需要填充的位数
  4392. $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
  4393. if ($amount_to_pad == 0) {
  4394. $amount_to_pad = PKCS7Encoder::block_size;
  4395. }
  4396. //获得补位所用的字符
  4397. $pad_chr = chr($amount_to_pad);
  4398. $tmp = "";
  4399. for ($index = 0; $index < $amount_to_pad; $index++) {
  4400. $tmp .= $pad_chr;
  4401. }
  4402. return $text . $tmp;
  4403. }
  4404. /**
  4405. * 对解密后的明文进行补位删除
  4406. * @param decrypted 解密后的明文
  4407. * @return 删除填充补位后的明文
  4408. */
  4409. function decode($text)
  4410. {
  4411. $pad = ord(substr($text, -1));
  4412. if ($pad < 1 || $pad > PKCS7Encoder::$block_size) {
  4413. $pad = 0;
  4414. }
  4415. return substr($text, 0, (strlen($text) - $pad));
  4416. }
  4417. }
  4418. /**
  4419. * Prpcrypt class
  4420. *
  4421. * 提供接收和推送给公众平台消息的加解密接口.
  4422. */
  4423. class Prpcrypt
  4424. {
  4425. public $key;
  4426. function __construct($k) {
  4427. $this->key = base64_decode($k . "=");
  4428. }
  4429. /**
  4430. * 兼容老版本php构造函数,不能在 __construct() 方法前边,否则报错
  4431. */
  4432. function Prpcrypt($k)
  4433. {
  4434. $this->key = base64_decode($k . "=");
  4435. }
  4436. /**
  4437. * 对明文进行加密
  4438. * @param string $text 需要加密的明文
  4439. * @return string 加密后的密文
  4440. */
  4441. public function encrypt($text, $appid)
  4442. {
  4443. try {
  4444. //获得16位随机字符串,填充到明文之前
  4445. $random = $this->getRandomStr();//"aaaabbbbccccdddd";
  4446. $text = $random . pack("N", strlen($text)) . $text . $appid;
  4447. // 网络字节序
  4448. $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  4449. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  4450. $iv = substr($this->key, 0, 16);
  4451. //使用自定义的填充方式对明文进行补位填充
  4452. $pkc_encoder = new PKCS7Encoder;
  4453. $text = $pkc_encoder->encode($text);
  4454. mcrypt_generic_init($module, $this->key, $iv);
  4455. //加密
  4456. $encrypted = mcrypt_generic($module, $text);
  4457. mcrypt_generic_deinit($module);
  4458. mcrypt_module_close($module);
  4459. // print(base64_encode($encrypted));
  4460. //使用BASE64对加密后的字符串进行编码
  4461. return array(ErrorCode::$OK, base64_encode($encrypted));
  4462. } catch (Exception $e) {
  4463. //print $e;
  4464. return array(ErrorCode::$EncryptAESError, null);
  4465. }
  4466. }
  4467. /**
  4468. * 对密文进行解密
  4469. * @param string $encrypted 需要解密的密文
  4470. * @return string 解密得到的明文
  4471. */
  4472. public function decrypt($encrypted, $appid)
  4473. {
  4474. try {
  4475. //使用BASE64对需要解密的字符串进行解码
  4476. $ciphertext_dec = base64_decode($encrypted);
  4477. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  4478. $iv = substr($this->key, 0, 16);
  4479. mcrypt_generic_init($module, $this->key, $iv);
  4480. //解密
  4481. $decrypted = mdecrypt_generic($module, $ciphertext_dec);
  4482. mcrypt_generic_deinit($module);
  4483. mcrypt_module_close($module);
  4484. } catch (Exception $e) {
  4485. return array(ErrorCode::$DecryptAESError, null);
  4486. }
  4487. try {
  4488. //去除补位字符
  4489. $pkc_encoder = new PKCS7Encoder;
  4490. $result = $pkc_encoder->decode($decrypted);
  4491. //去除16位随机字符串,网络字节序和AppId
  4492. if (strlen($result) < 16)
  4493. return "";
  4494. $content = substr($result, 16, strlen($result));
  4495. $len_list = unpack("N", substr($content, 0, 4));
  4496. $xml_len = $len_list[1];
  4497. $xml_content = substr($content, 4, $xml_len);
  4498. $from_appid = substr($content, $xml_len + 4);
  4499. if (!$appid)
  4500. $appid = $from_appid;
  4501. //如果传入的appid是空的,则认为是订阅号,使用数据中提取出来的appid
  4502. } catch (Exception $e) {
  4503. //print $e;
  4504. return array(ErrorCode::$IllegalBuffer, null);
  4505. }
  4506. if ($from_appid != $appid)
  4507. return array(ErrorCode::$ValidateAppidError, null);
  4508. //不注释上边两行,避免传入appid是错误的情况
  4509. return array(0, $xml_content, $from_appid); //增加appid,为了解决后面加密回复消息的时候没有appid的订阅号会无法回复
  4510. }
  4511. /**
  4512. * 随机生成16位字符串
  4513. * @return string 生成的字符串
  4514. */
  4515. function getRandomStr()
  4516. {
  4517. $str = "";
  4518. $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  4519. $max = strlen($str_pol) - 1;
  4520. for ($i = 0; $i < 16; $i++) {
  4521. $str .= $str_pol[mt_rand(0, $max)];
  4522. }
  4523. return $str;
  4524. }
  4525. }
  4526. /**
  4527. * error code
  4528. * 仅用作类内部使用,不用于官方API接口的errCode码
  4529. */
  4530. class ErrorCode
  4531. {
  4532. public static $OK = 0;
  4533. public static $ValidateSignatureError = 40001;
  4534. public static $ParseXmlError = 40002;
  4535. public static $ComputeSignatureError = 40003;
  4536. public static $IllegalAesKey = 40004;
  4537. public static $ValidateAppidError = 40005;
  4538. public static $EncryptAESError = 40006;
  4539. public static $DecryptAESError = 40007;
  4540. public static $IllegalBuffer = 40008;
  4541. public static $EncodeBase64Error = 40009;
  4542. public static $DecodeBase64Error = 40010;
  4543. public static $GenReturnXmlError = 40011;
  4544. public static $errCode=array(
  4545. '0' => '处理成功',
  4546. '40001' => '校验签名失败',
  4547. '40002' => '解析xml失败',
  4548. '40003' => '计算签名失败',
  4549. '40004' => '不合法的AESKey',
  4550. '40005' => '校验AppID失败',
  4551. '40006' => 'AES加密失败',
  4552. '40007' => 'AES解密失败',
  4553. '40008' => '公众平台发送的xml不合法',
  4554. '40009' => 'Base64编码失败',
  4555. '40010' => 'Base64解码失败',
  4556. '40011' => '公众帐号生成回包xml失败'
  4557. );
  4558. public static function getErrText($err) {
  4559. if (isset(self::$errCode[$err])) {
  4560. return self::$errCode[$err];
  4561. }else {
  4562. return false;
  4563. };
  4564. }
  4565. }