How in PHP to translate such a construction into a Curl query?

curl -X POST \ -H "Authorization: Bearer ${IAM_TOKEN}" \ -H "Transfer-Encoding: chunked" \ -o speech.raw \ --data-urlencode "text=Привет мир" \ -d "voice=zahar&emotion=good&folderId=${FOLDER_ID}&format=lpcm&sampleRateHertz=48000" \ https://pi.cloud.net/speech/tts:synthesize > speech.ogg 

I can not figure out how to specify - data-urlencode

I did it like this:

 $folder_id = '23232312ASdgf43'; $url_tts = 'https://pi.cloud.net/speech/tts:synthesize'; $data_tts = "voice=zahar&emotion=good&format=lpcm&sampleRateHertz=48000&folderId=$folder_id"; $authorization_cloud = "Authorization: Bearer ".$iam_token; $headers[] = $authorization_cloud; $headers[] = 'Transfer-Encoding: chunked'; $ch = curl_init($url_tts); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_tts); $result = curl_exec($ch); curl_close($ch); 

    2 answers 2

    try this

     $folder_id = '23232312ASdgf43'; $url_tts = 'https://pi.cloud.net/speech/tts:synthesize'; $authorization_cloud = "Authorization: Bearer " . $iam_token; $headers[] = $authorization_cloud; $headers[] = 'Transfer-Encoding: chunked'; $arrdata = array( 'voice' => 'zahar', 'emotion' => 'good', 'format' => 'lpcm', 'sampleRateHertz' => '48000', 'folderId' => $folder_id, 'text' => 'Привет мир', ); $ch = curl_init($url_tts); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($arrdata)); $result = curl_exec($ch); curl_close($ch); 
    • I tried. It did not work. - Dima Kuzmin
    • possible without http_build_query - Jean-Claude

    That's how it worked.

     $ch = curl_init(); $text = curl_escape($ch, $post); curl_setopt($ch, CURLOPT_URL, $url_tts); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, "voice=zahar&emotion=good&folderId=$folder_id&format=lpsm&sampleRateHertz=48000&text=$text"); $result = curl_exec($ch);