您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP微信开发接口消息代码解析

2024/2/18 4:00:33发布27次查看
一、写好接口程序 在你的服务器上上传好一个接口程序文件 内容如下: ?php define(token,weixin);//自己定义的token就是个通信的私钥 $wechatobj=newwechatcallbackapitest(); $wechatobj-valid(); //$wechatobj-responsemsg(); classwechatcallbackapitest {
一、写好接口程序
在你的服务器上上传好一个接口程序文件  内容如下:
valid();
//$wechatobj->responsemsg();
class wechatcallbackapitest
{
    public function valid()
    {
        $echostr = $_get[echostr];
        if($this->checksignature()){
            echo $echostr;
            exit;
        }
    }
    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();
            $texttpl =
%s
0
            ;
            if(!empty( $keyword ))
            {
                $msgtype = text;
                $contentstr = '你好啊.;
                $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
                echo $resultstr;
            }else{
                echo '说说话吧';
            }
        }else {
            echo '说说话吧';
            exit;
        }
    }
    private function checksignature()
    {
        $signature = $_get[signature];
        $timestamp = $_get[timestamp];
        $nonce = $_get[nonce];
        $token =token;
        $tmparr = array($token, $timestamp, $nonce);
        sort($tmparr);
        $tmpstr = implode( $tmparr );
        $tmpstr = sha1( $tmpstr );
        if( $tmpstr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}
?>
二、配置微信公众平台回复接口
设置回复接口,填好url和token(token必须跟上面程序里面定义的token一致)
三、验证接口
用自己的个人微信关注下你的公众账号,给这个账号发一条消息过去,收到原样的消息返回,即验证成功了。
四、开始自定义回复
注释掉$wechatobj->valid(); 这行,同时去掉//$wechatobj->responsemsg();这行的注释。
你可以修改responsemsg函数里面的代码,根据用户的消息类型('text','image','location')和消息内容来回复用户不同的内容。
消息接口就可以使用了,发个消息试试看吧?
封装weixin.class.php代码如下:
token = $token;        $this->debug = $debug;    }
 //获得用户发过来的消息(消息内容和消息类型  )    public function getmsg()    {        $poststr = $globals[http_raw_post_data];        if ($this->debug) {                        $this->write_log($poststr);        }        if (!empty($poststr)) {            $this->msg = (array)simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);            $this->msgtype = strtolower($this->msg['msgtype']);        }    }
 //回复文本消息    public function maketext($text='')    {        $createtime = time();        $funcflag = $this->setflag ? 1 : 0;        $texttpl =             msg['fromusername']}]]>            msg['tousername']}]]>            {$createtime}                                    %s            ;        return sprintf($texttpl,$text,$funcflag);    }
 //根据数组参数回复图文消息    public function makenews($newsdata=array())    {        $createtime = time();        $funcflag = $this->setflag ? 1 : 0;        $newtplheader =             msg['fromusername']}]]>            msg['tousername']}]]>            {$createtime}                                    %s;        $newtplitem =                                                             ;        $newtplfoot =             %s            ;        $content = '';        $itemscount = count($newsdata['items']);        $itemscount = $itemscount  $item) {                if ($keydebug) {                    $this->write_log($data);        }        echo $data;    }    public function valid()    {        if ($this->checksignature()) {            if( $_server['request_method']=='get' )            {                echo $_get['echostr'];                exit;            }        }else{            write_log('认证失败');            exit;        }    }    private function checksignature()    {        $signature = $_get[signature];        $timestamp = $_get[timestamp];        $nonce = $_get[nonce];        $tmparr = array($this->token, $timestamp, $nonce);        sort($tmparr);        $tmpstr = implode( $tmparr );        $tmpstr = sha1( $tmpstr );        if( $tmpstr == $signature ){            return true;        }else{            return false;        }    }    private function write_log($log){
//这里是你记录调试信息的地方  请自行完善   以便中间调试
}}?>
调用weixin.class.php
把你的微信公众平台主接口文件(如前面定义的http://www.yourdomain.com/weixin.php)中,修改代码为:
getmsg();$type = $weixin->msgtype;//消息类型$username = $weixin->msg['fromusername'];//哪个用户给你发的消息,这个$username是微信加密之后的,但是每个用户都是一一对应的if ($type==='text') {    if ($weixin->msg['content']=='hello2bizuser') {//微信用户第一次关注你的账号的时候,你的公众账号就会受到一条内容为'hello2bizuser'的消息        $reply = $weixin->maketext('欢迎你关注微市场微信导航哦');    }else{//这里就是用户输入了文本信息        $keyword = $weixin->msg['content'];   //用户的文本消息内容                include_once(chaxun.php);//文本消息 调用查询程序                  $chaxun= new chaxun(debug,$keyword,$username);                $results['items'] =$chaxun->search();//查询的代码                $reply = $weixin->makenews($results);    }}elseif ($type==='location') {      //用户发送的是位置信息  稍后的文章中会处理                  }elseif ($type==='image') {      //用户发送的是图片 稍后的文章中会处理}elseif ($type==='voice') {           //用户发送的是声音 稍后的文章中会处理}$weixin->reply($reply);
?>
查询代码
还需要将数据库里面的查询结果格式化为特定的形式
search($this->keyword);//普通的根据关键词查询数据库的操作  代码就不用分享了if(is_array($list)&&!empty($list)){                                foreach($list as $msg){ $record[]=array(//以下代码,将数据库中查询返回的数组格式化为微信返回消息能接收的数组形式,即title、description、picurl、url 详见微信官方的文档描述                    'title' =>$msg['title'],                    'description' =>$msg['discription'],                    'picurl' => $msg['pic_url'],                    'url' =>$msg['url']                );}}return $record;}
?>
请到微市场微信导航提交你的微信公众帐号
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product