Search code examples
phpgoogle-cloud-platformgoogle-speech-api

Can I get the full result of Google's speech to text transcription as JSON from the php library?


I'm using google's php api (https://github.com/googleapis/google-cloud-php) for speech to text transcription and am getting everything to work so far. However; all the examples on using the php library show the results being handled like this:

if ($op->operationSucceeded()) {
  $response = $op->getResult();

  // each result is for a consecutive portion of the audio. iterate
  // through them to get the transcripts for the entire audio file.
  foreach ($response->getResults() as $result) {
    $alternatives = $result->getAlternatives();
    $mostLikely = $alternatives[0];
    $transcript = $mostLikely->getTranscript();
    $confidence = $mostLikely->getConfidence();
    printf('Transcript: %s' . PHP_EOL, $transcript);
    printf('Confidence: %s' . PHP_EOL, $confidence);
  }
}

I would really like the full result as json so I can easily store it in a database table. Is there a way to get the full result returned as json?

Thanks!


Solution

  • You can call serializeToJsonString() on any object inheriting from Google\Protobuf\Internal\Message. Make sure you're using a relatively recent release of google/cloud.

    Additionally, if you're only using Cloud Speech, google/cloud-speech might be better, as it'll install a much smaller package.