我知道有一个个人制作的库(https://github.com/tgallice/wit-php)。但是,我找不到他如何格式化卷发。我只想做一个请求,因此使用他的图书馆会过大。
这是在终端中工作的字符串,但我不确定如何在php中写入: curl -H 'Authorization: Bearer ACCESSCODE' 'https://api.wit.ai/message?v=20160526&q=mycarisbroken'
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL,"https://api.wit.ai/message?v=20160526&q=my%20car%20doesnt%20work");
curl_setopt($ch1, CURLOPT_POST, 1);
// curl_setopt($ch1, CURLOPT_POSTFIELDS,$vars); //Post Fields
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
$headers = [
'Authorization: Bearer ACCESSCODEOMITTED',
];
curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch1, CURLOPT_HEADER, true);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, false);
$server_output = curl_exec ($ch1);
curl_close($ch1);
Data::$answer = json_decode($server_output)['entities']['intent'][0]['value'];
您给出的命令行将发送到远程服务器 - 但是在您的代码中,您发送了帖子。注释OUT curl_setopt($ch1, CURLOPT_POST, 1);
,您的PHP代码将完全执行命令行的作用。
您可以尝试使用此PHP库https://github.com/php-curl-class/php-curl-class。基本上,它将所有PHP卷曲功能都包含到类中。您可以在之后轻松地创建实现的模拟并编写不错的单元测试。
您的代码应该看起来像这样:
<?php
$curl = new Curl();
$curl->setHeader('Authorization', 'Bearer ACCESSCODEOMITTED');
$curl->get('htps://api.wit.ai/message?v=20160526&q=mycarisbroken');
if ($curl->error) {
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "n";
} else {
echo 'Response:' . "n";
var_dump($curl->response);
}