2016-07-29 48 views
0

我沒有找到解決方案,我在我的代碼基礎上找到了其他代碼,我在網絡http://zakilive.com/tag/google-cloud-messaging-php-tutorial/中找到了,因爲Google不存在樣品爲PHP,我有下一個代碼:我在Google Cloud Natural Language API中遇到了這個錯誤:The AnnotateTextRequest.features在PHP中爲空

<?php 
 

 

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

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

 

 
$headers = array 
 
(
 
\t 'Authorization: Bearer ' . API_ACCESS_KEY, 
 
\t '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;

完全緩解是: { 「錯誤」:{ 「代碼」:400, 「消息」:「的AnnotateTextRequest.features是空的。「,」狀態「:」INVALID_ARGUMENT「,」詳情「:[{」@type「:」type.googleapis.com/google.rpc.Ba dRequest「,」fieldViolations「:[{」field「:」AnnotateTextRequest.features「,」description「:」沒有指定的功能。「 }]}]}}

我仍然無法在其他語言的代碼。

幫助我,請

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

回答

1

我解決我的代碼,我缺乏的「特色」,我看到的頁面:https://cloud.google.com/natural-language/reference/rest/v1beta1/documents/annotateTexthttps://cloud.google.com/natural-language/reference/rest/v1beta1/documents/annotateText#Features和我的酒吧在我的代碼中$味精VAR:

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

,現在I H一個很好的迴應,但現在我的問題是我怎麼能生成一個API_ACCESS_KEY automaticaly,但我想這是其他問題。

+0

你讓它工作。我總是收到這個錯誤「請求無法用客戶端項目識別,請傳遞一個有效的API密鑰和請求。」 –

+0

FYI [此項目](https://github.com/darrynten/google-natural-language-php)在使用此API時可能會很有用 –

相關問題