微信第三天,聊天机器人

index.php

header('Content-type:text');
define("APPID","");
define("APPSECRET","");
define("TOKEN","weixin");
require("./wechat.inc.php");
$wechat=new WeChat(APPID,APPSECRET,TOKEN);
$wechat->responseMsg();

wechat.inc.php

class WeChat{
    private $_appid;
    private $_appsecret;
    private $_token;
    
    
    //回复信息
    public function responseMsg()
    {
    

        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fromUsername = $postObj->FromUserName;
            $toUsername = $postObj->ToUserName;
            $keyword = trim($postObj->Content);
            $time = time();            
            
            $file="./xxx.log";
file_put_contents($file,Urldecode($postObj));
            $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( strtolower($postObj->Event) == 'scan'){
$msgType = "text";
$contentStr = "扫码".$postObj->EventKey;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
exit();
}

$data="txt=".$keyword;
$contentStr=$this->_request("http://www.niurenqushi.com/api/simsimi/",false,"post",$data);
$contentStr=json_decode($contentStr);
$contentStr=$contentStr->text;

$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
            
        }
    }
    
    public function __construct($appid,$appsecret,$token)
    {
        $this->_appid=$appid;
        $this->_appsecret=$appsecret;
        $this->_token=$token;
    }
    private function _request($curl,$https=true,$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($https)
    {
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);//不做服务器认证
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);//不做客户端认证
    }

    if($method=='post')
    {
        curl_setopt($ch, CURLOPT_POST,true);//设置请求是post方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置post请求数据
        
    }

    $str=curl_exec($ch);//执行访问
    curl_close($ch);//关闭curl,释放资源
    return $str;    
    }


    private function _getAccesstoken()
    {
        $file="./accesstoken";
        if(file_exists($file))
        {
            $content=file_get_contents($file);
            $content=json_decode($content);
            if(time()-filemtime($file)<$content->expires_in)
            {
                return $content->access_token;
            }
        }
        $content=_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->_appid."&secret=".$this->_appsecret);
        file_put_contents($file,$content);
        $content=json_decode($content);
        return $content->access_token;    
    }
    
    
    /*获取ticket
expire_seconds:二维码有效期(秒)
type:二维码类型(临时或永久)
scene:场景编号
*/
    public function _getTicket($expire_seconds=604800,$type="temp",$scene=1)
    {        
        if($type=="temp"){
        $data='{"expire_seconds": '.$expire_seconds.', "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$scene.'}}}';
        return $this->_request("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->_getAccesstoken(),true,"post",$data);
        }else
        {
        $data='{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$scene.'}}}';
        return $this->_request("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->_getAccesstoken(),true,"post",$data);    
        }    
    }
/* 通过ticket获取二维码 */
public function _getQRCode($expire_seconds=604800,$type="temp",$scene=1)
{
$content=json_decode($this->_getTicket($expire_seconds,$type,$scene));
$ticket=$content->ticket;
$image=$this->_request("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket));
//$file="./".$type.$scene.".jpg"; //设置图片名字
//file_put_contents($file,$content); //二维码保存到本地
return $image;

}



}

 

posted @ 2017-12-02 19:21  自由无风  阅读(316)  评论(0编辑  收藏  举报