2

I am quite experienced in PHP but I've always had troubles with connection between servers like "post". I have a FLAC audio file that I need to post to Google's Speech Recognition API server. I don't know neither how to "listen" to its response. I would like a script like that, assuming that this kind of function exists :

<?php

   $fileId = $_GET['fileId'];
   $filepath = $fileId . ".flac";
   recognize($filepath);

  function recognize($pathToFile) {

      //It's the following function that I'm looking for 
      $response = $pathToFile->post("http://www.google.com/speech-api/v1/.....&client=chromium"); 

      //The $response would be the short JSON that Google feed back.
      echo $response;

  }

?>

EDIT

I've followed a tutorial to create a Shell Script that posts my FLAC file using Wget --post. I would like to post like this, but in PHP. Also, at the end of the command, there is this > answer.ret line, so that Google's answer would be written to this file. I was wondering if there was an alternate method to it in PHP. Here's the command line :

wget -q -U "Mozilla/5.0" --post-file audio1.flac --header="Content-Type: audio/x-flac; rate=16000" -O - "http://www.google.com/speech-api/v1/recognize?lang=fr-fr&client=chromium" > trancription1.ret

EDIT 2

I figured out how to do it, with @hakre 's answer and baked up a little Gist for curious people. Here it is: https://gist.github.com/chlkbumper/4969389. Don't forget that the FLAC file must be a 16k bitrate FLAC

mrcendre
  • 1,053
  • 2
  • 15
  • 28
  • What have you tried? That's clearly pseudocode. What hasn't worked? What research have you done to solve this problem? – Charles Dec 23 '12 at 01:03
  • 1
    Try [cURL](http://php.net/manual/en/book.curl.php): "[Execute a HTTP POST Using PHP CURL](http://davidwalsh.name/curl-post)" – ᴍᴇʜᴏᴠ Dec 23 '12 at 01:06
  • 1
    It seems that [Google's speech recognition API **is not a public API**](http://stackoverflow.com/q/12721436/168868). You *should not* try using it until it's been released and documented. – Charles Dec 23 '12 at 01:08
  • Google Voice Recognition accepts FLAC? – Brad Dec 23 '12 at 01:42
  • I've followed a tutorial to create a Shell Script that _posts_ the FLAC file using _Wget --post_. At the end of the command, theres this `> answer.ret` line, so that Google's answer would be written to this file. I was wondering if there was an alternate method to it in PHP. @Charles It's not a public API, but a lot of programs use it so I guess I can to... But you're right, Google should probably document it. @Brad Yes, It accepts the FLAC, and I think it takes the SPEEX to, but it's harder to convert. Well I have not tried. – mrcendre Dec 23 '12 at 10:19
  • I am trying to do this on android lets see how it goes – Josh Nov 17 '15 at 20:19

1 Answers1

1

A POST request is just a standard HTTP request, just with the POST method specified. The rest of the HTTP Request and HTTP Response is pretty much the same.

You get the response of a request in form of a HTTP Response btw.. It is absolutely normaltiv defined in RFC 2616 - just relate to this document and it explains everything.

A function in PHP to send HTTP requests is file_get_contents, it returns the requests response. This is done via the HTTP stream wrapper that offers some options you need to send a POST request (default is GET). See HTTP context options.

Another popular PHP extension for sending HTTP requests are the Curl bindings.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Thanks, it helped a lot ! If someone else is looking for setting up a PHP script using Google Speech Recognition API, here's a [link](http://cranklin.wordpress.com/2012/01/13/building-my-own-siri-jarvis/) that illustrate @hakre 's answer (cURL requests). Thanks again ! – mrcendre Dec 23 '12 at 13:48