title: Google Cloud Speech API using php
body: <p>I am trying to call Google Cloud Speech API by using PHP and got a problem.</p>
  
  <pre><code>$stturl = "https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=xxxxxxxxxxxx";
  $upload = file_get_contents("1.wav");
  $upload = base64_encode($upload);
  
  $data = array(
      "config"    =&gt;  array(
          "encoding"      =&gt;  "LINEAR16",
          "sampleRate"    =&gt;  16000,
          "languageCode"  =&gt;  "en-US"
      ),
      "audio"     =&gt;  array(
          "Content"       =&gt;  $upload,
      )
  );
  
  $jsonData = json_encode($data);
  
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $stturl);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
  
  $result = curl_exec($ch);
  </code></pre>
  
  <p>The result says that it is INVALID JSON PAYLOAD.</p>
  
  <blockquote>
    <p>{ "error": { "code": 400, "message": "Invalid JSON payload received.
    Unknown name \"content\" at 'audio': Cannot find field.", "status":
    "INVALID_ARGUMENT", "details": [ { "@type":
    "type.googleapis.com/google.rpc.BadRequest", "fieldViolations": [ {
    "field": "audio", "description": "Invalid JSON payload received.
    Unknown name \"content\" at 'audio': Cannot find field." } ] } ] } } "</p>
  </blockquote>
  
  <p>I think this is because $upload isn't configured correctly.
  According Google Cloud Speech API, it should be "A base64-encoded string".
  <a href="https://cloud.google.com/speech/reference/rest/v1beta1/RecognitionAudio" rel="nofollow">https://cloud.google.com/speech/reference/rest/v1beta1/RecognitionAudio</a></p>
  
  <p>That's why I used <strong>base64_encode</strong> function, but it seems JSON doesn't process this value correctly.
  Any thoughts?</p>