


WeChat public platform development (7) Chat robot function development_PHP tutorial
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.
In this experiment, we will call the API provided by the official Xiaohuangji (http://www.simsimi.com/), combined with grabbing the Xiaojiu robot (http://www.xiaojo.com/) web pages that 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.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 for testing here. 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<span &lc</span>=en<span &ft</span>=1.0<span &text</span>=hi
Parameter description:
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
0.0: Unfiltered (contains curses, sexual content);
1.0: Filter out uncivilized words (temporarily only supports Korean)
3.3 Return value analysis
result: execution result return code
- 400-Bad Request.
- 401-Unauthorized.
- 404-Not found.
- 500-Server Error.
id: reply message id (only available when result=100)
response: reply message (only available when result=100)
4.1 Register simsimi account
URL: http://developer.simsimi.com/signUp
4.2 Activate account
4.3 Obtain API Key
5.1 Implementation of calling Xiaohuangji API
Call the simsim($keyword) function and replace "Your API Key" with the applied API Key.
<span //</span><span 小黄鸡</span> <span public</span> <span function</span> simsim(<span $keyword</span><span ){ </span><span $key</span>="<span>41250a68-3cb5-43c8-9aa2-d7b3caf519b1</span>"<span ; </span><span $url_simsimi</span>="http://sandbox.api.simsimi.com/request.p?key=".<span $key</span>."&lc=ch&ft=0.0&text=".<span $keyword</span><span ; </span><span $json</span>=<span file_get_contents</span>(<span $url_simsimi</span><span ); <span>// </span><span>把整个文件读入一个字符串中</span> </span><span $result</span>=json_decode(<span $json</span>,<span true</span><span ); <span>// </span><span>对JSON 格式的字符串进行编码</span> </span><span //</span><span $errorCode=$result['result']; // 调试用</span> <span $response</span>=<span $result</span>['response'<span ]; // 回复的消息 </span><span if</span>(!<span empty</span>(<span $response</span><span )){ </span><span return</span> <span $response</span><span ; }</span><span else</span><span { </span><span $ran</span>=<span rand</span>(1,5<span ); </span><span switch</span>(<span $ran</span><span ){ </span><span case</span> 1: <span return</span> "小鸡鸡今天累了,明天再陪你聊天吧。"<span ; </span><span break</span><span ; </span><span case</span> 2: <span return</span> "小鸡鸡睡觉喽~~"<span ; </span><span break</span><span ; </span><span case</span> 3: <span return</span> "呼呼~~呼呼~~"<span ; </span><span break</span><span ; </span><span case</span> 4: <span return</span> "你话好多啊,不跟你聊了"<span ; </span><span break</span><span ; </span><span case</span> 5: <span return</span> "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,万代不朽"<span ; </span><span break</span><span ; </span><span default</span>: <span return</span> "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,万代不朽"<span ; </span><span break</span><span ; } } }</span>
Instructions:
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 , and let it randomly reply to a customized message, so that it can respond to requests.
5.2 Call Xiaojiu robot to implement
Xiaojiu Robot does not provide API, so it can only be crawled through web pages. The code is as follows:
<span //</span><span 小九机器人</span> <span public</span> <span function</span> xiaojo(<span $keyword</span><span ){ </span><span $curlPost</span>=<span array</span>("chat"=><span $keyword</span><span ); </span><span $ch</span> = curl_init();<span //</span><span 初始化curl</span> curl_setopt(<span $ch</span>, CURLOPT_URL,'http://www.xiaojo.com/bot/chata.php');<span //</span><span 抓取指定网页</span> curl_setopt(<span $ch</span>, CURLOPT_HTTPHEADER, <span $header</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_HEADER, 0);<span //</span><span 设置header</span> curl_setopt(<span $ch</span>, CURLOPT_RETURNTRANSFER, 1);<span //</span><span 要求结果为字符串且输出到屏幕上</span> curl_setopt(<span $ch</span>, CURLOPT_POST, 1);<span //</span><span post提交方式</span> curl_setopt(<span $ch</span>, CURLOPT_POSTFIELDS, <span $curlPost</span><span ); </span><span $data</span> = curl_exec(<span $ch</span>);<span //</span><span 运行curl</span> curl_close(<span $ch</span><span ); </span><span if</span>(!<span empty</span>(<span $data</span><span )){ </span><span return</span> <span $data</span><span ; }</span><span else</span><span { </span><span $ran</span>=<span rand</span>(1,5<span ); </span><span switch</span>(<span $ran</span><span ){ </span><span case</span> 1: <span return</span> "小鸡鸡今天累了,明天再陪你聊天吧。"<span ; </span><span break</span><span ; </span><span case</span> 2: <span return</span> "小鸡鸡睡觉喽~~"<span ; </span><span break</span><span ; </span><span case</span> 3: <span return</span> "呼呼~~呼呼~~"<span ; </span><span break</span><span ; </span><span case</span> 4: <span return</span> "你话好多啊,不跟你聊了"<span ; </span><span break</span><span ; </span><span case</span> 5: <span return</span> "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,万代不朽"<span ; </span><span break</span><span ; </span><span default</span>: <span return</span> "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,万代不朽"<span ; </span><span break</span><span ; } } }</span>
5.3 Two Dragons Playing with Phoenix
We can also integrate the above Xiaohuangji and Xiaojiu robots. The specific code is as follows:
<span //</span><span 双龙戏凤</span> <span public</span> <span function</span> chatter(<span $keyword</span><span ){ </span><span $curlPost</span>=<span array</span>("chat"=><span $keyword</span><span ); </span><span $ch</span> = curl_init(); <span //</span><span 初始化curl</span> curl_setopt(<span $ch</span>, CURLOPT_URL,'http://www.xiaojo.com/bot/chata.php'); <span //</span><span 抓取指定网页</span> curl_setopt(<span $ch</span>, CURLOPT_HTTPHEADER, <span $header</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_HEADER, 0); <span //</span><span 设置header</span> curl_setopt(<span $ch</span>, CURLOPT_RETURNTRANSFER, 1); <span //</span><span 要求结果为字符串且输出到屏幕上</span> curl_setopt(<span $ch</span>, CURLOPT_POST, 1); <span //</span><span post提交方式</span> curl_setopt(<span $ch</span>, CURLOPT_POSTFIELDS, <span $curlPost</span><span ); </span><span $data</span> = curl_exec(<span $ch</span>); <span //</span><span 运行curl</span> curl_close(<span $ch</span><span ); </span><span if</span>(!<span empty</span>(<span $data</span><span )){ </span><span return</span> <span $data</span>." [/::)小九]"<span ; }</span><span else</span><span { </span><span return</span> <span $this</span>->simsim(<span $keyword</span>)." [simsim/::D]"<span ; } }</span>
Please go to QQ group 213260412 to share to download and use.
Please follow Zhuojin Suzhou WeChat public account, Zhuojin Suzhou is developed based on SAE platform, Develop and test mainstream WeChat functions.
You can follow the Zhuojin Suzhou public account to conduct functional testing and obtain new application development.
1. Log in to the WeChat client, friends -> add friends -> search number -> zhuojinsz, find and follow.
2. Scan the QR code:
Zhuojin Suzhou Menu function:
Currently, the specific functions of the menu are still under development and will be updated gradually, so stay tuned. . .
We Believe, Great People Share Knowledge...

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools
