Record audio from Google Chrome and send to Cloud Speech API
<p>I've successfully sent an RAW audio file to Google's Cloud Speech API using my API key. Now, I would like to record my voice using Chrome's browser and send it, instead of "audio.raw" (just like on Google.com). I'm guessing that it should be an HTML5 script.</p>

<p><em>Here's what I've done so far:</em></p>

<pre><code>&lt;?php
$data = json_encode(array(
    'config' =&gt; array(
        'encoding' =&gt; 'LINEAR16',
        'sample_rate' =&gt; 16000,
        'language_code' =&gt; 'en-US'
    ),
    'audio' =&gt; array(
        'content' =&gt; base64_encode(file_get_contents(dirname(__FILE__) . '/audio.raw'))
    )
));

$ch = curl_init('https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=XXX');

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data)));

$result = json_decode(curl_exec($ch));

$text = (isset($result-&gt;results[0]-&gt;alternatives[0]-&gt;transcript) ? $result-&gt;results[0]-&gt;alternatives[0]-&gt;transcript : '');

echo $text;
?&gt;
</code></pre>