Wechat.php 164 KB

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