Home > Backend Development > PHP Tutorial > thinkPHP WeChat public account development, keyword reply using switch error

thinkPHP WeChat public account development, keyword reply using switch error

WBOY
Release: 2016-09-24 09:15:10
Original
1695 people have browsed it

Using PHP to develop the WeChat public platform, I encountered a problem when doing keyword replies, using if. There is no problem with the else if statement, but if it is switched to switch, the public platform cannot be submitted when modifying the configuration. Has anyone encountered this situation?
Here is the code:

<code>
if (strtolower( $postObj->MsgType ) == 'text') {
            switch ( trim($postObj->Content )) {
                case '电影':
                    $contnet = '最近都没有看过什么电影,真的是没有什么时间啊。';
                    break;
                case '菜':
                    $content = '最喜欢吃红烧肉了,真心不喜欢那些都是骨头和皮没有肉的荤菜。';
                    break;
                case '饮料':
                    $content = '柠檬水,我感觉我对柠檬水上瘾了。'
                    break;
                default:
                    $content = '这不是我们设置的关键字,你再看看?';
                    break;
            }
            $toUser = $postObj->FromUserName;
            $fromUser = $postObj->ToUserName;
            $time = time();
            $msgType = 'text';
            $template = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        </xml>";
            $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
            echo $info;
        }
</code>
Copy after login
Copy after login

Reply content:

Using PHP to develop the WeChat public platform, I encountered a problem when doing keyword replies, using if. There is no problem with the else if statement, but if it is switched to switch, the public platform cannot be submitted when modifying the configuration. Has anyone encountered this situation?
Here is the code:

<code>
if (strtolower( $postObj->MsgType ) == 'text') {
            switch ( trim($postObj->Content )) {
                case '电影':
                    $contnet = '最近都没有看过什么电影,真的是没有什么时间啊。';
                    break;
                case '菜':
                    $content = '最喜欢吃红烧肉了,真心不喜欢那些都是骨头和皮没有肉的荤菜。';
                    break;
                case '饮料':
                    $content = '柠檬水,我感觉我对柠檬水上瘾了。'
                    break;
                default:
                    $content = '这不是我们设置的关键字,你再看看?';
                    break;
            }
            $toUser = $postObj->FromUserName;
            $fromUser = $postObj->ToUserName;
            $time = time();
            $msgType = 'text';
            $template = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        </xml>";
            $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
            echo $info;
        }
</code>
Copy after login
Copy after login

Missing semicolon

<code>case '饮料':
                    $content = '柠檬水,我感觉我对柠檬水上瘾了。'
                    break;</code>
Copy after login

When encountering this kind of problem, it usually prompts a syntax error. Write code carefully and carefully, don't rush.

syntax error is easy to locate. Read the error message carefully.

First. Fromuser and touser use the opposite
second. Logical semicolon

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template