load->model('admin/Weixin_model'); } public function response() { if (!isset($_GET['echostr'])) { $this->responseMsg(); }else{ $this->valid(); } } public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } public function getCode() { $code = $_GET['code']; $uinfo=file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx6b7982f5a2a2d161&secret=c33fc8c2c5658ad76a04afe1fa1808dd&code={$code}&grant_type=authorization_code"); $uinfo=(array)json_decode($uinfo); $openid=$uinfo['openid']; echo "OPENID IS: ".$openid; } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = $this->_TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } public function responseMsg() { //$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; $postStr = file_get_contents("php://input"); if (!empty($postStr)) { /*$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = " %s 0 "; if($keyword == "?" || $keyword == "?") { $msgType = "text"; $contentStr = $postObj->FromUserName; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }*/ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch ($RX_TYPE) { case "text": $resultStr = $this->receiveText($postObj); break; case "event": $resultStr = $this->receiveEvent($postObj); break; default: $resultStr = ""; break; } echo $resultStr; }else { echo ""; exit; } } private function receiveText($object) { $funcFlag = 0; $contentStr = "你发送的内容为:".$object->Content; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; } private function receiveEvent($object) { $content = ""; switch ($object->Event) { case "CLICK": switch ($object->EventKey) { case "haoya": $content[] = array("Title"=>"OpenID", "Description"=>"你的OpenID为:".$object->FromUserName, "PicUrl"=>"", "Url" =>"www.163.com"); //header('Location: http://www.163.com/'); break; } break; } if(is_array($content)){ $result = $this->transmitNews($object, $content); }else{ $result = $this->transmitText($object, $content); } return $result; } private function transmitText($object, $content, $funcFlag = 0) { $textTpl = " %s %d "; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $funcFlag); return $resultStr; } private function transmitNews($object, $arr_item, $funcFlag = 0) { //首条标题28字,其他标题39字 if(!is_array($arr_item)) return; $itemTpl = " <![CDATA[%s]]> "; $item_str = ""; foreach ($arr_item as $item) $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']); $newsTpl = " %s %s $item_str %s "; $resultStr = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item), $funcFlag); return $resultStr; } }