微信接入自定义开发

申请公众号后,填写服务器配置文件,填写自己的服务器路径,路径需要能响应调用接口。第一次接入需要以下代码,微信才能让你的服务器路径提交成功。php代码如下

<?php
/**
  * wechat php test
  */


//define your token
define("TOKEN", "your token");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();


class wechatCallbackapiTest
{
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 = $GLOBALS["HTTP_RAW_POST_DATA"];


      //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;
}
}
}


?>


以上代码可以使得自定义接入成功,接下来就是对消息的响应

响应代码参考自网友


<?php


//加载微信JDK
include "include/wechat.class.php";


$options = array(
        //填写你设定的key
        'token'=>'',
        //填写高级调用功能的app id
        'appid'=>'',
        //填写高级调用功能的密钥
        'appsecret'=>'',
    );


//实例化
$weObj = new Wechat($options);


$weObj->valid();
//获取接收消息的类型
$type = $weObj->getRev()->getRevType();
//获取接收消息内容正文
$keyword =$weObj->getRev()->getRevContent();
//获取接收事件推送
$form = $weObj->getRev()->getRevEvent();
$form_Event =$form['event'];
$form_Key = $form['key'];


switch($type) {
    //text
    case Wechat::MSGTYPE_TEXT:
        switch ($keyword){
            case 1:
                $newsdata = array(
                    array(
                        'Title'=>'xx教育',
                        'Description'=>'hold on ...',
                        'PicUrl'=>'https://www.baidu.com/img/bd_logo1.png',
                        'Url'=>'http://www.mei19.com/'
                    ),
                   );
                 $weObj->news($newsdata)->reply();
                break;
            case 2:
                $newsdata = array(
                    array(
                        'Title'=>'xx图书',
                        'Description'=>'hold on ...',
                        'PicUrl'=>'https://www.baidu.com/img/bd_logo1.png',
                        'Url'=>'http://www.mei19.com/'
                    ),
                   );
                 $weObj->news($newsdata)->reply();
                break;
            case 3:
                $newsdata = array(
                    array(
                        'Title'=>'xx论坛',
                        'Description'=>'hold on ...',
                        'PicUrl'=>'https://www.baidu.com/img/bd_logo1.png',
                        'Url'=>'http://www.mei19.com/'
                    ),
                   );
                 $weObj->news($newsdata)->reply();
                break;
            case 4:
                $newsdata = array(
                    0=>array(
                        'Title'=>'xx资讯',
                        'Description'=>'hold on ...',
                        'PicUrl'=>'https://www.baidu.com/img/bd_logo1.png',
                        'Url'=>'http://www.mei19.com/'
                    ),
                   );
                 $weObj->news($newsdata)->reply();
                break;
            default:
                $weObj->text("可以输入help或者直接给我们留言")->reply();
        }
        break;
    //event
    case Wechat::MSGTYPE_EVENT:
         //判断事件类型
            if ('subscribe' == $form_Event)
            {
                $weObj->text("
你可以输入对应数字查找
                1、xx教育
                2、xx图书
                3、xx论坛
                4、xx资讯
                ")->reply();
            }


            //判断按钮事件
            if ('CLICK' == $form_Event)
            {


                // if (OPEN_COURSE == $form_Key)
                // {
                //     $newsdata = array(
                //         0=>array(
                //             'Title'=>'公开课',
                //             'Description'=>'hold on ...',
                //             'PicUrl'=>'https://www.baidu.com/img/bd_logo1.png',
                //         'Url'=>'http://www.mei19.com/'
                //         ),
                //        );
                //     $t= $weObj->news($newsdata)->reply();
                // }


                // if (LIKE_ME == $form_Key)
                // {
                //     $weObj->text("你的赞,会让我们更加努力")->reply();
                // }
            }
        break;
    //image
    case Wechat::MSGTYPE_IMAGE:
        $weObj->text("你的图和你一样美")->reply();
        break;
    //voice
    case Wechat::MSGTYPE_VOICE:
        $weObj->text("你对我说的情话")->reply();
        break;
    //video
    case Wechat::MSGTYPE_VIDEO:
        $weObj->text("你的视频很美")->reply();
        break;
    //location
    case Wechat::MSGTYPE_LOCATION:
        $weObj->text("你的位置我们会关注")->reply();
        break;
    //link
    case Wechat::MSGTYPE_LINK:
        $weObj->text("你发送了什么链接")->reply();
        break;


    default:
        $weObj->text("你说的我都不懂...")->reply();


}

//获取菜单操作:

$menu = $weObj->getMenu();

//设置菜单

$newmenu = array(  

"button"=>  

array(  

array('type'=>'click','name'=>'公开课','key'=>'OPEN_COURSE'), 

array('type'=>'view','name'=>'yy','url'=>'http://www.yy.com'),  

array('name'=>'关于我们',  

'sub_button'=>array(  

array('type'=>'view','name'=>'企业文化','url'=>'http://www.xxx.com/'),  

array('type'=>'click','name'=>'求赞','key'=>'LIKE_ME'),  

)  

)  

)  

);

$result = $weObj->createMenu($newmenu);

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值