微信开发生成二维码 接收回复消息

来源:互联网 发布:加内特身体数据 编辑:程序博客网 时间:2024/06/08 15:06
<?phpclass WeChat{    private $appID;    private $appSecret;    public function __construct($appID, $appSecret)    {        $this->appID = $appID;        $this->appSecret = $appSecret;    }    public function request($curl, $method = "GET", $data = null)    {        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $curl);        curl_setopt($ch, CURLOPT_HEADER, false);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        if ($method == "POST") {            curl_setopt($ch, CURLOPT_POST, true);            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);        }        $content = curl_exec($ch);        curl_close($ch);        return $content;    }    public function getAccessToken()    {        $tokenFile = "accessToken";        if (file_exists($tokenFile)) {            $content = file_get_contents($tokenFile);            $content = json_decode($content);            if (time() - filemtime($tokenFile) < $content->expires_in) {                return $content->access_token;            }        }        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appID&secret=$this->appSecret";        $content = $this->request($url);        file_put_contents($tokenFile, $content);        $content = json_decode($content);        return $content->access_token;    }    public function getTicket($sceneID, $temp = true, $expireSeconds = 604800)    {        if ($temp) {            $data = "{\"expire_seconds\": $expireSeconds, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": $sceneID}}}";        } else {            $data = "{\"action_name\": \"QR_LIMIT_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": $sceneID}}}";        }        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->getAccessToken();        $content = $this->request($url, "POST", $data);        $content = json_decode($content);        return $content->ticket;    }    public function showQRCode($sceneID, $temp = true, $expireSeconds = 604800)    {        $url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$this->getTicket($sceneID, $temp, $expireSeconds);        $content = $this->request($url);        header("content-type:image/jpeg");        echo $content;    }    public function valid()    {        $echoStr = $_GET["echostr"];        //valid signature , option        if($this->checkSignature()){            echo $echoStr;            exit;        }    }    public function responseMsg()    {        //get post data, May be due to the different environments        $postStr = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");        //extract post data        if (!empty($postStr)){            /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,               the best way is to check the validity of xml by yourself */            libxml_disable_entity_loader(true);            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);            $fromUsername = $postObj->FromUserName;            $toUsername = $postObj->ToUserName;            $keyword = trim($postObj->Content);            $time = time();            $textTpl = "<xml>                     <ToUserName><![CDATA[%s]]></ToUserName>                     <FromUserName><![CDATA[%s]]></FromUserName>                     <CreateTime>%s</CreateTime>                     <MsgType><![CDATA[%s]]></MsgType>                     <Content><![CDATA[%s]]></Content>                     <FuncFlag>0</FuncFlag>                     </xml>";            if(!empty( $keyword ))            {                $msgType = "text";                $contentStr = "Welcome to wechat world!";                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);                echo $resultStr;            }else{                echo "Input something...";            }        }else {            echo "";            exit;        }    }    private function checkSignature()    {        // you must define TOKEN by yourself        if (!defined("TOKEN")) {            throw new Exception('TOKEN is not defined!');        }        $signature = $_GET["signature"];        $timestamp = $_GET["timestamp"];        $nonce = $_GET["nonce"];        $token = TOKEN;        $tmpArr = array($token, $timestamp, $nonce);        // use SORT_STRING rule        sort($tmpArr, SORT_STRING);        $tmpStr = implode( $tmpArr );        $tmpStr = sha1( $tmpStr );        if( $tmpStr == $signature ){            return true;        }else{            return false;        }    }}?>
0 0
原创粉丝点击