PHP WeChat public platform development Chat Robot Development

Source: Internet
Author: User

[PHP public platform development series] 01. Configuration Interface
02. Public platform sample code analysis
03. subscribe Processing
04. simple reply function development
05. Development of the weather forecast function
06. Development of translation functions
07. chatbot function development
Address: http://www.phpchina.com/archives/view-43393-1.html
This series is provided by PHPChina's special author @ David _tang. For more information, see the author information and the address of this article. I. Introduction

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

Ii. Train of Thought Analysis

In this experiment, we will retrieve the API provided by the little yellow chicken official (http://www.simsimi.com/), combined with crawling the pages of the little 9 robot (http://www.xiaojo.com/), complement each other. Simsimi is charged, but you can try a 7-day test and use 100 replies for free each day. You can use a 9th robot for unlimited use, provided that it is not officially blocked.

Iii. Analysis of chicken's API

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. The paid version is similar, but the URL address is different.

3.2 request example 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: API Key applied

Lc: Language code, Supported languages, ch for simplified Chinese, zh for traditional Chinese, en for English, please refer to: http://developer.simsimi.com/lclist

Ft: whether to set a filter,

0.0: unfiltered (including cursing and sexual content );

1.0: Filter uncivilized words (only Korean is supported currently)

Text: Request text

3.3 Return Value Analysis

Result: return code of the execution result.

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

Id: id of the reply message (this option is available only when result = 100)

Response: The reply message (this option is available only when result = 100)

Msg: status corresponding to the return code of the execution result

4. Obtain the chicken's API Key

4.1 register a simsimi account

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

4.2 activate an account

4.3 obtain the API Key

V. Implementation

5.1 call the xiaohuang chicken API

Call the simsim ($ keyword) function for processing. Replace "Your API Key" with the applied API Key.

// Small yellow chicken 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); // read the entire file into a string $ result = json_decode ($ json, true ); // encode a JSON string // $ errorCode = $ result ['result']; // use $ response = $ result ['response'] for debugging. // if (! Empty ($ response) {return $ response;} else {$ ran = rand (); switch ($ ran) {case 1: return "the chicken is tired today, chat with you tomorrow. "; Break; case 2: return" go to bed ~~ "; Break; case 3: return" Call ~~ Call ~~ "; Break; case 4: return" you have a lot to talk about, don't talk to you "; break; case 5: return" Thank you for your attention [Zhuo Jin Suzhou ]". "\ n ". "No.: zhuojinsz ". "\ n ". "Excellent splendid, immortal"; break; default: return "Thank you for your attention [zhuojin Suzhou ]". "\ n ". "No.: zhuojinsz ". "\ n ". "Excellent splendid, immortal"; break ;}}}

Note:

Sometimes the yellow Rooster does not reply, so a judgment is added to the simsim () function. If $ response is not empty, $ response is returned. If $ response is empty, then a small code is added to randomly reply to the custom message, so that the user can be responsive.

5.2 call xiaojiu robot implementation

Xiaojiu robot does not provide APIs, so it can only capture webpages using PHP functions. The Code is as follows:

// Xiaojiu robot public function xiaojo ($ keyword) {$ curlPost = array ("chat" => $ keyword); $ ch = curl_init (); // initialize curl curl_setopt ($ ch, CURLOPT_URL, 'HTTP: // response); // capture the specified webpage curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); curl_setopt ($ ch, CURLOPT_HEADER, 0); // set the header curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // The result is a string and is output to the screen curl_setopt ($ ch, CURLOPT_POST, 1 ); // post submission method curl_setopt ($ ch, C URLOPT_POSTFIELDS, $ curlPost); $ data = curl_exec ($ ch); // run curl curl_close ($ ch); if (! Empty ($ data) {return $ data;} else {$ ran = rand (); switch ($ ran) {case 1: return "the chicken is tired today, chat with you tomorrow. "; Break; case 2: return" go to bed ~~ "; Break; case 3: return" Call ~~ Call ~~ "; Break; case 4: return" you have a lot to talk about, don't talk to you "; break; case 5: return" Thank you for your attention [Zhuo Jin Suzhou ]". "\ n ". "No.: zhuojinsz ". "\ n ". "Excellent splendid, immortal"; break; default: return "Thank you for your attention [zhuojin Suzhou ]". "\ n ". "No.: zhuojinsz ". "\ n ". "Excellent splendid, immortal"; break ;}}}

5.3 shuanglong opera Feng

We can also integrate the preceding xiaohuang chicken and xiaojiu robot. The specific code is as follows:

// Shuanglong opera Feng public function chatter ($ keyword) {$ curlPost = array ("chat" => $ keyword); $ ch = curl_init (); // initialize curl curl_setopt ($ ch, CURLOPT_URL, 'HTTP: // response); // capture the specified webpage curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); curl_setopt ($ ch, CURLOPT_HEADER, 0); // set the header curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // The result is a string and is output to the screen curl_setopt ($ ch, CURLOPT_POST, 1 ); // curl_setopt ($ Ch, CURLOPT_POSTFIELDS, $ curlPost); $ data = curl_exec ($ ch); // run curl curl_close ($ ch); if (! Empty ($ data) {return $ data. "[/: :) xiaojiu]";} else {return $ this-> simsim ($ keyword ). "[simsim/: D]" ;}}
Vi. Test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.