zoukankan      html  css  js  c++  java
  • 微信接口(一)创建菜单&自动回复

    刚划拉完微信。做一个笔记这里的数据是写死的,还有一份是通过查询数据库进行自动回复,自定义菜单设置的。不过因为使用到数据库,最好在网站后台吧微信平台开发集成进去。所以代码较多就先不放了。有问题的地方请留言,我也学习一下。

    <?php
    /*
    * @author;dou
    */
    //token值(令牌),与公众平台设置一致
    define("TOKEN", "dou");
    $wechatObj = new wechatCallbackapiTest();
    if (!isset($_GET['echostr'])) {
    $wechatObj->responseMsg();
    }else{
    $wechatObj->valid();
    }
    class wechatCallbackapiTest
    {
    //验证签名
    public function valid()
    {
    $echoStr = $_GET["echostr"];
    $signature = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
    $nonce = $_GET["nonce"];
    $token = TOKEN;
    $tmpArr = array($token, $timestamp, $nonce);
    sort($tmpArr, SORT_STRING);
    $tmpStr = implode($tmpArr);
    $tmpStr = sha1($tmpStr);
    if($tmpStr == $signature){
    echo $echoStr;
    exit;
    }
    }

    //响应消息
    public function responseMsg()
    {
    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
    if (!empty($postStr)){
    //$access_token = $this->get_access_token();//获取access_token
    //$this->logger("getmenu ".$this->getmenu($access_token));
    $this->createmenu($access_token);//创建菜单
    //获得菜单
    //$this->logger("getmenu ".$this->getmenu($access_token));
    $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
    $RX_TYPE = trim($postObj->MsgType);
    if (($postObj->MsgType == "event") && ($postObj->Event == "subscribe" || $postObj->Event == "unsubscribe")){
    //过滤关注和取消关注事件
    }else{
    }

    //消息类型分离
    switch ($RX_TYPE)
    {
    case "event":
    $result = $this->receiveEvent($postObj);
    break;
    case "text":
    $result = $this->receiveText($postObj);
    break;
    default:
    $result = "unknown msg type: ".$RX_TYPE;
    break;
    }
    echo $result;
    }else {
    echo "";
    exit;
    }
    }

    //接收事件消息
    private function receiveEvent($object)
    {
    $content = "";
    switch ($object->Event)
    {
    case "subscribe":
    $content = "测试内容:欢迎关注";
    break;
    case "unsubscribe":
    $content = "取消关注";
    break;
    default:
    $content = "no message!!!";
    break;
    }

    if(is_array($content)){
    if (isset($content[0]['PicUrl'])){
    $result = $this->transmitNews($object, $content);
    }
    }else{
    $result = $this->transmitText($object, $content);
    }
    return $result;
    }

    //接收文本消息
    private function receiveText($object)
    {
    $keyword = trim($object->Content);
    if (strstr($keyword, "1")||strstr($keyword, "2")||strstr($keyword, "3")||strstr($keyword, "4")||strstr($keyword, "5")||strstr($keyword, "6")){
    $content = "测试内容:[文字信息]自动回复";
    }
    else if (strstr($keyword, "11") || strstr($keyword, "22") ){
    $content = array();
    $content[] = array("Title"=>"[图文消息]标题", "Description"=>"详情信息", "PicUrl"=>"demo.jpg", "Url" =>"http://mp.weixin.qq.com/s");
    }else{
    $content="";
    $content = date("Y-m-d H:i:s",time())." OpenID:".$object->FromUserName." dou";
    }
    if(is_array($content)){
    if (isset($content[0])){
    $result = $this->transmitNews($object, $content);
    }
    }else{
    $result = $this->transmitText($object, $content);
    }
    return $result;
    }


    //回复文本消息
    private function transmitText($object, $content)
    {
    if (!isset($content) || empty($content)){
    return "";
    }
    $xmlTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[text]]></MsgType>
    <Content><![CDATA[%s]]></Content>
    </xml>";
    $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);

    return $result;
    }

    //回复图文消息
    private function transmitNews($object, $newsArray)
    {
    if(!is_array($newsArray)){
    return "";
    }
    $itemTpl = " <item>
    <Title><![CDATA[%s]]></Title>
    <Description><![CDATA[%s]]></Description>
    <PicUrl><![CDATA[%s]]></PicUrl>
    <Url><![CDATA[%s]]></Url>
    </item>
    ";
    $item_str = "";
    foreach ($newsArray as $item){
    $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
    }
    $xmlTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[news]]></MsgType>
    <ArticleCount>%s</ArticleCount>
    <Articles>
    $item_str </Articles>
    </xml>";

    $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
    return $result;
    }


    /////////////////


    private function get_access_token()
    {
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=*&secret=*";
    $data = json_decode(file_get_contents($url),true);
    if($data['access_token']){
    return $data['access_token'];
    }else{
    return "获取access_token错误";
    }
    }

    //自定义菜单
    public function createmenu($access_token)
    {
    $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
    $arr = array(
    'button' =>array(
    array(
    'name'=>urlencode("菜单1"),
    'sub_button'=>array(
    array(
    'name'=>urlencode("子菜单1"),
    'type'=>'view',
    'url'=>'http://www.baidu.com'
    ),
    )),
    )
    );


    $jsondata = urldecode(json_encode($arr));

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$jsondata);
    curl_exec($ch);
    curl_close($ch);
    }


    private function getmenu($access_token)
    {
    $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$access_token;
    $data = file_get_contents($url);
    return $data;
    }

    }
    ?>

    <--------个人原创|转载注明逗子|新手适用|大手勿喷|--------->

  • 相关阅读:
    Android Listview 隐藏滚动条
    打开Activity时,不自动显示(弹出)虚拟键盘
    Spring Boot web API接口设计之token、timestamp、sign
    WPF ListView点击删除某一行并获取绑定数据
    WPF中控件的显示与隐藏
    WPF 格式化输出- IValueConverter接口的使用 datagrid列中的值转换显示
    WPF之DataGrid应用 翻页
    WPF中修改DataGrid单元格值并保存
    DataGrid获取单元格的值
    WPF DataGrid 列宽填充表格方法
  • 原文地址:https://www.cnblogs.com/douz/p/7098700.html
Copyright © 2011-2022 走看看