Dialogflow - POST /实体 - 不工作 - PHP和JSON

时间:2018-01-30 15:18:21

标签: php json dialogflow

我有一个Dialogflow的项目。 我想使用PHP和JSON更新我的Entites。我找到了以下页面(1),但不幸的是它并没有真正帮助我。我认为我的错误是网址中的数字,但不幸的是我不知道要插入哪一个..

我的代码是:

$data = new \stdClass();

$data->entries[] = array('synonyms' => array('Apfel'), 'value' => 'Apfel');
$data->entries[] = array('synonyms' => array('Salami'), 'value' => 'Salami');
$data->name = 'Zutaten';
//echo json_encode($data);


$url = 'https://api.dialogflow.com/v1/entities?v=20150910';
$content = json_encode($data);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: Bearer 9debcXXXXXXXXXXXXXXXXXXXXXX"));
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ( $status != 201 ) {
    die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}


curl_close($curl);

$response = json_decode($json_response, true);

有没有人知道我最好能改变什么,所以这有效吗? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

我自己来找解决方案。非常感谢所有帮助过的人。

解决方案如下: 我不得不退出这两个命令:

curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: Bearer 9debXXXXXXXXXXX"));
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));

发出此命令:

curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: Bearer 9debc126XXXXXXXXX\r\nContent-type: application/json"));