微信公众号最佳实践 ( 10.2)回复QQ表情和Emoji表情

回复QQ表情和Emoji表情

微信的的文字发送方有一个笑脸表情等表情符,单击该表情后,我们可以发送这些表情,这些表情包括QQ表情和Emoji表情(也叫符号表情
如图示:
微信公众号最佳实践 ( 10.2)回复QQ表情和Emoji表情_第1张图片微信公众号最佳实践 ( 10.2)回复QQ表情和Emoji表情_第2张图片

微信用户和微信公众号都能直接发送或回复相应的QQ表情给对方,微信公众号回复QQ标签的时候,需要回复相应的**表情代码**,这样就能发送相应的表情。这些表情代码被当作文本消息在微信中转义成图标。
QQ表情相应代码如下:
网站地址:http://www.oicqzone.com/tool/emoji/
微信公众号最佳实践 ( 10.2)回复QQ表情和Emoji表情_第3张图片

可以使用如下代码来实现文字到QQ表情的转换,这样,在回复的内容中如果有相应的词语,就能转换成相应的QQ表情,代码如下

$face = array('/::)','/::~','/::B','/::|','/:8-)','/::<','/::$','/::X','/::Z','/::\'(','/::-|','/::@','/::P','/::D','/::O','/::(','/::+','/:Cb','/::Q','/::T','/:,@P','/:,@-D','/::d','/:,@o','/::g','/:|-)','/::!','/::L','/::>','/::,@','/:,@f','/::-S','/:?','/:,@x','/:,@@','/::8','/:,@!','/:!!!','/:xx','/:bye','/:wipe','/:dig','/:handclap','/:&-(','/:B-)','/:<@','/:@>','/::-O','/:>-|','/:P-(','/::\'|','/:X-)','/::*','/:@x','/:8*','/:pd','/:','/:beer','/:basketb','/:oo','/:coffee','/:eat','/:pig','/:rose','/:fade','/:showlove','/:heart','/:break','/:cake','/:li','/:bome','/:kn','/:footb','/:ladybug','/:shit','/:moon','/:sun','/:gift','/:hug','/:strong','/:weak','/:share','/:v','/:@)','/:jj','/:@@','/:bad','/:lvu','/:no','/:ok','/:love','/:','/:jump','/:shake','/:','/:circle','/:kotow','/:turn','/:skip','/[]','/:#-0','/[]','/:kiss','/:<&','/:&>');

$word = array('微笑','伤心','美女','发呆','墨镜','哭','羞','哑','睡','哭','囧','怒','调皮','笑','惊讶','难过','酷','汗','抓狂','吐','笑','快乐','奇','傲','饿','累','吓','汗','高兴','闲','努力','骂','疑问','秘密','乱','疯','哀','鬼','打击','bye','汗','抠','鼓掌','糟糕','恶搞','什么','什么','累','看','难过','难过','坏','亲','吓','可怜','刀','水果','酒','篮球','乒乓','咖啡','美食','动物','鲜花','枯','唇','爱','分手','生日','电','炸弹','刀','足球','虫','臭','月亮','太阳','礼物','伙伴','赞','差','握手','优','恭','勾','顶','坏','爱','不','好的','爱','吻','跳','怕','尖叫','圈','拜','回头','跳','天使','激动','舞','吻','瑜伽','太极');

$message = $keyword; 

//区别:将 【文字】转换为【QQ表情符号】
$content = str_replace( $word, $face, $message);

//区别:将【QQ表情符号】转化为【文字】
$content = str_replace( $face, $word,  $message);

  • 区别:将 【文字】转换为【QQ表情符号】

$content = str_replace( $word, $face, $message);
微信公众号最佳实践 ( 10.2)回复QQ表情和Emoji表情_第4张图片

  • 区别:将【QQ表情符号】转化为【文字】
    $content = str_replace( $face, $word, $message);
    微信公众号最佳实践 ( 10.2)回复QQ表情和Emoji表情_第5张图片
    Emoji即表情符号,词义来自日语,是由粟田穰嵩创建的,目前已普遍应用于网络聊天软件中。Emoji表情的一部分如图所示:微信公众号最佳实践 ( 10.2)回复QQ表情和Emoji表情_第6张图片

Emoji表情包含多个版本,包含 Unified、 DoCoMo、 KDDI、 Softbank、 Google、 Wechat,其中Unified是最官方的版本,也是微信中使用的版本。Emoji表情使用十六进制代码标记,其符号到达700多种,各符号名称、含义及其在各种版本下的十六进制代码可以参考官网,
官网地址:https://emojipedia.org/

QQ地带Emoji表情代码大全:http://www.oicqzone.com/tool/emoji/

在微信公众号平台中,公众号回复Emoji表情,不能直接回复代码,需要进行ASCII转码,然后将编码以文本消息回复。

十六进制转码代码如下:

$cp)
  {
      if ($cp > 0x10000){
          # 4 bytes
          return    chr(0xF0 | (($cp & 0x1C0000) >> 18)).
              chr(0x80 | (($cp & 0x3F000) >> 12)).
              chr(0x80 | (($cp & 0xFC0) >> 6)).
              chr(0x80 | ($cp & 0x3F));
      }else if ($cp > 0x800){
         # 3 bytes
         return    chr(0xE0 | (($cp & 0xF000) >> 12)).
             chr(0x80 | (($cp & 0xFC0) >> 6)).
             chr(0x80 | ($cp & 0x3F));
     }else if ($cp > 0x80){
         # 2 bytes
         return    chr(0xC0 | (($cp & 0x7C0) >> 6)).
             chr(0x80 | ($cp & 0x3F));
     }else{
       # 1 byte
       return chr($cp);
   }
 }
?>

构造文本消息时,将相应的十六进制作为参数填入转码函数中即可,特别要注意的是,部分表情有两组十六进制代码,比如”中国国旗”,“美国国旗”,,需要将两组十六进制都进行转码后拼接,代码实现如下

  private function receiveText($object)
  {
      $keyword = trim($object->Content);
      $content = "中国国旗:".utf8_bytes(0x1F1E8).utf8_bytes(0x1F1F3)."\n".
          "美国国旗:".utf8_bytes(0x1F1FA).utf8_bytes(0x1F1F8)."\n".
          "男女牵手:".utf8_bytes(0x1F46B)."\n".
          "仙人掌:".utf8_bytes(0x1F335)."\n".
        "电话机:".utf8_bytes(0x260E)."\n".
        "药丸:".utf8_bytes(0x1F48A);
     $result = $this->transmitText($object, $content);
     return $result;
 }

微信公众号最佳实践 ( 10.2)回复QQ表情和Emoji表情_第7张图片
index.php整体代码如下:


/*
    CopyRight 2018 All Rights Reserved
*/

define("TOKEN", "weixin");

$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
    $wechatObj->responseMsg();
}else{
    $wechatObj->valid();
}

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            echo $echoStr;
            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;
        }
    }

    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)){
            $this->logger("R ".$postStr);
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);

            switch ($RX_TYPE)
            {
                case "event":
                    $result = $this->receiveEvent($postObj);
                    break;
                case "text":
                    $result = $this->receiveText($postObj);
                    break;
            }
            $this->logger("T ".$result);
            echo $result;
        }else {
            echo "";
            exit;
        }
    }

    private function receiveEvent($object)
    {
         $content = "";
         switch ($object->Event)
       {
           case "subscribe":
               $content = "欢迎关注 德强1012";
               if (!empty($object->EventKey)){
                   $sceneid = str_replace("qrscene_"," ",$object->EventKey);
                    require_once('coon.php');
                     $db = new class_mysql();
                     $sql = "INSERT INTO `qrcode` (`id`, `scene`, `year`, `month`, `day`) VALUES (NULL, '".$sceneid."', '".date("Y")."', '".date("m")."', '".date("d")."')";
                     $db->query($sql);
                 }
                 break;
             case "unsubscribe":
                 $content = "取消关注";
                 break;
             case "SCAN":
                 $content = "扫描场景 ".$object->EventKey;
                 break;
             default:
                 break;
         }
         if (is_array($content)){
             $result = $this->transmitNews($object, $content);
         }else{
             $result = $this->transmitText($object, $content);
         }
         return $result;
    }


    private function receiveText($object)
    {
          $keyword = trim($object->Content);
          if($keyword =="Emoji表情"){
             include("emoji.php");
             $content = "中国国旗:".utf8_bytes(0x1F1E8).utf8_bytes(0x1F1F3)."\n".
                  "美国国旗:".utf8_bytes(0x1F1FA).utf8_bytes(0x1F1F8)."\n".
                  "男女牵手:".utf8_bytes(0x1F46B)."\n".
                  "仙人掌:".utf8_bytes(0x1F335)."\n".
                "电话机:".utf8_bytes(0x260E)."\n".
                "药丸:".utf8_bytes(0x1F48A);
          }else{
             $face = array('/::)','/::~','/::B','/::|','/:8-)','/::<','/::$','/::X','/::Z','/::\'(','/::-|','/::@','/::P','/::D','/::O','/::(','/::+','/:Cb','/::Q','/::T','/:,@P','/:,@-D','/::d','/:,@o','/::g','/:|-)','/::!','/::L','/::>','/::,@','/:,@f','/::-S','/:?','/:,@x','/:,@@','/::8','/:,@!','/:!!!','/:xx','/:bye','/:wipe','/:dig','/:handclap','/:&-(','/:B-)','/:<@','/:@>','/::-O','/:>-|','/:P-(','/::\'|','/:X-)','/::*','/:@x','/:8*','/:pd','/:','/:beer','/:basketb','/:oo','/:coffee','/:eat','/:pig','/:rose','/:fade','/:showlove','/:heart','/:break','/:cake','/:li','/:bome','/:kn','/:footb','/:ladybug','/:shit','/:moon','/:sun','/:gift','/:hug','/:strong','/:weak','/:share','/:v','/:@)','/:jj','/:@@','/:bad','/:lvu','/:no','/:ok','/:love','/:','/:jump','/:shake','/:','/:circle','/:kotow','/:turn','/:skip','/[]','/:#-0','/[]','/:kiss','/:<&','/:&>');
             $word = array('微笑','伤心','美女','发呆','墨镜','哭','羞','哑','睡','哭','囧','怒','调皮','笑','惊讶','难过','酷','汗','抓狂','吐','笑','快乐','奇','傲','饿','累','吓','汗','高兴','闲','努力','骂','疑问','秘密','乱','疯','哀','鬼','打击','bye','汗','抠','鼓掌','糟糕','恶搞','什么','什么','累','看','难过','难过','坏','亲','吓','可怜','刀','水果','酒','篮球','乒乓','咖啡','美食','动物','鲜花','枯','唇','爱','分手','生日','电','炸弹','刀','足球','虫','臭','月亮','太阳','礼物','伙伴','赞','差','握手','优','恭','勾','顶','坏','爱','不','好的','爱','吻','跳','怕','尖叫','圈','拜','回头','跳','天使','激动','舞','吻','瑜伽','太极');
             $message = $keyword; 
             $content = str_replace( $word, $face, $message);
          }
          $result = $this->transmitText($object, $content);
         return $result;
     }

    private function transmitText($object, $content)
    {
        $textTpl = "
                        
                        
                        %s
                        
                        
                     ";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        return $result;
    }

    private function transmitNews($object, $arr_item)
    {
        if(!is_array($arr_item))
            return;

        $itemTpl = "    
                            <![CDATA[%s]]>
                            
                            
                            
                        
                    ";
        $item_str = "";
        foreach ($arr_item as $item)
            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);

        $newsTpl = "
                        
                        
                        %s
                        
                        
                        %s
                        
                        $item_str
                     ";

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

    private function logger($log_content)
    {
        if(isset($_SERVER['HTTP_BAE_ENV_APPID'])){   //BAE
            require_once "BaeLog.class.php";
            $logger = BaeLog::getInstance();
            $logger ->logDebug($log_content);
        }else if(isset($_SERVER['HTTP_APPNAME'])){   //SAE
            sae_set_display_errors(false);
            sae_debug($log_content);
            sae_set_display_errors(true);
        }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL
            $max_size = 10000;
            $log_filename = "log.xml";
            if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
            file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND);
        }
    }
}


?>

你可能感兴趣的:(微信公众号开发最佳实践,回复QQ表情和Emoji表情,微信公众号开发最佳实践)