wechat.class.php 151 KB

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