search
HomeBackend DevelopmentPHP TutorialPHP WeChat public platform development chat robot development_PHP tutorial

[PHP WeChat public platform development series]

01. Configure WeChat interface
02. Public platform sample code analysis
03. Subscription event (subscribe) processing
04. Development of simple reply function
05. Weather forecast function development
06. Translation function development
07. Chatbot function development


URL of this article: http://www.phpchina.com/archives/view-43393-1.html
This series is contributed by PHPChina's specially invited author @David_Tang. Please indicate the author's information and the address of this article when reprinting.

1. Introduction

The previous article introduced the development of the translation function of the WeChat public platform, which realizes translation between Chinese, English and Japanese, and can also be used in real life. In the next article, we will complete a more interesting function, which is a chatbot that can chat with you and make you happy when you are bored.

2. Idea analysis

In this experiment, we will call the API provided by the official Xiaohuangji (http://www.simsimi.com/), combined with crawling the webpage of the Xiaojiu robot (http://www.xiaojo.com/), complement each other. Simsimi is charged, but you can try a 7-day test and you can use 100 replies for free every day; Xiaojiu robot can be used without restrictions, but only if it is not officially blocked.

3. Little Yellow Chicken API Analysis

3.1 API & URL

Official API address: http://developer.simsimi.com/api

Request URL: http://sandbox.api.simsimi.com/request.p

The free version is used here for testing. The paid version is similar except that the URL address is different.

3.2 Request examples and parameter description

Request example:

http://sandbox.api.simsimi.com/request.p?key=your_trial_key&lc=en&ft=1.0&text=hi

Parameter description:

key: applied API Key

lc: Language code, supported languages, use ch for simplified Chinese, zh for traditional Chinese, and en for English. For details, please refer to: http://developer.simsimi.com/lclist

ft: whether to set filter,

​0.0: Unfiltered (contains cursing, sexual content);

1.0: Filter uncivilized words (only supports Korean for now)

text: requested text

3.3 Return value analysis

result: execution result return code

    • 100-OK.
    • 400-Bad Request.
    • 401-Unauthorized.
    • 404-Not found.
    • 500-Server Error.

id: Reply message id (this item is only available when result=100)

response: reply message (this item is only available when result=100)

msg: Status corresponding to the execution result return code

4. Obtain the Little Yellow Chicken API Key

4.1 Register simsimi account

URL: http://developer.simsimi.com/signUp

4.2 Activate account

4.3 Obtain API Key

5. Specific implementation

5.1 Implementation of calling Xiaohuangji API

Call the simsim($keyword) function and replace "Your API Key" with the applied API Key.

PHP WeChat public platform development chat robot development_PHP tutorial
    //小黄鸡
    public function simsim($keyword){

        $key="41250a68-3cb5-43c8-9aa2-d7b3caf519b1";
        $url_simsimi="http://sandbox.api.simsimi.com/request.p?key=".$key."&lc=ch&ft=0.0&text=".$keyword;
        
        $json=file_get_contents($url_simsimi);  // 把整个文件读入一个字符串中

        $result=json_decode($json,true);  // 对JSON 格式的字符串进行编码

        //$errorCode=$result['result'];  // 调试用

        $response=$result['response'];  // 回复的消息

        if(!empty($response)){
            return $response;
        }else{
            $ran=rand(1,5);
            switch($ran){
                case 1:
                    return "小鸡鸡今天累了,明天再陪你聊天吧。";
                    break;
                case 2:
                    return "小鸡鸡睡觉喽~~";
                    break;
                case 3:
                    return "呼呼~~呼呼~~";
                    break;
                case 4:
                    return "你话好多啊,不跟你聊了";
                    break;
                case 5:
                    return "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,万代不朽";
                    break;
                default:
                    return "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,万代不朽";
                    break;
            }
        }
    }
PHP WeChat public platform development chat robot development_PHP tutorial

Description:

Because sometimes the little yellow chicken does not reply, a judgment is added to the simsim() function. If $response is not empty, $response is returned; if $response is empty, a small code is added to let it Reply with customized messages randomly so you can respond to requests.

5.2 Call Xiaojiu robot to implement

Xiaojiu Robot does not provide an API, so web crawling can only be achieved through PHP functions. The code is as follows:

PHP WeChat public platform development chat robot development_PHP tutorial
    //小九机器人
    public function xiaojo($keyword){

        $curlPost=array("chat"=>$keyword);
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,'http://www.xiaojo.com/bot/chata.php');//抓取指定网页
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        if(!empty($data)){
            return $data;
        }else{
            $ran=rand(1,5);
            switch($ran){
                case 1:
                    return "小鸡鸡今天累了,明天再陪你聊天吧。";
                    break;
                case 2:
                    return "小鸡鸡睡觉喽~~";
                    break;
                case 3:
                    return "呼呼~~呼呼~~";
                    break;
                case 4:
                    return "你话好多啊,不跟你聊了";
                    break;
                case 5:
                    return "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,万代不朽";
                    break;
                default:
                    return "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,万代不朽";
                    break;
            }
        }
    }
PHP WeChat public platform development chat robot development_PHP tutorial

5.3 Two Dragons Playing with Phoenix

We can also integrate the Xiaohuangji and Xiaojiu robots above. The specific code is as follows:

PHP WeChat public platform development chat robot development_PHP tutorial
    //双龙戏凤
    public function chatter($keyword){

        $curlPost=array("chat"=>$keyword);
        $ch = curl_init();    //初始化curl
        curl_setopt($ch, CURLOPT_URL,'http://www.xiaojo.com/bot/chata.php');    //抓取指定网页
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_HEADER, 0);    //设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);    //post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);    //运行curl
        curl_close($ch);

        if(!empty($data)){
            return $data." [/::)小九]";
        }else{
            return $this->simsim($keyword)." [simsim/::D]";
        }
    }
PHP WeChat public platform development chat robot development_PHP tutorial

6. Test

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/740331.htmlTechArticle[PHP WeChat public platform development series] 01. Configure WeChat interface 02. Public platform sample code analysis 03. Subscribe to events (subscribe) processing 04. Simple reply function development 05. Weather forecast function...
Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment