Sunday, 15 July 2012

php - CURLOPT_POSTFIELDS array of folder content -



php - CURLOPT_POSTFIELDS array of folder content -

i'm making curl post google text speech. have set of .flac files, want send google text speech service, in order have content wrote in txt file.

this code wrote , works:

$url = 'https://www.google.com/speech-api/v2/recognize?output=json&lang=it-it&key=xxx'; $cont2 = array( 'flac/1.flac', 'flac/2.flac', 'flac/3.flac' ); $ch = curl_init(); curl_setopt($ch, curlopt_httpheader, array('content-type: audio/x-flac; rate=44100')); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_post, true); curl_setopt($ch, curlopt_returntransfer, 1); foreach ($cont2 $fn) { curl_setopt($ch, curlopt_postfields, file_get_contents($fn)); $result = curl_exec($ch); $info = curl_getinfo($ch); //var_dump($info); if ($result === false) { die(curl_error()); }else{ echo "<br />".$fn." upload ok"."<br />"; file_put_contents("pum.txt", $result, file_append); } }

it works charm, in "pum.txt" have file content wrote , it's ok.

my problem don't want add together the array "cont2", each time, new name o files need pass to, there in "flac folder".

to avoid that, utilize "scandir" method, remove "." , ".." string array , give array curl_opt_postfield, phone call gtt homecoming empty content.

this code wrote (instead $cont2 array)

$directory = 'flac/'; $cont = array_diff(scandir($directory), array('..', '.', '.ds_store'));

print_r of same $cont2 array:

array(3) { [3]=> string(6) "1.flac" [4]=> string(6) "2.flac" [5]=> string(6) "3.flac" }

but google tts homecoming empty result.

does please tell me i'm making mistake?

kind regards

brus

edit: utilize "$cont = glob("$directory/*.flac");" solved issue. hope help others.

as marc b states need directory files missing. utilize glob homecoming need:

$cont = glob("$directory/*.flac");

php arrays curl google-text-to-speech

No comments:

Post a Comment