12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394 |
- <?php
- namespace app\components\WeiXin;
- use Yii;
- class WeiXin extends \yii\base\Component{
- private $appId;
- private $appSecret;
- private $token;
- private $jsDomain;
- private $accessToken;
- private $acessTokenFile;
- private $jsApiTicketFile;
- private $getAccessTokenUrl;
- private $getIpListUrl;
- private $getUserInfoUrl;
- private $getJsApiTicketUrl;
- private $addMaterialUrl;
- private $addNewsUrl;
- private $uploadImgUrl;
- private $getMediaUrl;
- private $updateNewsUrl;
- private $delMediaUrl;
- private $getMaterialCountUrl;
- private $batcheGetMaterialUrl;
- private $uploadNewsVideoUrl;
- private $msgSendAllUrl;
- private $msgSendUrl;
- private $msgPreviewUrl;
- private $delMsgUrl;
- private $getMsgStatusUrl;
- private $createGroupUrl;
- private $getGroupsUrl;
- private $getGroupIdByOpenIdUrl;
- private $updateGroupNameUrl;
- private $changeUserGroupUrl;
- private $batchChangeUserGroupUrl;
- private $delGroupUrl;
- private $updateRemarkUrl;
- private $batchGetUserInfoUrl;
- private $getUserListUrl;
- private $getWebAuthCodeUrl;
- private $webAuthAccessToken;
- private $getWebAuthAccessTokenUrl;
- private $currentOpenId;
- private $getWebAuthUserInfoUrl;
- private $createMenuUrl;
- private $getMenuListUrl;
- private $deleteMenuUrl;
- private $createQrcodeUrl;
- private $getQrcodeUrl;
- private $getShortUrl;
- private $tplMsgUrl;
- private $customerMsgUrl;
- private $wxIpList;
- public $parameters;
-
- public $basePath;
- public $partnerKey;
- public $mchId;
- public $addOrderUrl;
- public $qryOrderUrl;
- public $redPackUrl;
- public $signType = 'MD5';
- public $timestamp;
-
- public function __construct($args)
- {
- extract($args);
- if(empty($appId)||empty($appSecret)||empty($token))$this->showErrorMessage('缺少appid,或appsecret,或token!');
- $this->appId = $appId;
- $this->appSecret = $appSecret;
- $this->token = $token;
- $this->timestamp = time();
- !empty($jsDomain)&&$this->jsDomain = $jsDomain;
- $this->basePath = Yii::$app->params['wxPath'];
- dir_create($this->basePath);
- $this->acessTokenFile = $this->basePath.$this->appId.'_access_token.json';
- $this->jsApiTicketFile = $this->basePath.$this->appId.'_jsapi_ticket.json';
- }
-
- public function init()
- {
- $this->getAccessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appId."&secret=".$this->appSecret;
- $this->getIpListUrl = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=%s";
- $this->getUserInfoUrl= "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN";
- $this->getJsApiTicketUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi";
- $this->addMaterialUrl= "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=%s";
- $this->addNewsUrl = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=%s";
- $this->uploadImgUrl = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=%s";
- $this->getMediaUrl = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=%s";
- $this->updateNewsUrl = "https://api.weixin.qq.com/cgi-bin/material/update_news?access_token=%s";
- $this->delMediaUrl = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=%s";
- $this->getMaterialCountUrl = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=%s";
- $this->batcheGetMaterialUrl = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=%s";
- $this->uploadNewsVideoUrl = "https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token=%s";
- $this->msgSendAllUrl = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=%s";
- $this->msgSendUrl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=%s";
- $this->msgPreviewUrl = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=%s";
- $this->delMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token=%s";
- $this->getMsgStatusUrl = "https://api.weixin.qq.com/cgi-bin/message/mass/get?access_token=%s";
- $this->createGroupUrl = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token=%s";
- $this->getGroupsUrl = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=%s";
- $this->getGroupIdByOpenIdUrl = "https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=%s";
- $this->updateGroupNameUrl = "https://api.weixin.qq.com/cgi-bin/groups/update?access_token=%s";
- $this->changeUserGroupUrl = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=%s";
- $this->batchChangeUserGroupUrl = "https://api.weixin.qq.com/cgi-bin/groups/members/batchupdate?access_token=%s";
- $this->delGroupUrl = "https://api.weixin.qq.com/cgi-bin/groups/delete?access_token=%s";
- $this->updateRemarkUrl = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=%s";
- $this->batchGetUserInfoUrl = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=%s";
- $this->getUserListUrl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s";
- $this->getWebAuthCodeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect";
- $this->getWebAuthAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
- $this->getWebAuthUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN";
- $this->createMenuUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s";
- $this->getMenuListUrl = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=%s";
- $this->deleteMenuUrl = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=%s";
- $this->createQrcodeUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s";
- $this->getQrcodeUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s";
- $this->getShortUrl = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=%s";
- $this->addOrderUrl = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
- $this->qryOrderUrl = 'https://api.mch.weixin.qq.com/pay/orderquery';
- $this->redPackUrl = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
- $this->tplMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s";
- $this->customerMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s";
- return $this;
- }
-
- public function createShortUrl($longUrl)
- {
- $this->getAccessToken();
- $this->getShortUrl = sprintf($this->getShortUrl,$this->accessToken);
- $postdata = json_encode(array('action'=>'long2short','long_url'=>$longUrl),JSON_UNESCAPED_UNICODE);
- $result = $this->httpsRequest($this->getShortUrl, $postdata);
- $result = json_decode($result,true);
- return $result;
- }
-
- public function createQrcode($sceneId,$type=1,$expireSeconds=604800)
- {
- $this->getAccessToken();
- $this->createQrcodeUrl = sprintf($this->createQrcodeUrl,$this->accessToken);
- if($type==1)
- {
- $postdata = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$sceneId.'}}}';
- }
- if($type==2)
- {
- $postdata = '{"expire_seconds": '.$expireSeconds.', "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$sceneId.'}}}';
- }
- if($type==3)
- {
- $postdata = '{"expire_seconds": '.$expireSeconds.', "action_name": "QR_STR_SCENE", "action_info": {"scene": {"scene_str": "'.$sceneId.'"}}}';
- }
- $result = $this->httpsRequest($this->createQrcodeUrl, $postdata);
- $result = json_decode($result,true);
- $result['codeUrl'] = sprintf($this->getQrcodeUrl,urlencode($result['ticket']));
- return $result;
- }
-
- public function deleteMenu()
- {
- $this->getAccessToken();
- $this->deleteMenuUrl = sprintf($this->deleteMenuUrl,$this->accessToken);
- $result = json_decode($this->httpGet($this->deleteMenuUrl),true);
- return $result;
- }
-
- public function createMenu($buttons)
- {
- $this->getAccessToken();
- $this->createMenuUrl = sprintf($this->createMenuUrl,$this->accessToken);
- $array['button'] = $buttons;
- $result = $this->httpsRequest($this->createMenuUrl, json_encode($array,JSON_UNESCAPED_UNICODE));
- $result = json_decode($result,true);
- return $result;
- }
-
- public function getMenuList()
- {
- $this->getAccessToken();
- $this->getMenuListUrl = sprintf($this->getMenuListUrl,$this->accessToken);
- $result = json_decode($this->httpGet($this->getMenuListUrl),true);
- return $result;
- }
-
- public function getWebAuthUrl($redirectUrl,$scope='snsapi_userinfo',$state='MR8888')
- {
- $this->getWebAuthCodeUrl = sprintf($this->getWebAuthCodeUrl,$this->appId,$redirectUrl,$scope,$state);
- return $this->getWebAuthCodeUrl;
- }
-
- public function getWebAuthAccessToken()
- {
- try
- {
- $this->getWebAuthAccessTokenUrl = sprintf($this->getWebAuthAccessTokenUrl,$this->appId,$this->appSecret,$_GET['code']);
- $data = json_decode($this->httpGet($this->getWebAuthAccessTokenUrl),true);
- $this->webAuthAccessToken = $data['access_token'];
- $this->currentOpenId = $data['openid'];
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function getOauthAccessToken($code)
- {
- $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appId."&secret=".$this->appSecret."&code=$code&grant_type=authorization_code";
- return json_decode($this->httpGet($url),true);
- }
-
- public function getWebAuthUserInfo()
- {
-
- $this->getWebAuthAccessToken();
- $this->getWebAuthUserInfoUrl = sprintf($this->getWebAuthUserInfoUrl,$this->webAuthAccessToken,$this->currentOpenId);
- $result = json_decode($this->httpGet($this->getWebAuthUserInfoUrl),true);
- $result['nick_name'] = $result['nickname'];
- $result['avatar'] = $result['headimgurl'];
- return $result;
- }
-
-
-
- public function getUserList($nextOpenId='')
- {
- try
- {
- $this->getAccessToken();
- $this->getUserListUrl = sprintf($this->getUserListUrl,$this->accessToken,$nextOpenId);
- $result = json_decode($this->httpGet($this->getUserListUrl),true);
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function batchGetUserInfo($userList)
- {
- try
- {
- $this->getAccessToken();
- $this->batchGetUserInfoUrl = sprintf($this->batchGetUserInfoUrl,$this->accessToken);
- if(is_array($userList))foreach($userList as $openId)
- {
- $array['user_list'][] = array('openid'=>$openId,'lang'=>'zh-CN');
- }
- $result = $this->httpsRequest($this->batchGetUserInfoUrl, json_encode($array,JSON_UNESCAPED_UNICODE));
- $result = json_decode($result,true);
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function updateRemark($openId,$remark)
- {
- try
- {
- $this->getAccessToken();
- $this->updateRemarkUrl = sprintf($this->updateRemarkUrl,$this->accessToken);
- $array = array('openid'=>$openId,'remark'=>$remark);
- $result = $this->httpsRequest($this->updateRemarkUrl, json_encode($array,JSON_UNESCAPED_UNICODE));
- $result = json_decode($result,true);
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function createGroup($groupName)
- {
- try
- {
- $this->getAccessToken();
- $this->createGroupUrl = sprintf($this->createGroupUrl,$this->accessToken);
- $array['group'] = array('name'=>$groupName);
- $result = $this->httpsRequest($this->createGroupUrl, json_encode($array,JSON_UNESCAPED_UNICODE));
- $result = json_decode($result,true);
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function getGroups()
- {
- try
- {
- $this->getAccessToken();
- $this->getGroupsUrl = sprintf($this->getGroupsUrl,$this->accessToken);
- $result = $this->httpGet($this->getGroupsUrl);
- $result = json_decode($result,true);
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function getGroupId($openId)
- {
- try
- {
- $this->getAccessToken();
- $this->getGroupIdByOpenIdUrl = sprintf($this->getGroupIdByOpenIdUrl,$this->accessToken);
- $array['openid'] = $openId;
- $result = $this->httpsRequest($this->getGroupIdByOpenIdUrl, json_encode($array,JSON_UNESCAPED_UNICODE));
- $result = json_decode($result,true);
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function updateGroupName($groupId,$groupName)
- {
- try
- {
- $this->getAccessToken();
- $this->updateGroupNameUrl = sprintf($this->updateGroupNameUrl,$this->accessToken);
- $array['group'] = array('id'=>$groupId,'name'=>$groupName);
- $result = $this->httpsRequest($this->updateGroupNameUrl, json_encode($array,JSON_UNESCAPED_UNICODE));
- $result = json_decode($result,true);
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function changUserGroup($openId,$toGroupId)
- {
- try
- {
- $this->getAccessToken();
- if(is_array($openId))
- {
- $requestUrl = sprintf($this->batchChangeUserGroupUrl,$this->accessToken);
- $array= array('openid_list'=>$openId,'to_groupid'=>$toGroupId);
- }
- else
- {
- $requestUrl = sprintf($this->changeUserGroupUrl,$this->accessToken);
- $array= array('openid'=>$openId,'to_groupid'=>$toGroupId);
- }
- $result = $this->httpsRequest($requestUrl, json_encode($array,JSON_UNESCAPED_UNICODE));
- $result = json_decode($result,true);
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function deleteGroup($groupId)
- {
- try
- {
- $this->getAccessToken();
- $this->delGroupUrl = sprintf($this->delGroupUrl,$this->accessToken);
- $array['group'] = array('id'=>$groupId);
- $result = $this->httpsRequest($this->delGroupUrl, json_encode($array,JSON_UNESCAPED_UNICODE));
- $result = json_decode($result,true);
- return $result;
- }
- catch(Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function getUserInfo($open_id)
- {
- try
- {
- $this->getAccessToken();
- $this->getUserInfoUrl = sprintf($this->getUserInfoUrl,$this->accessToken,$open_id);
- $result = json_decode($this->httpGet($this->getUserInfoUrl),TRUE);
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function msgSend($contentInfo)
- {
- try
- {
- $this->getAccessToken();
- $msg =array();
- $sendUrl = sprintf($this->msgSendAllUrl,$this->accessToken);
- extract($contentInfo);
- if(is_numeric($groupId))
- {
- $msg['filter'] = array('is_to_all'=>false,'group_id'=>$groupId);
- }
- if(!empty($openIds)&&is_array($openIds))
- {
- $msg['touser'] = $openIds;
- $sendUrl = sprintf($this->msgSendUrl,$this->accessToken);
- }
- $msg['msgtype'] = $msgtype;
- if($msgtype=='mpnews')
- {
- $msg['mpnews'] = array('media_id'=>$media_id);
- }
- if($msgtype=='text')
- {
- $msg['text'] = array('content'=>$content);
- }
- if($msgtype=='voice')
- {
- $msg['voice'] = array('media_id'=>$media_id);
- }
- if($msgtype=='image')
- {
- $msg['image'] = array('media_id'=>$media_id);
- }
- if($msgtype=='mpvideo')
- {
- $msg['mpvideo'] = array('media_id'=>$media_id);
- }
- if($msgtype=='wxcard')
- {
- $msg['wxcard'] = array('card_id'=>$card_id);
- }
- $result = $this->httpsRequest($sendUrl, json_encode($msg,JSON_UNESCAPED_UNICODE));
- $result = json_decode($result,true);
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function msgPreview($contentInfo)
- {
- try
- {
- $this->getAccessToken();
- $msg =array();
- $this->msgPreviewUrl = sprintf($this->msgPreviewUrl,$this->accessToken);
- extract($contentInfo);
- if(!empty($towxname)) $msg['towxname'] = $towxname;
- if(!empty($touser)) $msg['touser'] = $touser;
- $msg['msgtype'] = $msgtype;
- if($msgtype=='mpnews')
- {
- $msg['mpnews'] = array('media_id'=>$media_id);
- }
- if($msgtype=='text')
- {
- $msg['text'] = array('content'=>$content);
- }
- if($msgtype=='voice')
- {
- $msg['voice'] = array('media_id'=>$media_id);
- }
- if($msgtype=='image')
- {
- $msg['image'] = array('media_id'=>$media_id);
- }
- if($msgtype=='mpvideo')
- {
- $msg['mpvideo'] = array('media_id'=>$media_id);
- }
- if($msgtype=='wxcard')
- {
- $msg['wxcard'] = array('card_id'=>$card_id,'card_ext'=>$card_ext);
- }
- $result = $this->httpsRequest($this->msgPreviewUrl, json_encode($msg,JSON_UNESCAPED_UNICODE));
- $result = json_decode($result,true);
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function delMsg($msgId)
- {
- $this->getAccessToken();
- $this->delMsgUrl = sprintf($this->delMsgUrl,$this->accessToken);
- $array = array('msg_id'=>$msgId);
- $result = $this->httpsRequest($this->delMsgUrl, json_encode($array,JSON_UNESCAPED_UNICODE));
- $result = json_decode($result,true);
- return $result;
- }
-
- public function msgStatus($msgId)
- {
- $this->getAccessToken();
- $this->getMsgStatusUrl = sprintf($this->getMsgStatusUrl,$this->accessToken);
- $array = array('msg_id'=>$msgId);
- $result = $this->httpsRequest($this->getMsgStatusUrl, json_encode($array,JSON_UNESCAPED_UNICODE));
- $result = json_decode($result,true);
- return $result;
- }
-
- public function sendCustomermsg($type,$openid,$data)
- {
- $this->getAccessToken();
- $this->customerMsgUrl = sprintf($this->customerMsgUrl,$this->accessToken);
- if($type=='text')
- {
- $msgcontent = '{ "touser" : "'.$openid.'",
- "msgtype" : "text",
- "text" : {
- "content" : "'.$data['content'].'"
- }
- }';
- }
- if($type=='image'||$type=='voice'||$type=='mpnews')
- {
- $msgcontent = '{ "touser" : "'.$openid.'",
- "msgtype" : "'.$type.'",
- "'.$type.'" : {
- "media_id" : "'.$data['media_id'].'"
- }
- }';
- }
- if($type=='news')
- {
- $msgcontent = '{"touser":"'.$openid.'",
- "msgtype":"news",
- "news":{
- "articles": [
- {
- "title":"'.$data['title'].'",
- "description":"'.$data['description'].'",
- "url":"'.$data['url'].'",
- "picurl":"'.getFileUrl($data['picurl']).'"
- }
- ]
- }
- }';
- }
- if($type=='video')
- {
- $msgcontent = '{
- "touser":"'.$openid.'",
- "msgtype":"video",
- "video":
- {
- "media_id":"'.$data['media_id'].'",
- "thumb_media_id":"'.$data['thumb_media_id'].'",
- "title":"'.$data['title'].'",
- "description":"'.$data['description'].'",
- }
- }';
- }
- $result = $this->httpsRequest($this->customerMsgUrl, $msgcontent);
- $result = json_decode($result,true);
- return $result;
- }
-
- public function responseMsg($array,$articles=array())
- {
- try
- {
- $receive = $this->receiveMsg();
- if (!empty($receive)){
- $fromUsername = $receive['fromUsername'];
- $toUsername = $receive['toUsername'];
- $time = TIMESTAMP;
- extract($array);
- if($type=='text')
- {
- $tpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[%s]]></MsgType>
- <Content><![CDATA[%s]]></Content>
- <FuncFlag>0</FuncFlag>
- </xml>";
- $resultStr = sprintf($tpl, $fromUsername, $toUsername, $time, $type, $content);
- }
- if($type=='image')
- {
- $tpl="<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[%s]]></MsgType>
- <Image>
- <MediaId><![CDATA[%s]]></MediaId>
- </Image>
- </xml>";
- $resultStr = sprintf($tpl, $fromUsername, $toUsername, $time, $type, $media_id);
- }
- if($type=='voice')
- {
- $tpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[%s]]></MsgType>
- <Voice>
- <MediaId><![CDATA[%s]]></MediaId>
- </Voice>
- </xml>";
- $resultStr = sprintf($tpl, $fromUsername, $toUsername, $time, $type, $media_id);
- }
- if($type=='video')
- {
- $tpl="<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[%s]]></MsgType>
- <Video>
- <MediaId><![CDATA[%s]]></MediaId>
- <Title><![CDATA[%s]]></Title>
- <Description><![CDATA[%s]]></Description>
- </Video>
- </xml>";
- $resultStr = sprintf($tpl, $fromUsername, $toUsername, $time, $type, $media_id,$title,$description);
- }
- if($type=='news')
- {
- $tpl="<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[%s]]></MsgType>
- <ArticleCount>%s</ArticleCount>";
- $tpl.="<Articles>";
- if(!empty($articles))foreach($articles as $article){
- $tpl.="<item>
- <Title><![CDATA[".$article['title']."]]></Title>
- <Description><![CDATA[".$article['description']."]]></Description>
- <PicUrl><![CDATA[".$article['picurl']."]]></PicUrl>
- <Url><![CDATA[".$article['url']."]]></Url>
- </item>";
- }
- $tpl.="</Articles>";
- $tpl.="</xml>";
- $resultStr = sprintf($tpl, $fromUsername, $toUsername, $time, $type, $count);
- }
- echo $resultStr;
- }else {
- echo "";
- exit;
- }
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function receiveMsg()
- {
- try
- {
-
- $postStr = file_get_contents("php://input");
- file_put_contents(BASE_PATH.'components/WeiXin/test.txt',$postStr);
- if (!empty($postStr)){
- $msg = array();
-
- libxml_disable_entity_loader(true);
- $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
-
- $fromUsername = $postObj->FromUserName;
- $this->getUserInfo($fromUsername);
- $this->currentOpenId = $fromUsername;
- $toUsername = $postObj->ToUserName;
- $msgType = $postObj->MsgType;
- $event = $postObj->Event;
- $msgID = $postObj->MsgID;
- $createTime = $postObj->CreateTime;
- if($msgType=='text')
- {
- $msg['content'] = $postObj->Content;
- }
- if($msgType=='image')
- {
- $msg['picUrl'] = $postObj->PicUrl;
- $msg['mediaId'] = $postObj->MediaId;
- }
- if($msgType=='voice')
- {
- $msg['format'] = $postObj->Format;
- $msg['mediaId'] = $postObj->MediaId;
- }
- if($msgType=='video')
- {
- $msg['thumbMediaId'] = $postObj->ThumbMediaId;
- $msg['mediaId'] = $postObj->MediaId;
- }
- if($msgType=='shortvideo')
- {
- $msg['thumbMediaId'] = $postObj->ThumbMediaId;
- $msg['mediaId'] = $postObj->MediaId;
- }
- if($msgType=='event')
- {
- if($postObj->TotalCount)
- {
- }
- if($event=='LOCATION')
- {
- $msg['latitude'] = $postObj->Latitude;
- $msg['longitude'] = $postObj->Longitude;
- $msg['precision'] = $postObj->Precision;
- }
- if($event=='subscribe')
- {
- $postObj->EventKey&&$msg['eventKey'] = $postObj->EventKey;
- $postObj->Ticket&&$msg['ticket'] = $postObj->Ticket;
- }
- if($event=='SCAN')
- {
- $postObj->EventKey&&$msg['eventKey'] = $postObj->EventKey;
- $postObj->Ticket&&$msg['ticket'] = $postObj->Ticket;
- }
- if($event=='unsubscribe'){}
- if($event=='click')
- {
- $postObj->EventKey&&$msg['eventKey'] = $postObj->EventKey;
- }
- if($event=='VIEW')
- {
- $postObj->EventKey&&$msg['eventKey'] = $postObj->EventKey;
- }
- if($event=='scancode_push')
- {
- $msg['eventKey'] = $postObj->EventKey;
- $msg['ScanType'] = $postObj->ScanCodeInfo->ScanType;
- $msg['ScanResult'] = $postObj->ScanCodeInfo->ScanResult;
- }
- if($event=='scancode_waitmsg')
- {
- $msg['eventKey'] = $postObj->EventKey;
- $msg['ScanType'] = $postObj->ScanCodeInfo->ScanType;
- $msg['ScanResult'] = $postObj->ScanCodeInfo->ScanResult;
- }
- if($event=='pic_sysphoto')
- {
- $msg['eventKey'] = $postObj->EventKey;
- $msg['Count'] = $postObj->SendPicsInfo->Count;
- $msg['PicList'] = $postObj->SendPicsInfo->PicList;
- }
- if($event=='pic_photo_or_album')
- {
- $msg['eventKey'] = $postObj->EventKey;
- $msg['Count'] = $postObj->SendPicsInfo->Count;
- $msg['PicList'] = $postObj->SendPicsInfo->PicList;
- }
- if($event=='pic_weixin')
- {
- $msg['eventKey'] = $postObj->EventKey;
- $msg['Count'] = $postObj->SendPicsInfo->Count;
- $msg['PicList'] = $postObj->SendPicsInfo->PicList;
- }
- if($event=='location_select')
- {
- $msg['eventKey'] = $postObj->EventKey;
- $msg['Location_X'] = $postObj->SendLocationInfo->Location_X;
- $msg['Location_Y'] = $postObj->SendLocationInfo->Location_Y;
- $msg['Scale'] = $postObj->SendLocationInfo->Scale;
- $msg['Label'] = $postObj->SendLocationInfo->Label;
- $msg['Poiname'] = $postObj->SendLocationInfo->Poiname;
- }
- }
- if($msgType=='link')
- {
- $msg['title'] = $postObj->Title;
- $msg['description'] = $postObj->Description;
- $msg['url'] = $postObj->Url;
- }
- $msg['fromUsername']=$fromUsername;
- $msg['toUsername']=$toUsername;
- $msg['msgType']=$msgType;
- $msg['event']=$event;
- $msg['msgID']=$msgID;
- $msg['createTime']=$createTime;
- return $msg;
- }
- else
- {
- return array();
- }
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function addMaterial($type,$filepath,$title='',$introduction='')
- {
- try
- {
- $this->getAccessToken();
- $ext = $this->fileext($filepath);
- $filesize = filesize($filepath);
- $voiceFatmat = array('mp3','wma','wav','amr');
- $voiceMaxSize = 5;
- $voiceTimeLenth = "60秒";
- $imageFatmat = array('bmp','png','jpeg','jpg','gif');
- $imageMaxSize = 2;
- $newsImageFatmat = array('jpg','png');
- $newsImageMaxSize = 1;
- if($type=='voice')
- {
- if(!in_array($ext,$voiceFatmat))$this->showErrorMessage('不支持该语音格式');
- if($filesize>$voiceMaxSize*1014*1024)$this->showErrorMessage('语音超出大小');
- }
- if($type=='image')
- {
- if(!in_array($ext,$imageFatmat))$this->showErrorMessage('不支持该图片格式');
- if($filesize>$imageMaxSize*1014*1024)$this->showErrorMessage('图片超出大小');
- }
- if($type=='video'||$type=='newsvideo')
- {
- $description='{"title":"'.$title.'","introduction":"'.$introduction.'"}';
- if(!empty($description))$data['description'] = $description;
- }
- if($type=='newsimage')
- {
- if(!in_array($ext,$newsImageFatmat))$this->showErrorMessage('不支持该图片格式');
- if($filesize>$newsImageMaxSize*1014*1024)$this->showErrorMessage('图片超出大小');
- $this->addMaterialUrl = sprintf($this->uploadImgUrl,$this->accessToken);
- }
- else
- {
- $this->addMaterialUrl = sprintf($this->addMaterialUrl,$this->accessToken);
- }
- $data['media'] = new \CURLFile(realpath($filepath));
- $result = $this->httpsRequest($this->addMaterialUrl, $data);
- $result = json_decode($result,true);
- if($type=='newsvideo')
- {
- $this->uploadNewsVideoUrl = sprintf($this->uploadNewsVideoUrl,$this->accessToken);
- $array = array('media_id'=>$result['media_id'],'title'=>$title,'description'=>$description);
- $result = $this->httpsRequest($this->uploadNewsVideoUrl, json_encode($array,JSON_UNESCAPED_UNICODE));
- $result = json_decode($result,true);
- }
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function addNews($articles)
- {
- try
- {
- $this->getAccessToken();
- $this->addNewsUrl = sprintf($this->addNewsUrl,$this->accessToken);
- $result = $this->httpsRequest($this->addNewsUrl, json_encode($articles,JSON_UNESCAPED_UNICODE));
- $result = json_decode($result,true);
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function updateNews($media_id,$data,$index=0)
- {
- try
- {
- $this->getAccessToken();
- $this->updateNewsUrl = sprintf($this->updateNewsUrl,$this->accessToken);
- $data['media_id'] = $media_id;
- $data['index'] = $index;
- $result = $this->httpsRequest($this->updateNewsUrl, json_encode($data,JSON_UNESCAPED_UNICODE));
- $result = json_decode($result,true);
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function getMedia($media_id)
- {
- try
- {
- $this->getAccessToken();
- $this->getMediaUrl = sprintf($this->getMediaUrl,$this->accessToken);
- $result = json_decode($this->httpsRequest($this->getMediaUrl,json_encode(array('media_id'=>$media_id))),TRUE);
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function delMedia($media_id)
- {
- try
- {
- $this->getAccessToken();
- $this->delMediaUrl = sprintf($this->delMediaUrl,$this->accessToken);
- $result = json_decode($this->httpsRequest($this->delMediaUrl,json_encode(array('media_id'=>$media_id))),TRUE);
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
-
- public function getAccessToken($type=1)
- {
- try
- {
- if(file_exists($this->acessTokenFile))
- {
- $data = json_decode(file_get_contents($this->acessTokenFile));
- if($data->expire_time<TIMESTAMP)
- {
- $refresh = true;
- }
- else
- {
- $this->accessToken = $data->access_token;
- }
- }
- else
- {
- $refresh = true;
- }
- if($refresh)
- {
- $data = json_decode($this->httpGet($this->getAccessTokenUrl));
- $data->expire_time = TIMESTAMP+3600;
- file_put_contents($this->acessTokenFile,json_encode($data));
- $this->accessToken = $data->access_token;
- }
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function getJsApiTicket($type=1,$url='')
- {
- try
- {
- if(file_exists($this->jsApiTicketFile))
- {
- $data = json_decode(file_get_contents($this->jsApiTicketFile));
- if($data->expire_time<TIMESTAMP)
- {
- $refresh = true;
- }
- else
- {
- $ticket = $data->ticket;
- }
- }
- else
- {
- $refresh = true;
- }
- if($refresh)
- {
- $this->getAccessToken();
- $this->getJsApiTicketUrl = sprintf($this->getJsApiTicketUrl,$this->accessToken);
- $data = json_decode($this->httpGet($this->getJsApiTicketUrl));
- $data->expire_time = TIMESTAMP+7000;
- file_put_contents($this->jsApiTicketFile,json_encode($data));
- $ticket = $data->ticket;
- }
- $timestamp = TIMESTAMP;
- $noncestr = $this->createNonceStr();
- if(empty($url))$url = $this->getUrl();
- $string = "jsapi_ticket=".$ticket."&noncestr=".$noncestr."×tamp=".$timestamp."&url=".$url;
- $signature = sha1($string);
- return array('appid'=>$this->appId,'ticket'=>$ticket,'noncestr'=>$noncestr,'timestamp'=>$timestamp,'url'=>$url,'signature'=>$signature);
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function getWeiXinIpList()
- {
- try
- {
- $this->getIpListUrl = sprintf($this->getIpListUrl,$this->accessToken);
- $result = $this->httpGet($this->getIpListUrl);
- $result = json_decode($result,TRUE);
- $this->wxIpList = $result['ip_list'];
- return $this->wxIpList;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function getMaterialCount()
- {
- try
- {
- $this->getAccessToken();
- $this->getMaterialCountUrl = sprintf($this->getMaterialCountUrl,$this->accessToken);
- return $result = json_decode($this->httpGet($this->getMaterialCountUrl),TRUE);
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function batcheGetMaterial($type,$offset,$count)
- {
- try
- {
- $this->getAccessToken();
- $this->batcheGetMaterialUrl = sprintf($this->batcheGetMaterialUrl,$this->accessToken);
- $result = json_decode($this->httpsRequest($this->batcheGetMaterialUrl,json_encode(array('type'=>$type,'offset'=>$offset,'count'=>$count))),TRUE);
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- public function sendTemplateMsg($openid,$templateid,$data,$url=''){
-
- try
- {
- $this->getAccessToken();
- $this->tplMsgUrl = sprintf($this->tplMsgUrl,$this->accessToken);
- $arr = array(
- 'touser'=>$openid ,
- 'template_id'=>$templateid,
- 'url'=>$url,
- 'data'=>$data
- ) ;
- $result = json_decode($this->httpsRequest($this->tplMsgUrl,json_encode($arr)),TRUE);
- return $result;
- }
- catch(\Exception $e)
- {
- $this->showError($e->getMessage());
- }
- }
-
- function sendRedPack($parameter)
- {
- $money = max($parameter['money'], 0.01);
- $money = intval($money * 100);
- $mch_billno = $this->mchId.date('YmdHis',$this->timestamp).$this->num_rand(4);
- $ip = $parameter['ip'];
- $openid = $parameter['openid'];
- if (!$openid) {
- $this->echo_json('openid获取失败');
- }
- $this->parameters = array();
- $this->setParameter('nonce_str', $this->createNonceStr());
- $this->setParameter('mch_billno', $mch_billno);
- $this->setParameter('mch_id', $this->mchId);
- $this->setParameter('wxappid', $this->appId);
- $this->setParameter('nick_name', $parameter['nick_name']);
- $this->setParameter('send_name', $parameter['send_name']);
- $this->setParameter('re_openid', $openid);
- $this->setParameter('total_amount', $money);
- $this->setParameter('min_value', $money);
- $this->setParameter('max_value', $money);
- $this->setParameter('total_num', 1);
- $this->setParameter('wishing', $parameter['wishing']);
- $this->setParameter('client_ip', $ip);
- $this->setParameter('act_name', $parameter['act_name']);
- $this->setParameter('remark', $parameter['remark']);
- $this->setParameter('sign', $this->getSign());
- $xml = $this->arrayToXml($this->parameters);
- $res = $this->wxHttpsRequestPem($this->redPackUrl, $xml);
- $resObj = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);
- if ($resObj->return_code != 'SUCCESS') {
- return array('code'=>-1, 'msg'=>$resObj->return_msg);
- }
- if ($resObj->result_code != 'SUCCESS') {
- return array('code'=>-1, 'msg'=>$resObj->err_code_des);
- }
- return array('code'=>1, 'order_no'=>$mch_billno, 'openid'=>$openid);
- }
-
- public function valid()
- {
- $echoStr = $_GET["echostr"];
- if($this->checkSignature()){
- echo $echoStr;
- exit;
- }
- }
- public function checkSignature()
- {
- if (empty($this->token)) {
- $this->showError('错误token!');
- }
- $signature = $_GET["signature"];
- $timestamp = $_GET["timestamp"];
- $nonce = $_GET["nonce"];
- $tmpArr = array($this->token, $timestamp, $nonce);
-
- sort($tmpArr, SORT_STRING);
- $tmpStr = implode( $tmpArr );
- $tmpStr = sha1( $tmpStr );
- if( $tmpStr == $signature ){
- return true;
- }else{
- return false;
- }
- }
-
- public function setParameter($parameter, $parameterValue)
- {
- $this->parameters[trim($parameter)] = trim($parameterValue);
- }
-
- public function getParameter($parameter)
- {
- return $this->parameters[$parameter];
- }
-
- function num_rand($length) {
- mt_srand((double) microtime() * 1000000);
- $randVal = mt_rand(1, 9);
- for ($i = 1; $i < $length; $i++) {
- $randVal .= mt_rand(0, 9);
- }
- return $randVal;
- }
- function echo_json_callback($results) {
- if ($_GET['callback']=='') $_GET['callback']='wzcallback';
- if (is_string($results)) $results=array('code'=>-1,'msg'=>$results);
- header("Content-type: text/html; charset=utf-8");
- header("Cache-Control: no-cache");
- echo $_GET['callback']."(".json_encode($results).")";
- exit;
- }
- function echo_json($arr=array(), $code = -1){
- $arr = is_array($arr)?$arr:array("msg"=>$arr, 'code'=>$code);
- header('Content-type: application/json');
- echo json_encode($arr);exit;
- }
-
- public function showError($msg)
- {
- throw new \Exception($msg);
- }
-
- public function showErrorMessage($msg)
- {
- echo $msg;
- exit;
- }
- private function httpGet($url) {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_TIMEOUT, 500);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_URL, $url);
- $res = curl_exec($curl);
- curl_close($curl);
- return $res;
- }
-
- public function wxHttpsRequestPem($url, $vars, $second=30,$aHeader=array()){
- $ch = curl_init();
-
- curl_setopt($ch,CURLOPT_TIMEOUT,$second);
- curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
-
-
-
- curl_setopt($ch,CURLOPT_URL,$url);
- curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
- curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
-
-
-
- curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
- curl_setopt($ch,CURLOPT_SSLCERT,$this->basePath.'cert/apiclient_cert.pem');
-
- curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
- curl_setopt($ch,CURLOPT_SSLKEY,$this->basePath.'cert/apiclient_key.pem');
- curl_setopt($ch,CURLOPT_CAINFO,'PEM');
- curl_setopt($ch,CURLOPT_CAINFO,$this->basePath.'cert/rootca.pem');
-
-
- if( count($aHeader) >= 1 ){
- curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
- }
- curl_setopt($ch,CURLOPT_POST, 1);
- curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
- $data = curl_exec($ch);
- if($data){
- curl_close($ch);
- return $data;
- }
- else {
- $error = curl_errno($ch);
- echo "call faild, errorCode:$error\n";
- curl_close($ch);
- return false;
- }
- }
- private function httpsRequest($url, $data = null)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
- if (!empty($data)){
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- }
- curl_setopt($curl, CURLOPT_TIMEOUT, 3000);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($curl);
- curl_close($curl);
- return $output;
- }
- private function fileext($filepath)
- {
- return strtolower(trim(substr(strrchr($filepath, '.'), 1, 10)));
- }
-
- private function getUrl() {
- $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
- $php_self = $_SERVER['PHP_SELF'] ? $this->safe_replace($_SERVER['PHP_SELF']) : $this->safe_replace($_SERVER['SCRIPT_NAME']);
- $path_info = isset($_SERVER['PATH_INFO']) ? $this->safe_replace($_SERVER['PATH_INFO']) : '';
- $relate_url = isset($_SERVER['REQUEST_URI']) ? $this->safe_replace($_SERVER['REQUEST_URI']) : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$this->safe_replace($_SERVER['QUERY_STRING']) : $path_info);
- return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
- }
-
- function safe_replace($string)
- {
- $string = str_replace('%20','',$string);
- $string = str_replace('%27','',$string);
- $string = str_replace('%2527','',$string);
- $string = str_replace('*','',$string);
- $string = str_replace('"','"',$string);
- $string = str_replace("'",'',$string);
- $string = str_replace('"','',$string);
- $string = str_replace(';','',$string);
- $string = str_replace('>','>',$string);
- $string = str_replace('<','<',$string);
- $string = str_replace('{','',$string);
- $string = str_replace('}','',$string);
- $string = str_replace('\\','',$string);
- return $string;
- }
-
- public function createNonceStr($length = 16) {
- $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- $str = "";
- for ($i = 0; $i < $length; $i++) {
- $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
- }
- return $str;
- }
-
- public function arrayToXml($arr)
- {
- $xml = "<xml>";
- foreach ($arr as $key=>$val) {
- $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
- }
- $xml.="</xml>";
- return $xml;
- }
-
- public function getSign($arr)
- {
- $arr = $arr ? $arr : $this->parameters;
-
- unset($arr['sign']);
- ksort($arr);
- $str = '';
- foreach ($arr as $key => $val) {
- if ($val != null) {
-
- $str .= $key . '=' . $val . '&';
- }
- }
-
- $str .= 'key='.$this->partnerKey;
- return strtoupper(md5($str));
- }
- }
|