google-cloud-php icon indicating copy to clipboard operation
google-cloud-php copied to clipboard

How to authenticate with the new version v1 ? No documentation.

Open audas opened this issue 4 years ago • 0 comments

$speechClient = new Google\Cloud\Speech\V1\SpeechClient();
try {
    $encoding = Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding::FLAC;
    $sampleRateHertz = 44100;
    $languageCode = 'en-US';
    $config = new Google\Cloud\Speech\V1\RecognitionConfig();
    $config->setEncoding($encoding);
    $config->setSampleRateHertz($sampleRateHertz);
    $config->setLanguageCode($languageCode);
    $uri = 'gs://bucket_name/file_name.flac';
    $audio = new Google\Cloud\Speech\V1\RecognitionAudio();
    $audio->setUri($uri);
    $response = $speechClient->recognize($config, $audio);
} finally {
    $speechClient->close();
}

The linked authentication methods do not work for this.

Is there some documentation somewhere which provides a working example?

On the SPEECH page it provides an example

https://github.com/googleapis/google-cloud-php/tree/master/Speech

It tells us to see the Authentication guide to authenticate - that authentication guide absolutely DOES NOT WORK for this example when outside the Google Cloud.

So how on earth are we meant to do this. Just PROVIDE SOME EXAMPLES !

Whats even WORSE is that it tells us to use this example - however it throws an error.

use Google\Cloud\Speech\SpeechClient;

$speech = new SpeechClient([
    'languageCode' => 'en-US'
]);

Fatal error: Uncaught Error: Call to undefined method Google\Cloud\Speech\SpeechClient::recognizeAudioStream()

Above error comes from this line which is the authentication method.

$responses = $speechClient->recognizeAudioStream($config, $audioResource);

use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
use Google\Cloud\Speech\V1\RecognitionConfig;
use Google\Cloud\Speech\V1\StreamingRecognitionConfig;

$recognitionConfig = new RecognitionConfig();
$recognitionConfig->setEncoding(AudioEncoding::FLAC);
$recognitionConfig->setSampleRateHertz(44100);
$recognitionConfig->setLanguageCode('en-US');
$config = new StreamingRecognitionConfig();
$config->setConfig($recognitionConfig);

$audioResource = fopen('path/to/audio.flac', 'r');

$responses = $speechClient->recognizeAudioStream($config, $audioResource);

foreach ($responses as $element) {
    // doSomethingWith($element);
}

audas avatar Dec 04 '21 22:12 audas