2017-05-31 84 views
0

我當時希望有人能幫我弄清楚如何讓谷歌的語音文本API運行。PHP - 谷歌語音API:未找到'Google Cloud Speech SpeechClient'類

https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/api

我試圖運行的特定代碼:SRC /功能/ transcribe_sync.php

與線加在最後調用函數:

transcribe_sync("mono.flac", 'en-US'); 

它找不到依賴關係。我一直在和作曲家一起玩,但一直沒有弄明白。

繼承人的錯誤:

php transcribe_sync.php 
PHP Fatal error: Uncaught Error: Class 'Google\Cloud\Speech\SpeechClient' not found in /home/bhaag/Documents/php-docs-samples-master/speech/api/src/functions/transcribe_sync.php:45 
Stack trace: 
#0 /home/bhaag/Documents/php-docs-samples-master/speech/api/src/functions/transcribe_sync.php(55): Google\Cloud\Samples\Speech\transcribe_sync('mono.flac', 'en-US') 
#1 {main} 
    thrown in /home/bhaag/Documents/php-docs-samples-master/speech/api/src/functions/transcribe_sync.php on line 45 

繼承人我composer.json文件:

{ 
    "require": { 
     "google/cloud-speech": "^0.2", 
     "google/cloud-storage": "^1.0", 
     "google/gax": "^0.8.1", 
     "google/proto-client-php": "^0.10.0", 
     "symfony/console": "^3.0", 
     "php-ffmpeg/php-ffmpeg": "^0.9.3" 
    }, 
    "autoload": { 
     "psr-4": { 
      "Google\\Cloud\\Samples\\Speech\\": "src/" 
     }, 
     "files": [ 
      "src/functions/streaming_recognize.php", 
      "src/functions/transcribe_async.php", 
      "src/functions/transcribe_async_gcs.php", 
      "src/functions/transcribe_sync.php", 
      "src/functions/transcribe_sync_gcs.php" 
     ] 
    }, 
    "require-dev": { 
     "phpunit/phpunit": "~4" 
    } 
} 

我已經能夠得到運行,所以我知道我所有的憑據設置全語音樣本但是對於完整樣本(主目錄中的speech.php)來說,它看起來真的很神祕。

我該怎麼做呢?謝謝你的幫助!

回答

0

樣本的構建方式是通過根文件中的speech.php文件運行。要直接運行transcribe_sync.php,則需要包括作曲家自動加載磁帶機:

require_once "/path/to/vendor/autoload.php"; 

與路徑是否已安裝在作曲家的依賴,其中更換/path/to

另外,(我建議使用此選項),沒有任何改變的樣例代碼運行,在命令行中運行以下命令:

php speech.php transcribe mono.flac 
+0

哦謝謝!這確實解決了依賴性問題,但它仍然給我一個錯誤: PHP解析錯誤:語法錯誤,意外的'命名空間'(T_NAMESPACE)在/ home/bhaag/Documents/php-docs-samples-master/speech/api/transcribe_sync .php 28行 感謝您的幫助! –

+0

我在<?php之後的第一行添加了「require_once」/path/to/vendor/autoload.php「」,是嗎? –

+0

一定要在該行的末尾添加分號。我已經更新了我的答案以證明這一點。 – jdp