我在Google Cloud Natural Language API中出现此错误:PHP

时间:2016-07-29 15:12:50

标签: php google-cloud-platform

我没有找到解决方案,我的代码基于我在网络http://zakilive.com/tag/google-cloud-messaging-php-tutorial/中找到的其他代码,因为在Google中不存在php的示例,我有下一个代码:

<?php


define( "API_ACCESS_KEY", "my api key" );

$msg = array
(
	'document' 	=> array(
	
		'type'=>'PLAIN_TEXT',
		'content'=>"Michelangelo Caravaggio, Italian painter, is known for
              'The Calling of Saint Matthew'."
	
	),
	'encodingType'=>'UTF8',
);


$headers = array
(
	'Authorization: Bearer ' . API_ACCESS_KEY,
	'Content-Type: application/json'
);
 
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://language.googleapis.com/v1beta1/documents:annotateText' );

curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $msg ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;

完整的回复是: {“error”:{“code”:400,“message”:“AnnotateTextRequest.features为空。”,“status”:“INVALID_ARGUMENT”,“details”:[{“@ type”:“type.googleapis。 com / google.rpc.BadRequest“,”fieldViolations“:[{”field“:”AnnotateTextRequest.features“,”description“:”未指定任何功能。“ }]}]}}

我还是不能用其他语言编写代码。

请帮帮我

API为:https://cloud.google.com/natural-language/docs/

1 个答案:

答案 0 :(得分:2)

我修复了我的代码,我缺少“功能”,我在页面中看到:https://cloud.google.com/natural-language/reference/rest/v1beta1/documents/annotateTexthttps://cloud.google.com/natural-language/reference/rest/v1beta1/documents/annotateText#Features我在我的代码中发布$ msg var:

$msg = array
(
	'document' 	=> array(
	
		'type'=>'PLAIN_TEXT',
		'content'=>"Michelangelo Caravaggio, Italian painter, is known for
              'The Calling of Saint Matthew'."
	
	),
	'features'=>array(
	"extractSyntax"=> true,
  "extractEntities"=> false,
  "extractDocumentSentiment"=> false
	),
	'encodingType'=>'UTF8',
	
);

现在我有一个很好的回复,但现在我的问题是如何自动生成API_ACCESS_KEY,但我想这是另一个问题。