private $_msg_template = array(
'text' => '<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content></xml>',
'image' => '<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[%s]]></MediaId></Image></xml>',
'music' => '<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[music]]></MsgType><Music><Title><![CDATA[%s]]></Title><Description><![CDATA[%s]]></Description><MusicUrl><![CDATA[%s]]></MusicUrl><HQMusicUrl><![CDATA[%s]]></HQMusicUrl><ThumbMediaId><![CDATA[%s]]></ThumbMediaId></Music></xml>',
'news' => '<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>%s</ArticleCount><Articles>%s</Articles></xml>',
'news_item' => '<item><Title><![CDATA[%s]]></Title><Description><![CDATA[%s]]></Description><PicUrl><![CDATA[%s]]></PicUrl><Url><![CDATA[%s]]></Url></item>',
);
public function index(Request $request)
{
$this->respomseMsg();
$this->CuStOmIzEmEnUs();
$this->valid();
}
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if($tmpStr == $signature){
return true;
}else{
return false;
}
}
public function respomseMsg(){
$xml_str = file_get_contents('php://input');
if(empty($xml_str)){
die('');
}
if(!empty($xml_str)){
libxml_disable_entity_loader(true);
$request_xml = simplexml_load_string($xml_str, 'SimpleXMLElement', LIBXML_NOCDATA);
switch ($request_xml->MsgType){
case 'event':
$event = $request_xml->Event;
if ($event=='subscribe') {
$this->_doSubscribe($request_xml);
}elseif ($event=='CLICK') {
$this->_doClick($request_xml);
}elseif ($event=='VIEW') {
$this->_doView($request_xml);
}else{
}
break;
case 'text':
$this->_doText($request_xml);
break;
case 'image':
$this->_doImage($request_xml);
break;
case 'voice':
$this->_doVoice($request_xml);
break;
case 'video':
$this->_doVideo($request_xml);
break;
case 'shortvideo':
$this->_doShortvideo($request_xml);
break;
case 'location':
$this->_doLocation($request_xml);
break;
case 'link':
$this->_doLink($request_xml);
break;
}
}
}
private function _doSubscribe($request_xml){
$item_list = [
[
"title" => "title","desc" => "desc","picurl" =>"picurl","url" => "url"
],[
"title" => "title","desc" => "desc","picurl" =>"picurl","url" => "url"
],[
"title" => "title","desc" => "desc","picurl" =>"picurl","url" => "url"
]
];
$this->_msgNews($request_xml->FromUserName,$request_xml->ToUserName,$item_list);
}
private function _msgNews($to,$from,$item_list=array()){
$item_str = '';
foreach ($item_list as $item) {
$item_str .= sprintf($this->_msg_template['news_item'],$item['title'],$item['desc'],$item['picurl'],$item['url']);
}
$response = sprintf($this->_msg_template['news'], $to, $from, time(), count($item_list), $item_str);
die($response);
}
public function CuStOmIzEmEnUs(){
if(!Session::has('key')){
$this->get_access_token();
}
$url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.Session::get('key');
$data = '{
"button":[
{
"type":"view",
"name":"",
"url":""
},
{
"type":"view",
"name":"",
"url":""
},
{
"name":"谁看过我",
"sub_button":[
{
"type":"view",
"name":"",
"url":"url"
},
{
"type":"click",
"name":"",
"key":""
}]
}]
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$curl = curl_exec($ch);
curl_close($ch);
die(json_decode($curl));
}