PHP前端开发

微信开发Emoji表情的实例教程

百变鹏仔 2天前 #前端问答
文章标签 实例教程

一 什么是emoji  

emoji就是表情符号;词义来自日语(えもじ,e-moji,moji在日语中的含义是字符)
表情符号现已普遍应用于手机短信和网络聊天软件。
emoji表情符号,在外国的手机短信里面已经是很流行使用的一种表情。

在国内的微信和微信中也被采用。

Emoji 全编码表:
punchdrunker.github.com/iOSEmoji/table_html/flower.html

code.iamcal.com/php/emoji/

<?php     include(&#39;emoji.php&#39;);    # browser sniffing tells us that a docomo phone    # submitted this text    $clean_text = emoji_docomo_to_unified($_POST[message]);    ...    # now we want to show it in a desktop browser    $html = emoji_unified_to_html($clean_text);?>

 回复代码如下所示:

<?php /*    方倍工作室 http://www.fangbei.org/    CopyRight 2015 All Rights Reserved*/define("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();if (!isset($_GET[&#39;echostr&#39;])) {    $wechatObj->responseMsg();}else{    $wechatObj-&gt;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){            header('content-type:text');            echo $echoStr;            exit;        }    }    //响应消息    public function responseMsg()    {        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];        if (!empty($postStr)){            $this-&gt;logger("R ".$postStr);            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);            $RX_TYPE = trim($postObj-&gt;MsgType);                                 //消息类型分离            switch ($RX_TYPE)            {                case "event":                    $result = $this-&gt;receiveEvent($postObj);                    break;                case "text":                    $result = $this-&gt;receiveText($postObj);                    break;                               default:                    $result = "unknown msg type: ".$RX_TYPE;                    break;            }            $this-&gt;logger("T ".$result);            echo $result;        }else {            echo "";            exit;        }    }    //接收事件消息    private function receiveEvent($object)    {        $content = "";        switch ($object-&gt;Event)        {            case "subscribe":                $content = "欢迎关注方倍工作室 ";                break;            case "unsubscribe":                $content = "取消关注";                break;            case "CLICK":                switch ($object-&gt;EventKey)                {                    case "COMPANY":                        $content = array();                        $content[] = array("Title"=&gt;"方倍工作室", "Description"=&gt;"", "PicUrl"=&gt;"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =&gt;"http://m.cnblogs.com/?u=txw1958");                        break;                    default:                        $content = "点击菜单:".$object-&gt;EventKey;                        break;                }                break;            default:                $content = "receive a new event: ".$object-&gt;Event;                break;        }        if(is_array($content)){            if (isset($content[0]['PicUrl'])){                $result = $this-&gt;transmitNews($object, $content);            }else if (isset($content['MusicUrl'])){                $result = $this-&gt;transmitMusic($object, $content);            }        }else{            $result = $this-&gt;transmitText($object, $content);        }        return $result;    }    //接收文本消息    private function receiveText($object)    {        $keyword = trim($object-&gt;Content);        //多客服人工回复模式        if (strstr($keyword, "请问在吗") || strstr($keyword, "在线客服")){            $result = $this-&gt;transmitService($object);            return $result;        }        //自动回复模式        if (strstr($keyword, "文本")){            $content = "这是个文本消息";        }else if (strstr($keyword, "表情")){            $content = "中国:".$this-&gt;bytes_to_emoji(0x1F1E8).$this-&gt;bytes_to_emoji(0x1F1F3)."仙人掌:".$this-&gt;bytes_to_emoji(0x1F335);        }else if (strstr($keyword, "单图文")){            $content = array();            $content[] = array("Title"=&gt;"单图文标题",  "Description"=&gt;"单图文内容", "PicUrl"=&gt;"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =&gt;"http://m.cnblogs.com/?u=txw1958");        }else if (strstr($keyword, "图文") || strstr($keyword, "多图文")){            $content = array();            $content[] = array("Title"=&gt;"多图文1标题", "Description"=&gt;"", "PicUrl"=&gt;"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =&gt;"http://m.cnblogs.com/?u=txw1958");            $content[] = array("Title"=&gt;"多图文2标题", "Description"=&gt;"", "PicUrl"=&gt;"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =&gt;"http://m.cnblogs.com/?u=txw1958");            $content[] = array("Title"=&gt;"多图文3标题", "Description"=&gt;"", "PicUrl"=&gt;"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =&gt;"http://m.cnblogs.com/?u=txw1958");        }else if (strstr($keyword, "音乐")){            $content = array();            $content = array("Title"=&gt;"最炫民族风", "Description"=&gt;"歌手:凤凰传奇", "MusicUrl"=&gt;"http://121.199.4.61/music/zxmzf.mp3", "HQMusicUrl"=&gt;"http://121.199.4.61/music/zxmzf.mp3");         }else{            $content = date("Y-m-d H:i:s",time())."OpenID:".$object-&gt;FromUserName."技术支持 方倍工作室";            // $content = "";        }        if(is_array($content)){            if (isset($content[0])){                $result = $this-&gt;transmitNews($object, $content);            }else if (isset($content['MusicUrl'])){                $result = $this-&gt;transmitMusic($object, $content);            }        }else{            $result = $this-&gt;transmitText($object, $content);        }        return $result;    }    //回复文本消息    private function transmitText($object, $content)    {        if (!isset($content) || empty($content)){            return "";        }        $xmlTpl = "<xml>    <tousername></tousername>    <fromusername></fromusername>    <createtime>%s</createtime>    <msgtype></msgtype>    <content></content></xml>";        $result = sprintf($xmlTpl, $object-&gt;FromUserName, $object-&gt;ToUserName, time(), $content);        return $result;    }    //回复图文消息    private function transmitNews($object, $newsArray)    {        if(!is_array($newsArray)){            return "";        }        $itemTpl = "        <item>            <title></title>            <description></description>            <picurl></picurl>            <url></url>        </item>";        $item_str = "";        foreach ($newsArray as $item){            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);        }        $xmlTpl = "<xml>    <tousername></tousername>    <fromusername></fromusername>    <createtime>%s</createtime>    <msgtype></msgtype>    <articlecount>%s</articlecount>    <articles>$item_str    </articles></xml>";        $result = sprintf($xmlTpl, $object-&gt;FromUserName, $object-&gt;ToUserName, time(), count($newsArray));        return $result;    }    //回复音乐消息    private function transmitMusic($object, $musicArray)    {        if(!is_array($musicArray)){            return "";        }        $itemTpl = "<music>        <title></title>        <description></description>        <musicurl></musicurl>        <hqmusicurl></hqmusicurl>    </music>";        $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);        $xmlTpl = "<xml>    <tousername></tousername>    <fromusername></fromusername>    <createtime>%s</createtime>    <msgtype></msgtype>    $item_str</xml>";        $result = sprintf($xmlTpl, $object-&gt;FromUserName, $object-&gt;ToUserName, time());        return $result;    }    //字节转Emoji表情    function bytes_to_emoji($cp)    {        if ($cp &gt; 0x10000){       # 4 bytes            return chr(0xF0 | (($cp &amp; 0x1C0000) &gt;&gt; 18)).chr(0x80 | (($cp &amp; 0x3F000) &gt;&gt; 12)).chr(0x80 | (($cp &amp; 0xFC0) &gt;&gt; 6)).chr(0x80 | ($cp &amp; 0x3F));        }else if ($cp &gt; 0x800){   # 3 bytes            return chr(0xE0 | (($cp &amp; 0xF000) &gt;&gt; 12)).chr(0x80 | (($cp &amp; 0xFC0) &gt;&gt; 6)).chr(0x80 | ($cp &amp; 0x3F));        }else if ($cp &gt; 0x80){    # 2 bytes            return chr(0xC0 | (($cp &amp; 0x7C0) &gt;&gt; 6)).chr(0x80 | ($cp &amp; 0x3F));        }else{                    # 1 byte            return chr($cp);        }    }    //日志记录    private function logger($log_content)    {        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 = 1000000;            $log_filename = "log.xml";            if(file_exists($log_filename) and (abs(filesize($log_filename)) &gt; $max_size)){unlink($log_filename);}            file_put_contents($log_filename, date('Y-m-d H:i:s')." ".$log_content."", FILE_APPEND);        }    }}?&gt;