Search code examples
phplaravelgoogle-cloud-platformspeech-to-text

Laravel 8 (PHP) & Google Cloud Speech API: Streaming calls are not supported while using the REST transport.)


Wassup Guys,

I've managed to install the Google Cloud Speech Api and use my .json file as credentials for the speech client.

Now I'm getting the "Streaming calls are not supported while using the REST transport." error.

Did I miss something? Below is the method of my controller. AudioController.php

<?php

namespace App\Http\Controllers;


use Illuminate\Http\Request;

use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
use Google\Cloud\Speech\V1\RecognitionConfig;
use Google\Cloud\Speech\V1\StreamingRecognitionConfig;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\Response;
use Google\Cloud\Speech\V1\SpeechClient;


class AudioController extends Controller
{
    public function getData(Request $request)
    {
        //$url = Storage::download('public/track.mp3');
        $url = asset('storage/test.flac');

        //$file = Storage::disk('public')->get('track.mp3');
        //$download = new Response($file, 200);


        // dd($request->all(), $url);
        return view('welcome')->with('downloadLink', $url);
    }

    public function transcribedText(Request $request)
    {

        $recognitionConfig = new RecognitionConfig();
        $recognitionConfig->setEncoding(AudioEncoding::FLAC);
        $recognitionConfig->setSampleRateHertz(44100);
        $recognitionConfig->setLanguageCode('en-US');
        $config = new StreamingRecognitionConfig();
        $config->setConfig($recognitionConfig);
        $auth = Storage::disk('public')->get('auth.json');
        $test = Storage::path('public/auth.json');
        //dd(file_get_contents($test, true));
        //dd(json_decode($auth));
        //putenv('GOOGLE_APPLICATION_CREDENTIALS='.$auth);
        $speechClient = new SpeechClient([
            'credentials' => Storage::path('public/auth.json'),
        ]);
        $file = Storage::disk('public')->get('test.flac');


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

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

        foreach ($responses as $element) {
            dd($element);
        }
    }
}

Solution

  • Make sure gRPC for PHP is installed and enabled.

    Check your php.ini file for:

    extension=grpc.so - Linux.

    extension=php_grpc.dll - Windows.

    Add the composer dependency:

    composer require "grpc/grpc:^1.38"
    

    Install gRPC for PHP