123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588 |
- <?php
- use think\facade\Cache;
- class Wechat
- {
- const MSGTYPE_TEXT = 'text';
- const MSGTYPE_IMAGE = 'image';
- const MSGTYPE_LOCATION = 'location';
- const MSGTYPE_LINK = 'link';
- const MSGTYPE_EVENT = 'event';
- const MSGTYPE_MUSIC = 'music';
- const MSGTYPE_NEWS = 'news';
- const MSGTYPE_VOICE = 'voice';
- const MSGTYPE_VIDEO = 'video';
- const EVENT_SUBSCRIBE = 'subscribe';
- const EVENT_UNSUBSCRIBE = 'unsubscribe';
- const EVENT_SCAN = 'SCAN';
- const EVENT_LOCATION = 'LOCATION';
- const EVENT_MENU_VIEW = 'VIEW';
- const EVENT_MENU_CLICK = 'CLICK';
- const EVENT_MENU_SCAN_PUSH = 'scancode_push';
- const EVENT_MENU_SCAN_WAITMSG = 'scancode_waitmsg';
- const EVENT_MENU_PIC_SYS = 'pic_sysphoto';
- const EVENT_MENU_PIC_PHOTO = 'pic_photo_or_album';
- const EVENT_MENU_PIC_WEIXIN = 'pic_weixin';
- const EVENT_MENU_LOCATION = 'location_select';
- const EVENT_SEND_MASS = 'MASSSENDJOBFINISH';
- const EVENT_SEND_TEMPLATE = 'TEMPLATESENDJOBFINISH';
- const EVENT_KF_SEESION_CREATE = 'kfcreatesession';
- const EVENT_KF_SEESION_CLOSE = 'kfclosesession';
- const EVENT_KF_SEESION_SWITCH = 'kfswitchsession';
- const EVENT_CARD_PASS = 'card_pass_check';
- const EVENT_CARD_NOTPASS = 'card_not_pass_check';
- const EVENT_CARD_USER_GET = 'user_get_card';
- const EVENT_CARD_USER_DEL = 'user_del_card';
- const API_URL_PREFIX = 'https://api.weixin.qq.com/cgi-bin';
- const AUTH_URL = '/token?grant_type=client_credential&';
- const MENU_CREATE_URL = '/menu/create?';
- const MENU_GET_URL = '/menu/get?';
- const MENU_DELETE_URL = '/menu/delete?';
- const GET_TICKET_URL = '/ticket/getticket?';
- const CALLBACKSERVER_GET_URL = '/getcallbackip?';
- const QRCODE_CREATE_URL='/qrcode/create?';
- const QR_SCENE = 0;
- const QR_LIMIT_SCENE = 1;
- const QRCODE_IMG_URL='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=';
- const SHORT_URL='/shorturl?';
- const USER_GET_URL='/user/get?';
- const USER_INFO_URL='/user/info?';
- const USER_UPDATEREMARK_URL='/user/info/updateremark?';
- const GROUP_GET_URL='/groups/get?';
- const USER_GROUP_URL='/groups/getid?';
- const GROUP_CREATE_URL='/groups/create?';
- const GROUP_UPDATE_URL='/groups/update?';
- const GROUP_MEMBER_UPDATE_URL='/groups/members/update?';
- const GROUP_MEMBER_BATCHUPDATE_URL='/groups/members/batchupdate?';
- const CUSTOM_SEND_URL='/message/custom/send?';
- const MEDIA_UPLOADNEWS_URL = '/media/uploadnews?';
- const MASS_SEND_URL = '/message/mass/send?';
- const TEMPLATE_SET_INDUSTRY_URL = '/message/template/api_set_industry?';
- const TEMPLATE_ADD_TPL_URL = '/message/template/api_add_template?';
- const GET_ALL_PRIVATE_TEMPLATE = '/template/get_all_private_template?';
- const DEL_PRIVATE_TEMPLATE = '/template/get_all_private_template?';
- const TEMPLATE_SEND_URL = '/message/template/send?';
- const MASS_SEND_GROUP_URL = '/message/mass/sendall?';
- const MASS_DELETE_URL = '/message/mass/delete?';
- const MASS_PREVIEW_URL = '/message/mass/preview?';
- const MASS_QUERY_URL = '/message/mass/get?';
- const UPLOAD_MEDIA_URL = 'http://file.api.weixin.qq.com/cgi-bin';
- const MEDIA_UPLOAD_URL = '/media/upload?';
- const MEDIA_GET_URL = '/media/get?';
- const MEDIA_VIDEO_UPLOAD = '/media/uploadvideo?';
- const OAUTH_PREFIX = 'https://open.weixin.qq.com/connect/oauth2';
- const OAUTH_AUTHORIZE_URL = '/authorize?';
-
- const CUSTOM_SERVICE_GET_RECORD = '/customservice/getrecord?';
- const CUSTOM_SERVICE_GET_KFLIST = '/customservice/getkflist?';
- const CUSTOM_SERVICE_GET_ONLINEKFLIST = '/customservice/getonlinekflist?';
- const API_BASE_URL_PREFIX = 'https://api.weixin.qq.com';
- const OAUTH_TOKEN_URL = '/sns/oauth2/access_token?';
- const OAUTH_REFRESH_URL = '/sns/oauth2/refresh_token?';
- const OAUTH_USERINFO_URL = '/sns/userinfo?';
- const OAUTH_AUTH_URL = '/sns/auth?';
-
- const CUSTOM_SESSION_CREATE = '/customservice/kfsession/create?';
- const CUSTOM_SESSION_CLOSE = '/customservice/kfsession/close?';
- const CUSTOM_SESSION_SWITCH = '/customservice/kfsession/switch?';
- const CUSTOM_SESSION_GET = '/customservice/kfsession/getsession?';
- const CUSTOM_SESSION_GET_LIST = '/customservice/kfsession/getsessionlist?';
- const CUSTOM_SESSION_GET_WAIT = '/customservice/kfsession/getwaitcase?';
- const CS_KF_ACCOUNT_ADD_URL = '/customservice/kfaccount/add?';
- const CS_KF_ACCOUNT_UPDATE_URL = '/customservice/kfaccount/update?';
- const CS_KF_ACCOUNT_DEL_URL = '/customservice/kfaccount/del?';
- const CS_KF_ACCOUNT_UPLOAD_HEADIMG_URL = '/customservice/kfaccount/uploadheadimg?';
-
- const CARD_CREATE = '/card/create?';
- const CARD_DELETE = '/card/delete?';
- const CARD_UPDATE = '/card/update?';
- const CARD_GET = '/card/get?';
- const CARD_BATCHGET = '/card/batchget?';
- const CARD_MODIFY_STOCK = '/card/modifystock?';
- const CARD_LOCATION_BATCHADD = '/card/location/batchadd?';
- const CARD_LOCATION_BATCHGET = '/card/location/batchget?';
- const CARD_GETCOLORS = '/card/getcolors?';
- const CARD_QRCODE_CREATE = '/card/qrcode/create?';
- const CARD_CODE_CONSUME = '/card/code/consume?';
- const CARD_CODE_DECRYPT = '/card/code/decrypt?';
- const CARD_CODE_GET = '/card/code/get?';
- const CARD_CODE_UPDATE = '/card/code/update?';
- const CARD_CODE_UNAVAILABLE = '/card/code/unavailable?';
- const CARD_TESTWHILELIST_SET = '/card/testwhitelist/set?';
- const CARD_MEMBERCARD_ACTIVATE = '/card/membercard/activate?';
- const CARD_MEMBERCARD_UPDATEUSER = '/card/membercard/updateuser?';
- const CARD_MOVIETICKET_UPDATEUSER = '/card/movieticket/updateuser?';
- const CARD_BOARDINGPASS_CHECKIN = '/card/boardingpass/checkin?';
- const CARD_LUCKYMONEY_UPDATE = '/card/luckymoney/updateuserbalance?';
- const SEMANTIC_API_URL = '/semantic/semproxy/search?';
-
- const SUB_BIZSEND = '/message/subscribe/bizsend?';
- const GET_TEMPLATE = '/wxaapi/newtmpl/gettemplate?';
- const GET_PUB_TEMPLATE = '/wxaapi/newtmpl/getpubtemplatetitles?';
- const ADD_TEMPLATE = '/wxaapi/newtmpl/deltemplate?';
- const DELETE_TEMPLATE = '/wxaapi/newtmpl/addtemplate?';
- const GET_CATEGORY = '/wxaapi/newtmpl/getcategory?';
- const GET_PUB_TEMPLATE_KEY_WORD = '/wxaapi/newtmpl/getpubtemplatekeywords?';
-
- const CLEAR_QUOTA ='/clear_quota?';
- const CLEAR_QUOTA_v2 ='/clear_quota/v2?';
- const QUOTA ='/openapi/quota/get?';
-
-
- static $DATACUBE_URL_ARR = array(
- 'user' => array(
- 'summary' => '/datacube/getusersummary?',
- 'cumulate' => '/datacube/getusercumulate?',
- ),
- 'article' => array(
- 'summary' => '/datacube/getarticlesummary?',
- 'total' => '/datacube/getarticletotal?',
- 'read' => '/datacube/getuserread?',
- 'readhour' => '/datacube/getuserreadhour?',
- 'share' => '/datacube/getusershare?',
- 'sharehour' => '/datacube/getusersharehour?',
- ),
- 'upstreammsg' => array(
- 'summary' => '/datacube/getupstreammsg?',
- 'hour' => '/datacube/getupstreammsghour?',
- 'week' => '/datacube/getupstreammsgweek?',
- 'month' => '/datacube/getupstreammsgmonth?',
- 'dist' => '/datacube/getupstreammsgdist?',
- 'distweek' => '/datacube/getupstreammsgdistweek?',
- 'distmonth' => '/datacube/getupstreammsgdistmonth?',
- ),
- 'interface' => array(
- 'summary' => '/datacube/getinterfacesummary?',
- 'summaryhour' => '/datacube/getinterfacesummaryhour?',
- )
- );
- private $token;
- private $Cache;
- private $encodingAesKey;
- private $encrypt_type;
- private $appid;
- private $appsecret;
- private $access_token;
- private $jsapi_ticket;
- private $user_token;
- private $partnerid;
- private $partnerkey;
- private $paysignkey;
- private $postxml;
- private $_msg;
- private $_funcflag = false;
- private $_receive;
- private $_text_filter = true;
- public $debug = false;
- public $errCode = 40001;
- public $errMsg = 'no access';
- public $logcallback;
-
- public function __construct($options)
- {
- $this->token = isset($options['token'])?$options['token']:'';
- $this->encodingAesKey = isset($options['encodingaeskey'])?$options['encodingaeskey']:'';
- $this->appid = isset($options['appid'])?$options['appid']:'';
- $this->appsecret = isset($options['appsecret'])?$options['appsecret']:'';
- $this->debug = isset($options['debug'])?$options['debug']:false;
- $this->logcallback = isset($options['logcallback'])?$options['logcallback']:false;
- }
-
- private function checkSignature($str='')
- {
- $signature = isset($_GET['signature'])?$_GET['signature']:'';
- $signature = isset($_GET['msg_signature'])?$_GET['msg_signature']:$signature;
- $timestamp = isset($_GET['timestamp'])?$_GET['timestamp']:'';
- $nonce = isset($_GET['nonce'])?$_GET['nonce']:'';
- $token = $this->token;
- $tmpArr = array($token, $timestamp, $nonce,$str);
- sort($tmpArr, SORT_STRING);
- $tmpStr = implode( $tmpArr );
- $tmpStr = sha1( $tmpStr );
- if( $tmpStr == $signature ){
- return true;
- }else{
- return false;
- }
- }
-
- public function valid($return=false)
- {
- $encryptStr='';
- if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- $postStr = file_get_contents('php://input');
- $array = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
- $this->encrypt_type = isset($_GET['encrypt_type']) ? $_GET['encrypt_type']: '';
- if ($this->encrypt_type == 'aes') {
- $this->log($postStr);
- $encryptStr = $array['Encrypt'];
- $pc = new Prpcrypt($this->encodingAesKey);
- $array = $pc->decrypt($encryptStr,$this->appid);
- if (!isset($array[0]) || ($array[0] != 0)) {
- if (!$return) {
- die('decrypt error!');
- } else {
- return false;
- }
- }
- $this->postxml = $array[1];
- if (!$this->appid)
- $this->appid = $array[2];
- } else {
- $this->postxml = $postStr;
- }
- } elseif (isset($_GET['echostr'])) {
- $echoStr = $_GET['echostr'];
- if ($return) {
- if ($this->checkSignature())
- return $echoStr;
- else
- return false;
- } else {
- if ($this->checkSignature())
- die($echoStr);
- else
- die('no access');
- }
- }
- if (!$this->checkSignature($encryptStr)) {
- if ($return)
- return false;
- else
- die('no access');
- }
- return true;
- }
-
- public function Message($msg = '',$append = false){
- if (is_null($msg)) {
- $this->_msg =array();
- }elseif (is_array($msg)) {
- if ($append)
- $this->_msg = array_merge($this->_msg,$msg);
- else
- $this->_msg = $msg;
- return $this->_msg;
- } else {
- return $this->_msg;
- }
- }
-
- public function setFuncFlag($flag) {
- $this->_funcflag = $flag;
- return $this;
- }
-
- protected function log($log){
- if ($this->debug && function_exists($this->logcallback)) {
- if (is_array($log)) $log = print_r($log,true);
- return call_user_func($this->logcallback,$log);
- }
- }
-
- public function getRev()
- {
- if ($this->_receive) return $this;
- $postStr = !empty($this->postxml)?$this->postxml:file_get_contents('php://input');
-
- $this->log($postStr);
- if (!empty($postStr)) {
- $this->_receive = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
- }
- return $this;
- }
-
- public function getRevData()
- {
- return $this->_receive;
- }
-
- public function getRevFrom() {
- if (isset($this->_receive['FromUserName']))
- return $this->_receive['FromUserName'];
- else
- return false;
- }
-
- public function getRevTo() {
- if (isset($this->_receive['ToUserName']))
- return $this->_receive['ToUserName'];
- else
- return false;
- }
-
- public function getRevType() {
- if (isset($this->_receive['MsgType']))
- return $this->_receive['MsgType'];
- else
- return false;
- }
-
- public function getRevID() {
- if (isset($this->_receive['MsgId']))
- return $this->_receive['MsgId'];
- else
- return false;
- }
-
- public function getRevCtime() {
- if (isset($this->_receive['CreateTime']))
- return $this->_receive['CreateTime'];
- else
- return false;
- }
-
- public function getRevContent(){
- if (isset($this->_receive['Content']))
- return $this->_receive['Content'];
- else if (isset($this->_receive['Recognition']))
- return $this->_receive['Recognition'];
- else
- return false;
- }
-
- public function getRevPic(){
- if (isset($this->_receive['PicUrl']))
- return array(
- 'mediaid'=>$this->_receive['MediaId'],
- 'picurl'=>(string)$this->_receive['PicUrl'],
- );
- else
- return false;
- }
-
- public function getRevLink(){
- if (isset($this->_receive['Url'])){
- return array(
- 'url'=>$this->_receive['Url'],
- 'title'=>$this->_receive['Title'],
- 'description'=>$this->_receive['Description']
- );
- } else
- return false;
- }
-
- public function getRevGeo(){
- if (isset($this->_receive['Location_X'])){
- return array(
- 'x'=>$this->_receive['Location_X'],
- 'y'=>$this->_receive['Location_Y'],
- 'scale'=>$this->_receive['Scale'],
- 'label'=>$this->_receive['Label']
- );
- } else
- return false;
- }
-
- public function getRevEventGeo(){
- if (isset($this->_receive['Latitude'])){
- return array(
- 'x'=>$this->_receive['Latitude'],
- 'y'=>$this->_receive['Longitude'],
- 'precision'=>$this->_receive['Precision'],
- );
- } else
- return false;
- }
-
- public function getRevEvent(){
- if (isset($this->_receive['Event'])){
- $array['event'] = $this->_receive['Event'];
- }
- if (isset($this->_receive['EventKey'])){
- $array['key'] = $this->_receive['EventKey'];
- }
- if (isset($array) && count($array) > 0) {
- return $array;
- } else {
- return false;
- }
- }
-
- public function getRevScanInfo(){
- if (isset($this->_receive['ScanCodeInfo'])){
- if (!is_array($this->_receive['ScanCodeInfo'])) {
- $array=(array)$this->_receive['ScanCodeInfo'];
- $this->_receive['ScanCodeInfo']=$array;
- }else {
- $array=$this->_receive['ScanCodeInfo'];
- }
- }
- if (isset($array) && count($array) > 0) {
- return $array;
- } else {
- return false;
- }
- }
-
- public function getRevSendPicsInfo(){
- if (isset($this->_receive['SendPicsInfo'])){
- if (!is_array($this->_receive['SendPicsInfo'])) {
- $array=(array)$this->_receive['SendPicsInfo'];
- if (isset($array['PicList'])){
- $array['PicList']=(array)$array['PicList'];
- $item=$array['PicList']['item'];
- $array['PicList']['item']=array();
- foreach ( $item as $key => $value ){
- $array['PicList']['item'][$key]=(array)$value;
- }
- }
- $this->_receive['SendPicsInfo']=$array;
- } else {
- $array=$this->_receive['SendPicsInfo'];
- }
- }
- if (isset($array) && count($array) > 0) {
- return $array;
- } else {
- return false;
- }
- }
-
- public function getRevSendGeoInfo(){
- if (isset($this->_receive['SendLocationInfo'])){
- if (!is_array($this->_receive['SendLocationInfo'])) {
- $array=(array)$this->_receive['SendLocationInfo'];
- if (empty($array['Poiname'])) {
- $array['Poiname']='';
- }
- if (empty($array['Label'])) {
- $array['Label']='';
- }
- $this->_receive['SendLocationInfo']=$array;
- } else {
- $array=$this->_receive['SendLocationInfo'];
- }
- }
- if (isset($array) && count($array) > 0) {
- return $array;
- } else {
- return false;
- }
- }
-
- public function getRevVoice(){
- if (isset($this->_receive['MediaId'])){
- return array(
- 'mediaid'=>$this->_receive['MediaId'],
- 'format'=>$this->_receive['Format'],
- );
- } else
- return false;
- }
-
- public function getRevVideo(){
- if (isset($this->_receive['MediaId'])){
- return array(
- 'mediaid'=>$this->_receive['MediaId'],
- 'thumbmediaid'=>$this->_receive['ThumbMediaId']
- );
- } else
- return false;
- }
-
- public function getRevTicket(){
- if (isset($this->_receive['Ticket'])){
- return $this->_receive['Ticket'];
- } else
- return false;
- }
-
- public function getRevSceneId (){
- if (isset($this->_receive['EventKey'])){
- return str_replace('qrscene_','',$this->_receive['EventKey']);
- } else{
- return false;
- }
- }
-
- public function getRevTplMsgID(){
- if (isset($this->_receive['MsgID'])){
- return $this->_receive['MsgID'];
- } else
- return false;
- }
-
- public function getRevStatus(){
- if (isset($this->_receive['Status'])){
- return $this->_receive['Status'];
- } else
- return false;
- }
-
- public function getRevResult(){
- if (isset($this->_receive['Status']))
- $array['Status'] = $this->_receive['Status'];
- if (isset($this->_receive['MsgID']))
- $array['MsgID'] = $this->_receive['MsgID'];
-
- if (isset($this->_receive['TotalCount']))
- $array['TotalCount'] = $this->_receive['TotalCount'];
- if (isset($this->_receive['FilterCount']))
- $array['FilterCount'] = $this->_receive['FilterCount'];
- if (isset($this->_receive['SentCount']))
- $array['SentCount'] = $this->_receive['SentCount'];
- if (isset($this->_receive['ErrorCount']))
- $array['ErrorCount'] = $this->_receive['ErrorCount'];
- if (isset($array) && count($array) > 0) {
- return $array;
- } else {
- return false;
- }
- }
-
- public function getRevKFCreate(){
- if (isset($this->_receive['KfAccount'])){
- return $this->_receive['KfAccount'];
- } else
- return false;
- }
-
- public function getRevKFClose(){
- if (isset($this->_receive['KfAccount'])){
- return $this->_receive['KfAccount'];
- } else
- return false;
- }
-
- public function getRevKFSwitch(){
- if (isset($this->_receive['FromKfAccount']))
- $array['FromKfAccount'] = $this->_receive['FromKfAccount'];
- if (isset($this->_receive['ToKfAccount']))
- $array['ToKfAccount'] = $this->_receive['ToKfAccount'];
- if (isset($array) && count($array) > 0) {
- return $array;
- } else {
- return false;
- }
- }
-
- public function getRevCardPass(){
- if (isset($this->_receive['CardId']))
- return $this->_receive['CardId'];
- else
- return false;
- }
-
- public function getRevCardGet(){
- if (isset($this->_receive['CardId']))
- $array['CardId'] = $this->_receive['CardId'];
- if (isset($this->_receive['IsGiveByFriend']))
- $array['IsGiveByFriend'] = $this->_receive['IsGiveByFriend'];
- if (isset($this->_receive['UserCardCode']) && !empty($this->_receive['UserCardCode']))
- $array['UserCardCode'] = $this->_receive['UserCardCode'];
- if (isset($array) && count($array) > 0) {
- return $array;
- } else {
- return false;
- }
- }
-
- public function getRevCardDel(){
- if (isset($this->_receive['CardId']))
- $array['CardId'] = $this->_receive['CardId'];
- if (isset($this->_receive['UserCardCode']) && !empty($this->_receive['UserCardCode']))
- $array['UserCardCode'] = $this->_receive['UserCardCode'];
- if (isset($array) && count($array) > 0) {
- return $array;
- } else {
- return false;
- }
- }
- public static function xmlSafeStr($str)
- {
- return '<![CDATA['.preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/",'',$str).']]>';
- }
-
- public static function data_to_xml($data) {
- $xml = '';
- foreach ($data as $key => $val) {
- is_numeric($key) && $key = "item id=\"$key\"";
- $xml .= "<$key>";
- $xml .= ( is_array($val) || is_object($val)) ? self::data_to_xml($val) : self::xmlSafeStr($val);
- list($key, ) = explode(' ', $key);
- $xml .= "</$key>";
- }
- return $xml;
- }
-
- public function xml_encode($data, $root='xml', $item='item', $attr='', $id='id', $encoding='utf-8') {
- if(is_array($attr)){
- $_attr = array();
- foreach ($attr as $key => $value) {
- $_attr[] = "{$key}=\"{$value}\"";
- }
- $attr = implode(' ', $_attr);
- }
- $attr = trim($attr);
- $attr = empty($attr) ? '' : " {$attr}";
- $xml = "<{$root}{$attr}>";
- $xml .= self::data_to_xml($data, $item, $id);
- $xml .= "</{$root}>";
- return $xml;
- }
-
- private function _auto_text_filter($text) {
- if (!$this->_text_filter) return $text;
- return str_replace("\r\n", "\n", $text);
- }
-
- public function text($text='')
- {
- $FuncFlag = $this->_funcflag ? 1 : 0;
- $msg = array(
- 'ToUserName' => $this->getRevFrom(),
- 'FromUserName'=>$this->getRevTo(),
- 'MsgType'=>self::MSGTYPE_TEXT,
- 'Content'=>$this->_auto_text_filter($text),
- 'CreateTime'=>time(),
- 'FuncFlag'=>$FuncFlag
- );
- $this->Message($msg);
- return $this;
- }
-
- public function image($mediaid='')
- {
- $FuncFlag = $this->_funcflag ? 1 : 0;
- $msg = array(
- 'ToUserName' => $this->getRevFrom(),
- 'FromUserName'=>$this->getRevTo(),
- 'MsgType'=>self::MSGTYPE_IMAGE,
- 'Image'=>array('MediaId'=>$mediaid),
- 'CreateTime'=>time(),
- 'FuncFlag'=>$FuncFlag
- );
- $this->Message($msg);
- return $this;
- }
-
- public function voice($mediaid='')
- {
- $FuncFlag = $this->_funcflag ? 1 : 0;
- $msg = array(
- 'ToUserName' => $this->getRevFrom(),
- 'FromUserName'=>$this->getRevTo(),
- 'MsgType'=>self::MSGTYPE_VOICE,
- 'Voice'=>array('MediaId'=>$mediaid),
- 'CreateTime'=>time(),
- 'FuncFlag'=>$FuncFlag
- );
- $this->Message($msg);
- return $this;
- }
-
- public function video($mediaid='',$title='',$description='')
- {
- $FuncFlag = $this->_funcflag ? 1 : 0;
- $msg = array(
- 'ToUserName' => $this->getRevFrom(),
- 'FromUserName'=>$this->getRevTo(),
- 'MsgType'=>self::MSGTYPE_VIDEO,
- 'Video'=>array(
- 'MediaId'=>$mediaid,
- 'Title'=>$title,
- 'Description'=>$description
- ),
- 'CreateTime'=>time(),
- 'FuncFlag'=>$FuncFlag
- );
- $this->Message($msg);
- return $this;
- }
-
- public function music($title,$desc,$musicurl,$hgmusicurl='',$thumbmediaid='') {
- $FuncFlag = $this->_funcflag ? 1 : 0;
- $msg = array(
- 'ToUserName' => $this->getRevFrom(),
- 'FromUserName'=>$this->getRevTo(),
- 'CreateTime'=>time(),
- 'MsgType'=>self::MSGTYPE_MUSIC,
- 'Music'=>array(
- 'Title'=>$title,
- 'Description'=>$desc,
- 'MusicUrl'=>$musicurl,
- 'HQMusicUrl'=>$hgmusicurl
- ),
- 'FuncFlag'=>$FuncFlag
- );
- if ($thumbmediaid) {
- $msg['Music']['ThumbMediaId'] = $thumbmediaid;
- }
- $this->Message($msg);
- return $this;
- }
-
- public function news($newsData=array())
- {
- $FuncFlag = $this->_funcflag ? 1 : 0;
- $count = count($newsData);
- $msg = array(
- 'ToUserName' => $this->getRevFrom(),
- 'FromUserName'=>$this->getRevTo(),
- 'MsgType'=>self::MSGTYPE_NEWS,
- 'CreateTime'=>time(),
- 'ArticleCount'=>$count,
- 'Articles'=>$newsData,
- 'FuncFlag'=>$FuncFlag
- );
- $this->Message($msg);
- return $this;
- }
-
- public function reply($msg=array(),$return = false)
- {
- if (empty($msg)) {
- if (empty($this->_msg))
- return false;
- $msg = $this->_msg;
- }
- $xmldata= $this->xml_encode($msg);
- $this->log($xmldata);
- if ($this->encrypt_type == 'aes') {
- $pc = new Prpcrypt($this->encodingAesKey);
- $array = $pc->encrypt($xmldata, $this->appid);
- $ret = $array[0];
- if ($ret != 0) {
- $this->log('encrypt err!');
- return false;
- }
- $timestamp = time();
- $nonce = rand(77,999)*rand(605,888)*rand(11,99);
- $encrypt = $array[1];
- $tmpArr = array($this->token, $timestamp, $nonce,$encrypt);
- sort($tmpArr, SORT_STRING);
- $signature = implode($tmpArr);
- $signature = sha1($signature);
- $xmldata = $this->generate($encrypt, $signature, $timestamp, $nonce);
- $this->log($xmldata);
- }
- if ($return)
- return $xmldata;
- else
- echo $xmldata;
- }
-
- private function generate($encrypt, $signature, $timestamp, $nonce)
- {
-
- $format = '<xml>
- <Encrypt><![CDATA[%s]]></Encrypt>
- <MsgSignature><![CDATA[%s]]></MsgSignature>
- <TimeStamp>%s</TimeStamp>
- <Nonce><![CDATA[%s]]></Nonce>
- </xml>';
- return sprintf($format, $encrypt, $signature, $timestamp, $nonce);
- }
-
- private function http_get($url){
- $oCurl = curl_init();
- if(stripos($url,'https://')!==FALSE){
- curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
- }
- $func=debug_backtrace();
- \think\facade\Log::info('接口:' . $func[1]['function']);
- \think\facade\Log::info('参数:' . $url);
- curl_setopt($oCurl, CURLOPT_URL, $url);
- curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
- $sContent = curl_exec($oCurl);
- $aStatus = curl_getinfo($oCurl);
- curl_close($oCurl);
- \think\facade\Log::info('返回值:' .$sContent);
- if(intval($aStatus['http_code'])==200){
- return $sContent;
- }else{
- return false;
- }
- }
-
- private function http_post($url,$param,$post_file=false){
- $oCurl = curl_init();
- if(stripos($url,'https://')!==FALSE){
- curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
- }
- $func=debug_backtrace();
- \think\facade\Log::info('接口:' . $func[1]['function']);
- \think\facade\Log::info('参数:' . json_encode($param ,
- JSON_UNESCAPED_UNICODE
- )
- );
- if (is_string($param) || $post_file) {
- $strPOST = $param;
- } else {
- $aPOST = array();
- foreach($param as $key=>$val){
- $aPOST[] = $key.'='.urlencode($val);
- }
- $strPOST = join('&', $aPOST);
- }
- curl_setopt($oCurl, CURLOPT_URL, $url);
- curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt($oCurl, CURLOPT_POST,true);
- curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
- $sContent = curl_exec($oCurl);
- $aStatus = curl_getinfo($oCurl);
- curl_close($oCurl);
- \think\facade\Log::info('返回值:' . json_encode($param ,
- JSON_UNESCAPED_UNICODE
- )
- );
- if(intval($aStatus['http_code'])==200){
- return $sContent;
- }else{
- return false;
- }
- }
-
- protected function setCache($cachename,$value,$expired){
- return Cache::set($cachename,$value,$expired);
-
- }
-
- protected function getCache($cachename){
-
-
- return Cache::get($cachename);
- }
-
- protected function removeCache($cachename){
-
- return Cache::delete($cachename);
- }
-
- public function checkAuth($appid='',$appsecret='',$token=''){
- if (!$appid || !$appsecret) {
- $appid = $this->appid;
- $appsecret = $this->appsecret;
- }
- if ($token) {
- $this->access_token=$token;
- return $this->access_token;
- }
- $authname = 'wechat_access_token'.$appid;
- if ($rs = $this->getCache($authname)) {
- $this->access_token = $rs;
- return $rs;
- }
- $result = $this->http_get(self::API_URL_PREFIX.self::AUTH_URL.'appid='.$appid.'&secret='.$appsecret);
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || isset($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- $this->access_token = $json['access_token'];
- $expire = $json['expires_in'] ? intval($json['expires_in'])-300 : 3600;
- $this->setCache($authname,$this->access_token,$expire);
- return $this->access_token;
- }
- return false;
- }
-
- public function resetAuth($appid=''){
- if (!$appid) $appid = $this->appid;
- $this->access_token = '';
- $authname = 'wechat_access_token'.$appid;
- $this->removeCache($authname);
- return true;
- }
-
- public function resetJsTicket($appid=''){
- if (!$appid) $appid = $this->appid;
- $this->jsapi_ticket = '';
- $authname = 'wechat_jsapi_ticket'.$appid;
- $this->removeCache($authname);
- return true;
- }
-
- public function getJsTicket($appid='',$jsapi_ticket=''){
- if (!$this->access_token && !$this->checkAuth()) return false;
- if (!$appid) $appid = $this->appid;
- if ($jsapi_ticket) {
- $this->jsapi_ticket = $jsapi_ticket;
- return $this->jsapi_ticket;
- }
- $authname = 'wechat_jsapi_ticket'.$appid;
- if ($rs = $this->getCache($authname)) {
- $this->jsapi_ticket = $rs;
- return $rs;
- }
- $result = $this->http_get(self::API_URL_PREFIX.self::GET_TICKET_URL.'access_token='.$this->access_token.'&type=jsapi');
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- $this->jsapi_ticket = $json['ticket'];
- $expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
- $this->setCache($authname,$this->jsapi_ticket,$expire);
- return $this->jsapi_ticket;
- }
- return false;
- }
-
- public function getJsSign($url, $timestamp=0, $noncestr='', $appid=''){
- if (!$this->jsapi_ticket && !$this->getJsTicket($appid) || !$url) return false;
- if (!$timestamp)
- $timestamp = time();
- if (!$noncestr)
- $noncestr = $this->generateNonceStr();
- $ret = strpos($url,'#');
- if ($ret)
- $url = substr($url,0,$ret);
- $url = trim($url);
- if (empty($url))
- return false;
- $arrdata = array('timestamp' => $timestamp, 'noncestr' => $noncestr, 'url' => $url, 'jsapi_ticket' => $this->jsapi_ticket);
- $sign = $this->getSignature($arrdata);
- if (!$sign)
- return false;
- $signPackage = array(
- 'appid' => $this->appid,
- 'noncestr' => $noncestr,
- 'timestamp' => $timestamp,
- 'url' => $url,
- 'signature' => $sign
- );
- return $signPackage;
- }
-
- static function json_encode($arr) {
- $parts = array ();
- $is_list = false;
-
- $keys = array_keys ( $arr );
- $max_length = count ( $arr ) - 1;
- if (($keys [0] === 0) && ($keys [$max_length] === $max_length )) {
- $is_list = true;
- for($i = 0; $i < count ( $keys ); $i ++) {
- if ($i != $keys [$i]) {
- $is_list = false;
- break;
- }
- }
- }
-
- foreach ( $arr as $key => $value ) {
- if (is_array ( $value )) {
- if ($is_list)
- $parts [] = self::json_encode ( $value );
- else
- $parts [] = '"' . $key . '":' . self::json_encode ( $value );
- } else {
- $str = '';
- if (! $is_list)
- $str = '"' . $key . '":';
-
- if (!is_string ( $value ) && is_numeric ( $value ) && $value<2000000000)
- $str .= $value;
- elseif ($value === false)
- $str .= 'false';
- elseif ($value === true)
- $str .= 'true';
- else
- $str .= '"' . addslashes ( $value ) . '"';
-
- $parts [] = $str;
- }
- }
- $json = implode ( ',', $parts );
- if ($is_list)
- return '[' . $json . ']';
- return '{' . $json . '}';
- }
-
- public function getSignature($arrdata,$method='sha1') {
- if (!function_exists($method)) return false;
- ksort($arrdata);
- $paramstring = '';
- foreach($arrdata as $key => $value)
- {
- if(strlen($paramstring) == 0)
- $paramstring .= $key . '=' . $value;
- else
- $paramstring .= '&' . $key . '=' . $value;
- }
- $Sign = $method($paramstring);
- return $Sign;
- }
-
- public function generateNonceStr($length=16){
-
- $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
- $str = '';
- for($i = 0; $i < $length; $i++)
- {
- $str .= $chars[mt_rand(0, strlen($chars) - 1)];
- }
- return $str;
- }
-
- public function getServerIp(){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_get(self::API_URL_PREFIX.self::CALLBACKSERVER_GET_URL.'access_token='.$this->access_token);
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || isset($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json['ip_list'];
- }
- return false;
- }
-
- public function createMenu($data){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_URL_PREFIX.self::MENU_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return true;
- }
- return false;
- }
-
- public function getMenu(){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_get(self::API_URL_PREFIX.self::MENU_GET_URL.'access_token='.$this->access_token);
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || isset($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function deleteMenu(){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_get(self::API_URL_PREFIX.self::MENU_DELETE_URL.'access_token='.$this->access_token);
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return true;
- }
- return false;
- }
-
- public function uploadMedia($data, $type){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::UPLOAD_MEDIA_URL.self::MEDIA_UPLOAD_URL.'access_token='.$this->access_token.'&type='.$type,$data,true);
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function getMedia($media_id){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_get(self::UPLOAD_MEDIA_URL.self::MEDIA_GET_URL.'access_token='.$this->access_token.'&media_id='.$media_id);
- if ($result)
- {
- $json = json_decode($result,true);
- if (isset($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $result;
- }
- return false;
- }
-
- public function uploadArticles($data){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOADNEWS_URL.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function uploadMpVideo($data){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::UPLOAD_MEDIA_URL.self::MEDIA_VIDEO_UPLOAD.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function sendMassMessage($data){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_URL_PREFIX.self::MASS_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function sendGroupMassMessage($data){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_URL_PREFIX.self::MASS_SEND_GROUP_URL.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function deleteMassMessage($msg_id){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $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)));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return true;
- }
- return false;
- }
-
- public function previewMassMessage($data){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_URL_PREFIX.self::MASS_PREVIEW_URL.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function queryMassMessage($msg_id){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $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)));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function getQRCode($scene_id,$type=0,$expire=1800){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $type = ($type && is_string($scene_id))?2:$type;
- $data = array(
- 'action_name'=>$type?($type == 2?'QR_LIMIT_STR_SCENE':'QR_LIMIT_SCENE'):'QR_SCENE',
- 'expire_seconds'=>$expire,
- 'action_info'=>array('scene'=>($type == 2?array('scene_str'=>$scene_id):array('scene_id'=>$scene_id)))
- );
- if ($type == 1) {
- unset($data['expire_seconds']);
- }
- $result = $this->http_post(self::API_URL_PREFIX.self::QRCODE_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function getQRUrl($ticket) {
- return self::QRCODE_IMG_URL.urlencode($ticket);
- }
-
- public function getShortUrl($long_url){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $data = array(
- 'action'=>'long2short',
- 'long_url'=>$long_url
- );
- $result = $this->http_post(self::API_URL_PREFIX.self::SHORT_URL.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json['short_url'];
- }
- return false;
- }
-
- public function getDatacube($type,$subtype,$begin_date,$end_date=''){
- if (!$this->access_token && !$this->checkAuth()) return false;
- if (!isset(self::$DATACUBE_URL_ARR[$type]) || !isset(self::$DATACUBE_URL_ARR[$type][$subtype]))
- return false;
- $data = array(
- 'begin_date'=>$begin_date,
- 'end_date'=>$end_date?$end_date:$begin_date
- );
- $result = $this->http_post(self::API_BASE_URL_PREFIX.self::$DATACUBE_URL_ARR[$type][$subtype].'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return isset($json['list'])?$json['list']:$json;
- }
- return false;
- }
-
- public function getUserList($next_openid=''){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_get(self::API_URL_PREFIX.self::USER_GET_URL.'access_token='.$this->access_token.'&next_openid='.$next_openid);
- if ($result)
- {
- $json = json_decode($result,true);
- if (isset($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function getUserInfo($openid){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_get(self::API_URL_PREFIX.self::USER_INFO_URL.'access_token='.$this->access_token.'&openid='.$openid);
- if ($result)
- {
- $json = json_decode($result,true);
- if (isset($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function updateUserRemark($openid,$remark){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $data = array(
- 'openid'=>$openid,
- 'remark'=>$remark
- );
- $result = $this->http_post(self::API_URL_PREFIX.self::USER_UPDATEREMARK_URL.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function getGroup(){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_get(self::API_URL_PREFIX.self::GROUP_GET_URL.'access_token='.$this->access_token);
- if ($result)
- {
- $json = json_decode($result,true);
- if (isset($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function getUserGroup($openid){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $data = array(
- 'openid'=>$openid
- );
- $result = $this->http_post(self::API_URL_PREFIX.self::USER_GROUP_URL.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- } else
- if (isset($json['groupid'])) return $json['groupid'];
- }
- return false;
- }
-
- public function createGroup($name){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $data = array(
- 'group'=>array('name'=>$name)
- );
- $result = $this->http_post(self::API_URL_PREFIX.self::GROUP_CREATE_URL.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function updateGroup($groupid,$name){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $data = array(
- 'group'=>array('id'=>$groupid,'name'=>$name)
- );
- $result = $this->http_post(self::API_URL_PREFIX.self::GROUP_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function updateGroupMembers($groupid,$openid){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $data = array(
- 'openid'=>$openid,
- 'to_groupid'=>$groupid
- );
- $result = $this->http_post(self::API_URL_PREFIX.self::GROUP_MEMBER_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function batchUpdateGroupMembers($groupid,$openid_list){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $data = array(
- 'openid_list'=>$openid_list,
- 'to_groupid'=>$groupid
- );
- $result = $this->http_post(self::API_URL_PREFIX.self::GROUP_MEMBER_BATCHUPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function sendCustomMessage($data){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_URL_PREFIX.self::CUSTOM_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function getOauthRedirect($callback,$state='',$scope='snsapi_userinfo'){
- return self::OAUTH_PREFIX.self::OAUTH_AUTHORIZE_URL.'appid='.$this->appid.'&redirect_uri='.urlencode($callback).'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';
- }
-
- public function getOauthAccessToken(){
- $code = isset($_GET['code'])?$_GET['code']:'';
- if (!$code) return false;
- $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');
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- $this->user_token = $json['access_token'];
- return $json;
- }
- return false;
- }
- public function getAccessTokenByCode($code=''){
- if (!$code) return false;
- $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');
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- $this->user_token = $json['access_token'];
- return $json;
- }
- return false;
- }
-
- public function getOauthRefreshToken($refresh_token){
- $result = $this->http_get(self::API_BASE_URL_PREFIX.self::OAUTH_REFRESH_URL.'appid='.$this->appid.'&grant_type=refresh_token&refresh_token='.$refresh_token);
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- $this->user_token = $json['access_token'];
- return $json;
- }
- return false;
- }
-
- public function getOauthUserinfo($access_token,$openid){
- $result = $this->http_get(self::API_BASE_URL_PREFIX.self::OAUTH_USERINFO_URL.'access_token='.$access_token.'&openid='.$openid);
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function getOauthAuth($access_token,$openid){
- $result = $this->http_get(self::API_BASE_URL_PREFIX.self::OAUTH_AUTH_URL.'access_token='.$access_token.'&openid='.$openid);
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- } else
- if ($json['errcode']==0) return true;
- }
- return false;
- }
-
- public function setTMIndustry($id1,$id2=''){
- if ($id1) $data['industry_id1'] = $id1;
- if ($id2) $data['industry_id2'] = $id2;
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_SET_INDUSTRY_URL.'access_token='.$this->access_token,self::json_encode($data));
- if($result){
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function addTemplateMessage($tpl_id){
- $data = array ('template_id_short' =>$tpl_id);
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_ADD_TPL_URL.'access_token='.$this->access_token,self::json_encode($data));
- if($result){
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json['template_id'];
- }
- return false;
- }
-
- public function sendTemplateMessage($data){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_SEND_URL.'access_token='.$this->access_token,
- self::json_encode($data));
- if($result){
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function getAllPrivateTemplate(){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_URL_PREFIX.self::GET_ALL_PRIVATE_TEMPLATE.'access_token='.$this->access_token,'');
- if($result){
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function getCustomServiceMessage($data){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_URL_PREFIX.self::CUSTOM_SERVICE_GET_RECORD.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function transfer_customer_service($customer_account = '')
- {
- $msg = array(
- 'ToUserName' => $this->getRevFrom(),
- 'FromUserName'=>$this->getRevTo(),
- 'CreateTime'=>time(),
- 'MsgType'=>'transfer_customer_service',
- );
- if ($customer_account) {
- $msg['TransInfo'] = array('KfAccount'=>$customer_account);
- }
- $this->Message($msg);
- return $this;
- }
-
- public function getCustomServiceKFlist(){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_get(self::API_URL_PREFIX.self::CUSTOM_SERVICE_GET_KFLIST.'access_token='.$this->access_token);
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function getCustomServiceOnlineKFlist(){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_get(self::API_URL_PREFIX.self::CUSTOM_SERVICE_GET_ONLINEKFLIST.'access_token='.$this->access_token);
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function createKFSession($openid,$kf_account,$text=''){
- $data=array(
- 'openid' =>$openid,
- 'kf_account' => $kf_account
- );
- if ($text) $data['text'] = $text;
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX.self::CUSTOM_SESSION_CREATE.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function closeKFSession($openid,$kf_account,$text=''){
- $data=array(
- 'openid' =>$openid,
- 'nickname' => $kf_account
- );
- if ($text) $data['text'] = $text;
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX.self::CUSTOM_SESSION_CLOSE .'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function getKFSession($openid){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_get(self::API_BASE_URL_PREFIX.self::CUSTOM_SESSION_GET .'access_token='.$this->access_token.'&openid='.$openid);
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function getKFSessionlist($kf_account){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_get(self::API_BASE_URL_PREFIX.self::CUSTOM_SESSION_GET_LIST .'access_token='.$this->access_token.'&kf_account='.$kf_account);
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function getKFSessionWait(){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_get(self::API_BASE_URL_PREFIX.self::CUSTOM_SESSION_GET_WAIT .'access_token='.$this->access_token);
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function addKFAccount($account,$nickname,$password){
- $data=array(
- 'kf_account' =>$account,
- 'nickname' => $nickname,
- 'password' => md5($password)
- );
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX.self::CS_KF_ACCOUNT_ADD_URL.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function updateKFAccount($account,$nickname,$password){
- $data=array(
- 'kf_account' =>$account,
- 'nickname' => $nickname,
- 'password' => md5($password)
- );
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX.self::CS_KF_ACCOUNT_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function deleteKFAccount($account){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_get(self::API_BASE_URL_PREFIX.self::CS_KF_ACCOUNT_DEL_URL.'access_token='.$this->access_token.'&kf_account='.$account);
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function setKFHeadImg($account,$imgfile){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $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);
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function querySemantic($uid,$query,$category,$latitude=0,$longitude=0,$city='',$region=''){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $data=array(
- 'query' => $query,
- 'category' => $category,
- 'appid' => $this->appid,
- 'uid' => ''
- );
-
- if ($latitude) {
- $data['latitude'] = $latitude;
- $data['longitude'] = $longitude;
- } elseif ($city) {
- $data['city'] = $city;
- } elseif ($region) {
- $data['region'] = $region;
- }
- $result = $this->http_post(self::API_BASE_URL_PREFIX.self::SEMANTIC_API_URL.'access_token='.$this->access_token,self::json_encode($data));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function createCard($data) {
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CREATE . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function updateCard($data) {
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return true;
- }
- return false;
- }
-
- public function delCard($card_id) {
- $data = array(
- 'card_id' => $card_id,
- );
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_DELETE . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return true;
- }
- return false;
- }
-
- public function getCardInfo($card_id) {
- $data = array(
- 'card_id' => $card_id,
- );
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_GET . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function getCardColors() {
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_get(self::API_BASE_URL_PREFIX . self::CARD_GETCOLORS . 'access_token=' . $this->access_token);
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function getCardLocations($offset=0,$count=0) {
- $data=array(
- 'offset'=>$offset,
- 'count'=>$count
- );
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_LOCATION_BATCHGET . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function addCardLocations($data) {
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_LOCATION_BATCHADD . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function createCardQrcode($card_id,$code='',$openid='',$expire_seconds=0,$is_unique_code=false,$balance='') {
- $card = array(
- 'card_id' => $card_id
- );
- if ($code)
- $card['code'] = $code;
- if ($openid)
- $card['openid'] = $openid;
- if ($expire_seconds)
- $card['expire_seconds'] = $expire_seconds;
- if ($is_unique_code)
- $card['is_unique_code'] = $is_unique_code;
- if ($balance)
- $card['balance'] = $balance;
- $data = array(
- 'action_name' => 'QR_CARD',
- 'action_info' => array('card' => $card)
- );
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_QRCODE_CREATE . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function consumeCardCode($code,$card_id='') {
- $data = array('code' => $code);
- if ($card_id)
- $data['card_id'] = $card_id;
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_CONSUME . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function decryptCardCode($encrypt_code) {
- $data = array(
- 'encrypt_code' => $encrypt_code,
- );
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_DECRYPT . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function checkCardCode($code) {
- $data = array(
- 'code' => $code,
- );
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_GET . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function getCardIdList($offset=0,$count=50) {
- if ($count>50)
- $count = 50;
- $data = array(
- 'offset' => $offset,
- 'count' => $count,
- );
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_BATCHGET . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function updateCardCode($code,$card_id,$new_code) {
- $data = array(
- 'code' => $code,
- 'card_id' => $card_id,
- 'new_code' => $new_code,
- );
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return true;
- }
- return false;
- }
-
- public function unavailableCardCode($code,$card_id='') {
- $data = array(
- 'code' => $code,
- );
- if ($card_id)
- $data['card_id'] = $card_id;
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_CODE_UNAVAILABLE . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return true;
- }
- return false;
- }
-
- public function modifyCardStock($data) {
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_MODIFY_STOCK . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return true;
- }
- return false;
- }
-
- public function activateMemberCard($data) {
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_MEMBERCARD_ACTIVATE . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return true;
- }
- return false;
- }
-
- public function updateMemberCard($data) {
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_MEMBERCARD_UPDATEUSER . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
- public function updateLuckyMoney($code,$balance,$card_id='') {
- $data = array(
- 'code' => $code,
- 'balance' => $balance
- );
- if ($card_id)
- $data['card_id'] = $card_id;
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_LUCKYMONEY_UPDATE . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return true;
- }
- return false;
- }
-
- public function setCardTestWhiteList($openid=array(),$user=array()) {
- $data = array();
- if (count($openid) > 0)
- $data['openid'] = $openid;
- if (count($user) > 0)
- $data['username'] = $user;
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::CARD_TESTWHILELIST_SET . 'access_token=' . $this->access_token, self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return true;
- }
- return false;
- }
-
- public function subBizSend($openid='',$templat_id='',$data=array(),$page='',$miniprogram=array()){
- $temp=[];
- $temp['touser'] = $openid;
- $temp['template_id'] = $templat_id;
- $temp['page'] = $page;
- $temp['miniprogram'] = $miniprogram;
- $temp['data'] = $data;
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_URL_PREFIX . self::SUB_BIZSEND . 'access_token=' . $this->access_token,json_encode($temp));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || (!empty($json['errcode'])&& $json['errcode']!=='ok')) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return true;
- }
- return false;
- }
-
- public function getTemplate(){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::GET_TEMPLATE . 'access_token=' .$this->access_token,'');
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || (!empty($json['errcode'])&& $json['errcode']!=='ok')) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
-
- public function getPubTemplate($ids="",$start=0,$limit=30){
- $data=["ids"=>$ids,"start"=>$start,"limit"=>$limit];
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::GET_PUB_TEMPLATE . 'access_token=' .
- $this->access_token,self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || (!empty($json['errcode'])&& $json['errcode']!=='ok')) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
-
-
- public function getCategory(){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::GET_CATEGORY . 'access_token=' .
- $this->access_token,'');
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || (!empty($json['errcode'])&& $json['errcode']!=='ok')) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
-
-
- public function addTemplate(string $tid='',array $kidList=[], string $sceneDesc=""){
- $data=["tid"=>$tid,"kidList"=>$kidList,"sceneDesc"=>$sceneDesc];
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::ADD_TEMPLATE . 'access_token='
- .$this->access_token,self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || (!empty($json['errcode'])&& $json['errcode']!=='ok')) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return true;
- }
- return false;
- }
-
-
- public function deleteTemplate($priTmplId=''){
- $data=['priTmplId'=>$priTmplId];
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::DELETE_TEMPLATE . 'access_token='
- .$this->access_token,self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || (!empty($json['errcode'])&& $json['errcode']!=='ok')) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return true;
- }
- return false;
- }
-
-
- public function getPubTemplateKeyWordsById($priTmplId=''){
- $data=['tid'=>$priTmplId];
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_BASE_URL_PREFIX . self::GET_PUB_TEMPLATE_KEY_WORD . 'access_token='
- .$this->access_token,self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || (!empty($json['errcode'])&& $json['errcode']!=='ok')) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
-
-
- public function clearQuota(){
- $data=['appid'=>$this->appid];
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_URL_PREFIX . self::CLEAR_QUOTA . 'access_token='
- .$this->access_token,self::json_encode($data));
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || (!empty($json['errcode'])&& $json['errcode']!=='ok')) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return true;
- }
- return false;
- }
-
-
- public function clearQuotaV2(){
- $data=['appid'=>$this->appid,"appsecret"=>$this->appsecret];
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_URL_PREFIX . self::CLEAR_QUOTA_v2,$data);
- if ($result) {
- $json = json_decode($result, true);
- if (!$json || (!empty($json['errcode'])&& $json['errcode']!=='ok')) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- return true;
- }
- return false;
- }
-
-
- public function QuotaNum($path='')
- {
- $data=['cgi_path'=>$path];
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result=$this->http_post(self::API_URL_PREFIX . self::QUOTA. 'access_token='
- .$this->access_token, self::json_encode($data));
- if ($result) {
- $json=json_decode($result , true);
- if (!$json || (!empty($json['errcode']) && $json['errcode'] !== 'ok')) {
- $this->errCode=$json['errcode'];
- $this->errMsg=$json['errmsg'];
- return false;
- }
- return $json;
- }
- return false;
- }
- }
- class PKCS7Encoder
- {
- public static $block_size = 32;
-
- function encode($text)
- {
- $block_size = PKCS7Encoder::$block_size;
- $text_length = strlen($text);
-
- $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
- if ($amount_to_pad == 0) {
- $amount_to_pad = PKCS7Encoder::block_size;
- }
-
- $pad_chr = chr($amount_to_pad);
- $tmp = '';
- for ($index = 0; $index < $amount_to_pad; $index++) {
- $tmp .= $pad_chr;
- }
- return $text . $tmp;
- }
-
- function decode($text)
- {
- $pad = ord(substr($text, -1));
- if ($pad < 1 || $pad > PKCS7Encoder::$block_size) {
- $pad = 0;
- }
- return substr($text, 0, (strlen($text) - $pad));
- }
- }
- class Prpcrypt
- {
- public $key;
- function __construct($k) {
- $this->key = base64_decode($k . '=');
- }
-
- function Prpcrypt($k)
- {
- $this->key = base64_decode($k . '=');
- }
-
- public function encrypt($text, $appid)
- {
- try {
-
- $random = $this->getRandomStr();
- $text = $random . pack('N', strlen($text)) . $text . $appid;
-
- $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
- $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
- $iv = substr($this->key, 0, 16);
-
- $pkc_encoder = new PKCS7Encoder;
- $text = $pkc_encoder->encode($text);
- mcrypt_generic_init($module, $this->key, $iv);
-
- $encrypted = mcrypt_generic($module, $text);
- mcrypt_generic_deinit($module);
- mcrypt_module_close($module);
-
-
- return array(ErrorCode::$OK, base64_encode($encrypted));
- } catch (Exception $e) {
-
- return array(ErrorCode::$EncryptAESError, null);
- }
- }
-
- public function decrypt($encrypted, $appid)
- {
- try {
-
- $ciphertext_dec = base64_decode($encrypted);
- $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
- $iv = substr($this->key, 0, 16);
- mcrypt_generic_init($module, $this->key, $iv);
-
- $decrypted = mdecrypt_generic($module, $ciphertext_dec);
- mcrypt_generic_deinit($module);
- mcrypt_module_close($module);
- } catch (Exception $e) {
- return array(ErrorCode::$DecryptAESError, null);
- }
- try {
-
- $pkc_encoder = new PKCS7Encoder;
- $result = $pkc_encoder->decode($decrypted);
-
- if (strlen($result) < 16)
- return '';
- $content = substr($result, 16, strlen($result));
- $len_list = unpack('N', substr($content, 0, 4));
- $xml_len = $len_list[1];
- $xml_content = substr($content, 4, $xml_len);
- $from_appid = substr($content, $xml_len + 4);
- if (!$appid)
- $appid = $from_appid;
-
- } catch (Exception $e) {
-
- return array(ErrorCode::$IllegalBuffer, null);
- }
- if ($from_appid != $appid)
- return array(ErrorCode::$ValidateAppidError, null);
-
- return array(0, $xml_content, $from_appid);
- }
-
- function getRandomStr()
- {
- $str = '';
- $str_pol = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
- $max = strlen($str_pol) - 1;
- for ($i = 0; $i < 16; $i++) {
- $str .= $str_pol[mt_rand(0, $max)];
- }
- return $str;
- }
- }
- class ErrorCode
- {
- public static $OK = 0;
- public static $ValidateSignatureError = 40001;
- public static $ParseXmlError = 40002;
- public static $ComputeSignatureError = 40003;
- public static $IllegalAesKey = 40004;
- public static $ValidateAppidError = 40005;
- public static $EncryptAESError = 40006;
- public static $DecryptAESError = 40007;
- public static $IllegalBuffer = 40008;
- public static $EncodeBase64Error = 40009;
- public static $DecodeBase64Error = 40010;
- public static $GenReturnXmlError = 40011;
- public static $errCode=array(
- '0' => '处理成功',
- '40001' => '校验签名失败',
- '40002' => '解析xml失败',
- '40003' => '计算签名失败',
- '40004' => '不合法的AESKey',
- '40005' => '校验AppID失败',
- '40006' => 'AES加密失败',
- '40007' => 'AES解密失败',
- '40008' => '公众平台发送的xml不合法',
- '40009' => 'Base64编码失败',
- '40010' => 'Base64解码失败',
- '40011' => '公众帐号生成回包xml失败'
- );
- public static function getErrText($err) {
- if (isset(self::$errCode[$err])) {
- return self::$errCode[$err];
- }else {
- return false;
- };
- }
- }
|