PHP WeChat public platform development Chat robot Development _php Tutorial

Source: Internet
Author: User

"PHP Public Platform Development Series"

01. Configure the interface
02. Public Platform Sample Code analysis
03. Subscription event (SUBSCRIBE) processing
04. Simple Reply Function development
05. Development of weather forecast function
06. Translation Function Development
07. Chat Robot Function Development


This address: http://www.phpchina.com/archives/view-43393-1.html
This series by Phpchina invited author @david_tang feeds, reprint please indicate the author information and this article address.

First, Introduction

The previous article introduced the development of the translation function of the public platform, which realized the translation of Chinese, English and Japanese languages, and could be used in real life. In the next article, we will complete a more interesting feature, that is, chat robot, you can be bored when you chat with you to make you happy.

Second, the Thinking analysis

In this experiment, we will pick up the API provided by the http://www.simsimi.com/, which is supplemented by the Web pages that crawl the small nine robots (http://www.xiaojo.com/). Simsimi is charged, but you can try the 7-day Test and use 100 replies a day for free; the small nine robots can be used without restrictions, but only if the official is not shielded.

Three, small 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

Here is the free version of the test, similar to the paid version, but the URL address is different.

3.2 Sample Request and parameter description

Sample Request:

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

Parameter description:

Key: API Key for the application

Lc:language code, supported languages, Simplified Chinese with ch, Traditional Chinese en, English with en, please refer to for more details: http://developer.simsimi.com/lclist

FT: whether to set the filter,

0.0: Unfiltered (contains curse, sexual content);

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

Text: The requested literal

3.3 Return value analysis

Result: Execution results return code

      • 100-ok.
      • 400-bad Request.
      • 401-unauthorized.
      • 404-not found.
      • 500-server Error.

ID: The message ID of the reply (only if result=100 is available)

Response: Reply Message (This is only available when result=100)

msg: Execution result return code corresponding state

Four, get the small yellow chicken API Key

4.1 Register Simsimi Account

Url:http://developer.simsimi.com/signup

4.2 Activating your Account

4.3 Get API Key

V. Concrete Realization

5.1 Call small yellow chicken API implementation

Call the Simsim ($keyword) function and replace the "Your API Key" with the API key you requested.

    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);  Reads the entire file into a string $result =json_decode ($json, true);  Encode the string in JSON format//$errorCode = $result [' result '];  Debug with $response = $result [' response '];        Reply message if (!empty ($response)) {return $response;            }else{$ran =rand (1,5); Switch ($ran) {case 1:return "the chicken is tired today and will chat with you tomorrow."                    ";                Break                    Case 2:return "The Chicken Sleeps ~ ~";                Break                    Case 3:return "Whistling ~ ~ ~";                Break                    Case 4:return "You speak a lot, don't talk to You";                Break Case 5:return "Thank you for your interest in" Zhuo Jin Suzhou "." \ n "." No.Zhuojinsz "." \ n "."                    Brilliant Splendid, Bandai Immortal ";                Break Default:return "Thank you for your interest in" Zhuo Jin Suzhou ". \ n "." Number: Zhuojinsz "." \ n "."                    Brilliant Splendid, Bandai Immortal ";            Break }        }    }

Description

Because sometimes the small yellow chicken does not reply, so in the Simsim () function added a judgment, if $response is not empty, then return $response; If $response is empty, add a small code to randomly reply to the custom message, So you can do whatever you like.

5.2 Call small nine robot implementation

The small nine robot does not provide the API, so only through the PHP function to achieve the page crawl, the code is as follows:

    Small nine robot public function Xiaojo ($keyword) {$curlPost =array ("chat" + = $keyword); $ch = Curl_init ();//Initialize Curl curl_setopt ($ch, Curlopt_url, ' http://www.xiaojo.com/bot/chata.php ');//crawl specified page cur        L_setopt ($ch, Curlopt_httpheader, $header);        curl_setopt ($ch, Curlopt_header, 0);//Set HEADER curl_setopt ($ch, Curlopt_returntransfer, 1);//require the result to be a string and output to the screen        curl_setopt ($ch, Curlopt_post, 1);//post Submission Method curl_setopt ($ch, Curlopt_postfields, $curlPost);        $data = curl_exec ($ch);//Run Curl Curl_close ($ch);        if (!empty ($data)) {return $data;            }else{$ran =rand (1,5); Switch ($ran) {case 1:return "the chicken is tired today and will chat with you tomorrow."                    ";                Break                    Case 2:return "The Chicken Sleeps ~ ~";                Break                    Case 3:return "Whistling ~ ~ ~";                Break Case 4:return "You talkA lot of ah, don't talk with You ";                Break Case 5:return "Thank you for your interest in" Zhuo Jin Suzhou "." \ n "." Number: Zhuojinsz "." \ n "."                    Brilliant Splendid, Bandai Immortal ";                Break Default:return "Thank you for your interest in" Zhuo Jin Suzhou ". \ n "." Number: Zhuojinsz "." \ n "."                    Brilliant Splendid, Bandai Immortal ";            Break }        }    }

5.3 Double Dragon Play Phoenix

We can also integrate the small yellow chicken and the small nine robots, the code is as follows:

    Shuanglong Play Phoenix public    function chatter ($keyword) {        $curlPost =array ("chat" + = $keyword);        $ch = Curl_init ();    Initialize Curl        curl_setopt ($ch, Curlopt_url, ' http://www.xiaojo.com/bot/chata.php ');    Crawl specified Web page        curl_setopt ($ch, Curlopt_httpheader, $header);        curl_setopt ($ch, Curlopt_header, 0);    Set Header        curl_setopt ($ch, Curlopt_returntransfer, 1);    Requires the result to be a string and output to the screen        curl_setopt ($ch, Curlopt_post, 1);    Post Submission Method        curl_setopt ($ch, Curlopt_postfields, $curlPost);        $data = curl_exec ($ch);    Run Curl        curl_close ($ch);        if (!empty ($data)) {            return $data. " [/::) small nine] ";        } else{            return $this->simsim ($keyword). " [simsim/::D] ";        }    }

VI. Testing

http://www.bkjia.com/PHPjc/740331.html www.bkjia.com true http://www.bkjia.com/PHPjc/740331.html techarticle "PHP Public Platform Development Series" 01. Configure interface 02. Public Platform Sample Code Analysis 03. Subscription events (subscribe) processing 04. Simple reply Function Development 05. Weather Forecast Function ...

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