Little Yellow Chicken API developed by WeChat public platform

Y2J
Release: 2017-05-09 09:33:22
Original
3937 people have browsed it

1. Introduction

#The previous article introduced the development of the translation function of WeChat public platform, which realizes Chinese, English, Japanese language translation 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 official website of Little Yellow Chicken (www. The API provided by simsimi.com/), combined with crawling the webpage of Xiaojiu robot (www.xiaojo.com/), complements 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: developer.simsimi.com/api

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

The free version is used for testing here, the paid version is similar , only the URL address is different.

3.2 Request example and parameter description

Request example:

sandbox.api.simsimi.com/request.p?key=your_trial_key&lc=en&ft=1.0&text=hi
Copy after login

Parameter description:

key: API Key# applied for

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

ft: Whether to set filtering Device,

0.0: Unfiltered (including curses, sexual content);

1.0: Filter uncivilized words (only Korean is supported for the time being)

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 : The message ID of the reply (this item will only be available if result=100)


response: The message id of the reply (this item will be available only if result=100)

msg : The status corresponding to the execution result return code

4. Obtain the Little Yellow Chicken API Key##4.1 Register simsimi account

URL: developer.simsimi.com/signUp

##4.2 Activate account

4.3 Obtain API Key

##5. Specific implementation

5.1 Call Xiaohuangji API implementation

Call simsim($keyword) function processing and replace "Your API Key" into 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;
            }
        }
    }
Copy after login

Explanation:

Because sometimes the little yellow chicken does not reply, a judgment is added to the simsim() function. If $ If response is not empty, $response is returned; if $response is empty, a small code is added to allow it to randomly reply with a custom 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:

    //小九机器人
    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;
            }
        }
    }
Copy after login
5.3 Double Dragon Playing with Phoenix

We can also integrate the above little yellow chicken and Xiaojiu robot. 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]";
        }
    }
Copy after login

6. Test

[Related recommendations]

1.

WeChat public account platform Source code download


2.

Alizi order system source code free download

The above is the detailed content of Little Yellow Chicken API developed by WeChat public platform. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!