wechat.class.php 163 KB

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