wechat.class.php 151 KB

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