


PHP 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.

//小黄鸡 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; } } }

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:

//小九机器人 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; } } }

5.3 Two Dragons Playing with Phoenix
We can also integrate the Xiaohuangji and Xiaojiu robots above. The specific code is as follows:

//双龙戏凤 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]"; } }

6. Test

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

Compatibility issues and troubleshooting methods for company security software and application. Many companies will install security software in order to ensure intranet security. However, security software sometimes...

Strict types in PHP are enabled by adding declare(strict_types=1); at the top of the file. 1) It forces type checking of function parameters and return values to prevent implicit type conversion. 2) Using strict types can improve the reliability and predictability of the code, reduce bugs, and improve maintainability and readability.
