/
xiaoi.php
executable file
·34 lines (31 loc) · 1.13 KB
/
xiaoi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
function XiaoI ($openid, $content) {
//定义app
$app_key = "1i0WYqi9LCOv";
$app_secret = "JZEdu3AHsmjUADvpaEjB";
//签名算法
$realm = "xiaoi.com";
$method = "POST";
$uri = "/robot/ask.do";
$nonce = "";
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
for ($i = 0; $i < 40; $i++) {
$nonce .= $chars[mt_rand(0, strlen($chars) - 1)];
}
$HA1 = sha1($app_key.":".$realm.":".$app_secret);
$HA2 = sha1($method.":".$uri);
$sign = sha1($HA1.":".$nonce.":".$HA2);
//接口调用
$url = "http://nlp.xiaoi.com/robot/ask.do";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Auth: app_key="'.$app_key.'", nonce="'.$nonce.'", signature="'.$sign.'"'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "question=".urlencode($content)."&userId=".$openid."&platform=custom&type=0");
$output = curl_exec($ch);
if ($output === FALSE) {
return "cURL Error: ". curl_error($ch);
}
return trim($output);
}