├── .gitignore ├── GoogleSpeechToText.php ├── README.md └── sample ├── example.php └── quick.flac /.gitignore: -------------------------------------------------------------------------------- 1 | personal-example.php -------------------------------------------------------------------------------- /GoogleSpeechToText.php: -------------------------------------------------------------------------------- 1 | apiKey = $apiKey; 60 | $this->requestPair = $this->getPair(); 61 | $this->setupCurl(); 62 | } 63 | 64 | /** 65 | * Setup CURL requests, both up and down. 66 | */ 67 | private function setupCurl() 68 | { 69 | $this->uploadHandle = curl_init(); 70 | $this->downloadHandle = curl_init(); 71 | curl_setopt( 72 | $this->downloadHandle, 73 | CURLOPT_URL, 74 | self::SPEECH_BASE_URL . 'down?pair=' . $this->requestPair 75 | ); 76 | 77 | curl_setopt( 78 | $this->downloadHandle, 79 | CURLOPT_RETURNTRANSFER, 80 | true 81 | ); 82 | 83 | curl_setopt( 84 | $this->uploadHandle, 85 | CURLOPT_RETURNTRANSFER, 86 | true 87 | ); 88 | 89 | curl_setopt( 90 | $this->uploadHandle, 91 | CURLOPT_POST, 92 | true 93 | ); 94 | } 95 | 96 | /** 97 | * Generate a Pair for the request. This identifies the requests later. 98 | * 99 | * @return string 100 | */ 101 | private function getPair() 102 | { 103 | $c = '0123456789'; 104 | $s = ''; 105 | for ($i=0; $i<16; $i++) { 106 | $s .= $c[rand(0, strlen($c) - 1)]; 107 | } 108 | 109 | return $s; 110 | } 111 | 112 | /** 113 | * Make the request, returning either an array, or boolean false on 114 | * failure. 115 | * 116 | * @param string $file the file name to process 117 | * @param integer $rate the bitrate of the flac content (example: 44100) 118 | * @param string $language the ISO language code 119 | * (en-US has been confirmed as working) 120 | * @throws Exception 121 | * @return array|boolean false for failure. 122 | */ 123 | public function process($file, $rate, $language = 'en-US') 124 | { 125 | if (!$file || !file_exists($file) || !is_readable($file)) { 126 | throw new Exception( 127 | '$file must be specified and be a valid location.' 128 | ); 129 | } 130 | 131 | $data = file_get_contents($file); 132 | if (!$data) { 133 | throw new Exception('Unable to read ' . $file); 134 | } 135 | 136 | if (empty($rate) || !is_integer($rate)) { 137 | throw new Exception('$rate must be specified and be an integer'); 138 | } 139 | 140 | curl_setopt( 141 | $this->uploadHandle, 142 | CURLOPT_URL, 143 | self::SPEECH_BASE_URL . 'up?lang=' . 144 | $language . '&lm=dictation&client=chromium&pair=' . 145 | $this->requestPair . '&key=' . $this->apiKey 146 | ); 147 | curl_setopt( 148 | $this->uploadHandle, 149 | CURLOPT_HTTPHEADER, 150 | array( 151 | 'Transfer-Encoding: chunked', 152 | 'Content-Type: audio/x-flac; rate=' . $rate 153 | ) 154 | ); 155 | curl_setopt( 156 | $this->uploadHandle, 157 | CURLOPT_POSTFIELDS, 158 | array( 159 | 'file' => $data 160 | ) 161 | ); 162 | 163 | $curlMulti = curl_multi_init(); 164 | 165 | curl_multi_add_handle($curlMulti, $this->downloadHandle); 166 | curl_multi_add_handle($curlMulti, $this->uploadHandle); 167 | 168 | $active = null; 169 | do { 170 | curl_multi_exec($curlMulti, $active); 171 | } while ($active > 0); 172 | 173 | $res = curl_multi_getcontent($this->downloadHandle); 174 | 175 | $output = array(); 176 | $results = explode("\n", $res); 177 | foreach ($results as $result) { 178 | $object = json_decode($result, true); 179 | if ( 180 | (isset($object['result']) == true) && 181 | (count($object['result']) > 0) 182 | ) { 183 | foreach ($object['result'] as $obj) { 184 | $output[] = $obj; 185 | } 186 | } 187 | } 188 | 189 | curl_multi_remove_handle($curlMulti, $this->downloadHandle); 190 | curl_multi_remove_handle($curlMulti, $this->uploadHandle); 191 | curl_multi_close($curlMulti); 192 | 193 | if (empty($output)) { 194 | return false; 195 | } 196 | 197 | return $output; 198 | } 199 | 200 | /** 201 | * Close any outstanding connections in the destruct 202 | */ 203 | public function __destruct() 204 | { 205 | curl_close($this->uploadHandle); 206 | curl_close($this->downloadHandle); 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP Speech To Text # 2 | 3 | ### About ### 4 | 5 | This is an example file to demonstrate how to interact with the Google Speech API. 6 | 7 | ### Getting Started ### 8 | 9 | Visit: https://code.google.com/apis/console/ 10 | Enable the Speech API listed on the page. 11 | 12 | #### Don't see Speech API? #### 13 | 14 | That's a fairly common thing. It seems that only developers on the `chromium-dev` list actually can see it listed. 15 | 16 | Don't worry. Go to: http://groups.google.com/a/chromium.org/group/chromium-dev/topics 17 | 18 | Now, sign up for the list. 19 | 20 | Go back to the API Console and refresh the page. You'll see it listed. 21 | 22 | ### Usage ### 23 | 24 | See the `sample/example.php` file, or use this below: 25 | 26 | ```php 27 | process($file, $bitRate, 'en-US'); 36 | var_dump($result); 37 | ``` 38 | 39 | Running the example file outputs: 40 | 41 | ```text 42 | array(1) { 43 | [0]=> 44 | array(2) { 45 | ["alternative"]=> 46 | array(5) { 47 | [0]=> 48 | array(2) { 49 | ["transcript"]=> 50 | string(44) "the quick brown fox jumped over the lazy dog" 51 | ["confidence"]=> 52 | float(0.87096781) 53 | } 54 | [1]=> 55 | array(1) { 56 | ["transcript"]=> 57 | string(43) "the quick brown fox jumps over the lazy dog" 58 | } 59 | [2]=> 60 | array(1) { 61 | ["transcript"]=> 62 | string(49) "the quick brown fox jumped over the lazy dog cafe" 63 | } 64 | [3]=> 65 | array(1) { 66 | ["transcript"]=> 67 | string(49) "the quick brown fox jumped over the lazy dog food" 68 | } 69 | [4]=> 70 | array(1) { 71 | ["transcript"]=> 72 | string(47) "the quick brown fox jumped over the lazy dog hi" 73 | } 74 | } 75 | ["final"]=> 76 | bool(true) 77 | } 78 | } 79 | 80 | ``` 81 | -------------------------------------------------------------------------------- /sample/example.php: -------------------------------------------------------------------------------- 1 | process($file, $bitRate, 'en-US'); 10 | var_dump($result); -------------------------------------------------------------------------------- /sample/quick.flac: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerthomas84/php-speech-to-text/5f37c4d7a2bf7a0b40f0e1e568cbaa23ab2a2c60/sample/quick.flac --------------------------------------------------------------------------------