title: Using Google Text-To-Speech API to save speech audio
body: <p>I am trying to implement methods discussed in this <a href="https://stackoverflow.com/questions/9893175/google-text-to-speech-api/31791632#31791632">question</a> to write a php function that downloads an audio file for a given string, but I can't seem to get around google's abuse protection. Results are sporadic, sometimes I get an audio file and other times it's an empty 2KB mp3 due to a response with "Our systems have detected unusual traffic from your computer network". Here is what I've got so far ( note the $file has a location in my code but for the purposes of this I've omitted it ) :</p>
  
  <pre><code>function downloadMP3( $url, $file ){    
      $curl = curl_init();
  
      curl_setopt( $curl, CURLOPT_URL, $url );
      curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
      curl_setopt( $curl, CURLOPT_REFERER, 'http://translate.google.com/' );
      curl_setopt( $curl, CURLOPT_USERAGENT, 'stagefright/1.2 (Linux;Android 5.0)' );
  
      $output = curl_exec( $curl );    
  
      curl_close( $curl );
  
      if( $output === false ) { 
          return false;
      }
  
      $fp = fopen( $file, 'wb' );
      fwrite( $fp, $output );
      fclose( $fp );
  
      return true;
  }
  
  $word = "Test";
  
  $file  = md5( $word ) . '.mp3';
  
  if ( !file_exists( $file ) ) {
      $url = 'http://translate.google.com/translate_tts?q=' . $word . '&amp;tl=en&amp;client=t';
      downloadMP3( $url, $file );
  }
  </code></pre>