1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427 |
- <?php
- 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 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 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 = '/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 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/oauth2/sns/userinfo?';
- const OAUTH_AUTH_URL = '/sns/auth?';
- const PAY_DELIVERNOTIFY = '/pay/delivernotify?';
- const PAY_ORDERQUERY = '/pay/orderquery?';
-
- const CUSTOM_SERVICE_GET_RECORD = '/customservice/getrecord?';
- const CUSTOM_SERVICE_GET_KFLIST = '/customservice/getkflist?';
- const CUSTOM_SERVICE_GET_ONLINEKFLIST = '/customservice/getonlinekflist?';
- const CUSTOM_SEESSION_CREATE = '/customservice/kfsession/create?';
- const CUSTOM_SEESSION_CLOSE = '/customservice/kfsession/close?';
- const CUSTOM_SEESSION_SWITCH = '/customservice/kfsession/switch?';
- const CUSTOM_SEESSION_GET = '/customservice/kfsession/getsession?';
- const CUSTOM_SEESSION_GET_LIST = '/customservice/kfsession/getsessionlist?';
- const CUSTOM_SEESSION_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?';
- private $token;
- 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";
- private $_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->partnerid = isset($options['partnerid'])?$options['partnerid']:'';
- $this->partnerkey = isset($options['partnerkey'])?$options['partnerkey']:'';
- $this->paysignkey = isset($options['paysignkey'])?$options['paysignkey']:'';
- $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;
- }
- private function log($log){
- if ($this->debug ) {
- if (function_exists($this->_logcallback)) {
- if (is_array($log)) $log = print_r($log,true);
- return call_user_func($this->_logcallback,$log);
- }elseif (class_exists('Log')) {
- Log::write('wechat:'.$log, Log::DEBUG);
- }
- }
- return false;
- }
-
- 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['SendPicsInfo'])) {
- $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);
- }
- curl_setopt($oCurl, CURLOPT_URL, $url);
- curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
- $sContent = curl_exec($oCurl);
- $aStatus = curl_getinfo($oCurl);
- curl_close($oCurl);
- 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);
- }
- 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);
- if(intval($aStatus["http_code"])==200){
- return $sContent;
- }else{
- return false;
- }
- }
-
- public function checkAuth($appid='',$appsecret='',$token=''){
- if (!$appid || !$appsecret) {
- $appid = $this->appid;
- $appsecret = $this->appsecret;
- }
- $authname = 'wechat_access_token'.$appid;
- if ($token) {
- $this->access_token=$token;
- return $this->access_token;
- }
- if ($rs = S($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'])-100 : 3600;
- S($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;
- S($authname,null);
- return true;
- }
-
- public function resetJsTicket($appid=''){
- if (!$appid) $appid = $this->appid;
- $this->jsapi_ticket = '';
- $authname = 'wechat_jsapi_ticket'.$appid;
- S($authname,null);
- return true;
- }
-
- public function getJsTicket($appid='',$jsapi_ticket=''){
- if (!$this->access_token && !$this->checkAuth()) return false;
- if ($jsapi_ticket) {
- $this->jsapi_ticket = $jsapi_ticket;
- return $this->access_token;
- }
- $authname = 'wechat_jsapi_ticket'.$appid;
- if ($rs = S($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;
- S($authname,$this->access_token,$expire);
- return $this->jsapi_ticket;
- }
- return false;
- }
-
- public function getJsSign($url, $timeStamp, $nonceStr, $appid=''){
- if (!$this->jsapi_ticket && !$this->getJsTicket($appid)) return false;
- $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);
- return $this->getSignature($arrdata);
- }
-
- 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 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.'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;
- $data = array(
- 'action_name'=>$type?"QR_LIMIT_SCENE":"QR_SCENE",
- 'expire_seconds'=>$expire,
- 'action_info'=>array('scene'=>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.$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 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 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 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 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;
- }
- $paySign = $method($paramstring);
- return $paySign;
- }
-
- 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 createNativeUrl($productid){
- $nativeObj["appid"] = $this->appid;
- $nativeObj["appkey"] = $this->paysignkey;
- $nativeObj["productid"] = urlencode($productid);
- $nativeObj["timestamp"] = time();
- $nativeObj["noncestr"] = $this->generateNonceStr();
- $nativeObj["sign"] = $this->getSignature($nativeObj);
- unset($nativeObj["appkey"]);
- $bizString = "";
- foreach($nativeObj as $key => $value)
- {
- if(strlen($bizString) == 0)
- $bizString .= $key . "=" . $value;
- else
- $bizString .= "&" . $key . "=" . $value;
- }
- return "weixin://wxpay/bizpayurl?".$bizString;
-
- }
-
- public function createPackage($out_trade_no,$body,$total_fee,$notify_url,$spbill_create_ip,$fee_type=1,$bank_type="WX",$input_charset="UTF-8",$time_start="",$time_expire="",$transport_fee="",$product_fee="",$goods_tag="",$attach=""){
- $arrdata = array("bank_type" => $bank_type, "body" => $body, "partner" => $this->partnerid, "out_trade_no" => $out_trade_no, "total_fee" => $total_fee, "fee_type" => $fee_type, "notify_url" => $notify_url, "spbill_create_ip" => $spbill_create_ip, "input_charset" => $input_charset);
- if ($time_start) $arrdata['time_start'] = $time_start;
- if ($time_expire) $arrdata['time_expire'] = $time_expire;
- if ($transport_fee) $arrdata['transport_fee'] = $transport_fee;
- if ($product_fee) $arrdata['product_fee'] = $product_fee;
- if ($goods_tag) $arrdata['goods_tag'] = $goods_tag;
- if ($attach) $arrdata['attach'] = $attach;
- ksort($arrdata);
- $paramstring = "";
- foreach($arrdata as $key => $value)
- {
- if(strlen($paramstring) == 0)
- $paramstring .= $key . "=" . $value;
- else
- $paramstring .= "&" . $key . "=" . $value;
- }
- $stringSignTemp = $paramstring . "&key=" . $this->partnerkey;
- $signValue = strtoupper(md5($stringSignTemp));
- $package = http_build_query($arrdata) . "&sign=" . $signValue;
- return $package;
- }
-
- public function getPaySign($package, $timeStamp, $nonceStr){
- $arrdata = array("appid" => $this->appid, "timestamp" => $timeStamp, "noncestr" => $nonceStr, "package" => $package, "appkey" => $this->paysignkey);
- $paySign = $this->getSignature($arrdata);
- return $paySign;
- }
-
- public function checkOrderSignature($orderxml=''){
- if (!$orderxml) {
- $postStr = file_get_contents("php://input");
- if (!empty($postStr)) {
- $orderxml = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
- } else return false;
- }
- $arrdata = array('appid'=>$orderxml['AppId'],'appkey'=>$this->paysignkey,'timestamp'=>$orderxml['TimeStamp'],'noncestr'=>$orderxml['NonceStr'],'openid'=>$orderxml['OpenId'],'issubscribe'=>$orderxml['IsSubscribe']);
- $paySign = $this->getSignature($arrdata);
- if ($paySign!=$orderxml['AppSignature']) return false;
- return true;
- }
-
- public function sendPayDeliverNotify($openid,$transid,$out_trade_no,$status=1,$msg='ok'){
- if (!$this->access_token && !$this->checkAuth()) return false;
- $postdata = array(
- "appid"=>$this->appid,
- "appkey"=>$this->paysignkey,
- "openid"=>$openid,
- "transid"=>strval($transid),
- "out_trade_no"=>strval($out_trade_no),
- "deliver_timestamp"=>strval(time()),
- "deliver_status"=>strval($status),
- "deliver_msg"=>$msg,
- );
- $postdata['app_signature'] = $this->getSignature($postdata);
- $postdata['sign_method'] = 'sha1';
- unset($postdata['appkey']);
- $result = $this->http_post(self::API_BASE_URL_PREFIX.self::PAY_DELIVERNOTIFY.'access_token='.$this->access_token,self::json_encode($postdata));
- 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 getPayOrder($out_trade_no) {
- if (!$this->access_token && !$this->checkAuth()) return false;
- $sign = strtoupper(md5("out_trade_no=$out_trade_no&partner={$this->partnerid}&key={$this->partnerkey}"));
- $postdata = array(
- "appid"=>$this->appid,
- "appkey"=>$this->paysignkey,
- "package"=>"out_trade_no=$out_trade_no&partner={$this->partnerid}&sign=$sign",
- "timestamp"=>strval(time()),
- );
- $postdata['app_signature'] = $this->getSignature($postdata);
- $postdata['sign_method'] = 'sha1';
- unset($postdata['appkey']);
- $result = $this->http_post(self::API_BASE_URL_PREFIX.self::PAY_ORDERQUERY.'access_token='.$this->access_token,self::json_encode($postdata));
- if ($result)
- {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'].json_encode($postdata);
- return false;
- }
- return $json["order_info"];
- }
- return false;
- }
-
- public function getAddrSign($url, $timeStamp, $nonceStr, $user_token=''){
- if (!$user_token) $user_token = $this->user_token;
- if (!$user_token) {
- $this->errMsg = 'no user access token found!';
- return false;
- }
- $url = htmlspecialchars_decode($url);
- $arrdata = array(
- 'appid'=>$this->appid,
- 'url'=>$url,
- 'timestamp'=>strval($timeStamp),
- 'noncestr'=>$nonceStr,
- 'accesstoken'=>$user_token
- );
- return $this->getSignature($arrdata);
- }
-
- 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 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,
- "nickname" => $kf_account
- );
- if ($text) $data["text"] = $text;
- if (!$this->access_token && !$this->checkAuth()) return false;
- $result = $this->http_post(self::API_URL_PREFIX.self::CUSTOM_SEESSION_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_URL_PREFIX.self::CUSTOM_SEESSION_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_URL_PREFIX.self::CUSTOM_SEESSION_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_URL_PREFIX.self::CUSTOM_SEESSION_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_URL_PREFIX.self::CUSTOM_SEESSION_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,$code_id,$new_code) {
- $data = array(
- 'code' => $card,
- '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,$code_id='') {
- $data = array(
- 'code' => $code,
- );
- if ($code_id)
- $data['code_id'] = $code_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;
- }
- }
- 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 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;
- };
- }
- }
|