wechat.class.php 144 KB

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