美好的一天,我一直有严重的问题,成功地从我的PHP功能返回值到我的Facebook信使聊天机器人。我打算让聊天机器人通过向我的机器人传达这个词,让人们找到文字的含义。 问题是api我使用'wordsapi'你可以查找它,返回一个json,里面有很多定义。我希望能够遍历定义并将值返回到我的机器人。即使经过广泛的尝试,我发现这很难,我将非常感谢你的帮助。 以下是机器人源代码。
webhook.php
<?php
include 'Unirest.php';
/* validate verify token needed for setting up web hook */
if (isset($_GET['hub_verify_token'])) {
if ($_GET['hub_verify_token'] === 'THIS_IS_REDACTED_FOR_SECURITY_PURPOSES') {
echo $_GET['hub_challenge'];
return;
} else {
echo 'Invalid Verify Token';
return;
}
}
/* receive and send messages */
$input = json_decode(file_get_contents('php://input'), true);
$sender_id = $input['entry'][0]['messaging'][0]['sender']['id'];
$messaging_array = $input['entry'][0]['messaging'][0];
$message_text = $input['entry'][0]['messaging'][0]['message']['text'];
$msgarray = preg_split('/\s+/', $message_text);
$response = '';
if(isset($messaging_array)){
$istyping = '{
"recipient":{
"id":"'.$sender_id.'"
},
"sender_action":"typing_on"
}';
istyping($message_text,$istyping);
if(strpos($message_text, 'hi') !== false){
$answer = 'Hello this is Nwachukwu\'s bot how may i help you today?';
sendTextMessage($sender_id,$message_text,$answer);
}
elseif($msgarray[0] === 'dict'){
$word = str_replace_once('dict ', '', $message_text);
$answer = dictSearch($word);
sendTextMessage($sender_id,$message_text,$answer);
}
else{
$answer = 'Nwachukwu is still constructing me. I am not yet intelligent enough to understand that. Check back later.';
sendTextMessage($sender_id,$message_text,$answer);
}
}
function str_replace_once($needle, $replace, $haystack){
$pos = strpos($haystack, $needle);
if ($pos === false) {
return $haystack;
}
return substr_replace($haystack, $replace, $pos, strlen($needle));
}
function sendTextMessage($sender_id,$message_text,$answer){
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token='THIS_IS_REDACTED_FOR_SECURITY_PURPOSES';
/*initialize curl*/
$response = '{
"recipient":{
"id":"'.$sender_id.'"
},
"message":{
"text":"'.$answer.'"
}
}';
$ch = curl_init($url);
/* curl setting to send a json post data */
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $response);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (!empty($message_text)) {
curl_exec($ch); // user will get the message
$errors = curl_error($ch);
$resp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($errors);
var_dump($resp);
curl_close($ch);
}
}
function istyping($message_text,$istyping){
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token='THIS_IS_REDACTED_FOR_SECURITY_PURPOSES';
/*initialize curl*/
$ch = curl_init($url);
/* curl setting to send a json post data */
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $istyping);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (!empty($message_text)) {
curl_exec($ch); // user will get the message
$errors = curl_error($ch);
$resp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($errors);
var_dump($resp);
curl_close($ch);
}
}
function dictSearch($word){
$s = $word;
$urll = "https://wordsapiv1.p.mashape.com/words/".$s;
$response = Unirest\Request::get($urll,
array(
"X-Mashape-Key" => "THIS_IS_REDACTED_FOR_SECURITY_PURPOSES",
"Accept" => "application/json"
)
);
$resp = ($response->raw_body);
$input = json_decode($resp);
$answer = '';
$error = 'No definition for that word';
foreach ($input->results as $result){
foreach ($result as $value=>$key){
if ($value === 'definition'){
$answer[] = $key;
}
}
}
return $answer;
}
?>
来自wordsapi的json响应示例
{
"word": "rat",
"results": [
{
"definition": "someone who works (or provides workers) during a strike",
"partOfSpeech": "noun",
"synonyms": [
"blackleg",
"scab",
"strikebreaker"
],
"typeOf": [
"worker"
]
},
{
"definition": "a person who is deemed to be despicable or contemptible",
"partOfSpeech": "noun",
"synonyms": [
"bum",
"crumb",
"dirty dog",
"git",
"lowlife",
"puke",
"rotter",
"scum bag",
"skunk",
"so-and-so",
"stinker",
"stinkpot"
],
"typeOf": [
"unpleasant person",
"disagreeable person"
],
"examples": [
"kill the rat"
]
},
{
"definition": "take the place of work of someone on strike",
"partOfSpeech": "verb",
"synonyms": [
"blackleg",
"fink",
"scab"
],
"typeOf": [
"do work",
"work"
],
"derivation": [
"ratter"
]
},
{
"definition": "give away information about somebody",
"partOfSpeech": "verb",
"synonyms": [
"betray",
"denounce",
"give away",
"grass",
"shit",
"shop",
"snitch",
"stag",
"tell on"
],
"typeOf": [
"inform"
],
"hasTypes": [
"sell someone out"
],
"derivation": [
"ratting",
"ratter"
]
},
{
"definition": "one who reveals confidential information in return for money",
"partOfSpeech": "noun",
"synonyms": [
"betrayer",
"blabber",
"informer",
"squealer"
],
"typeOf": [
"source",
"informant"
],
"hasTypes": [
"nark",
"grass",
"fink",
"copper's nark",
"canary",
"sneak",
"sneaker",
"snitch",
"snitcher",
"stool pigeon",
"stoolie",
"stoolpigeon",
"supergrass"
]
},
{
"definition": "any of various long-tailed rodents similar to but larger than a mouse",
"partOfSpeech": "noun",
"typeOf": [
"rodent",
"gnawer"
],
"hasTypes": [
"oryzomys palustris",
"mole rat",
"norway rat",
"brown rat",
"rice rat",
"rattus rattus",
"rattus norvegicus",
"roof rat",
"bandicoot rat",
"black rat",
"pocket rat",
"jerboa rat"
],
"derivation": [
"ratty"
]
},
{
"definition": "a pad (usually made of hair) worn as part of a woman's coiffure",
"partOfSpeech": "noun",
"typeOf": [
"pad"
],
"partOf": [
"hairdo",
"coif",
"coiffure",
"hair style",
"hairstyle"
]
},
{
"definition": "catch rats, especially with dogs",
"partOfSpeech": "verb",
"typeOf": [
"capture",
"catch"
],
"derivation": [
"ratter"
]
},
{
"definition": "desert one's party or group of friends, for example, for one's personal advantage",
"partOfSpeech": "verb",
"typeOf": [
"desert",
"defect"
],
"derivation": [
"ratter"
]
},
{
"definition": "employ scabs or strike breakers in",
"partOfSpeech": "verb",
"inCategory": [
"manufacture",
"industry"
],
"typeOf": [
"hire",
"engage",
"employ"
],
"derivation": [
"ratter"
]
},
{
"definition": "give (hair) the appearance of being fuller by using a rat",
"partOfSpeech": "verb",
"typeOf": [
"fill out",
"pad"
]
}
],
"syllables": {
"count": 1,
"list": [
"rat"
]
},
"pronunciation": {
"all": "ræt"
},
"frequency": 4.49
}
我想迭代数组并返回搜索到的单词的定义。