wechat.class.php 165 KB

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