wechat.class.php 165 KB

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