Let's translate CURL:
-
curl
: Starts CURL.
-
-k
: Sets to insecure mode, not verifying SSL.
-
-H "Authorization: Bearer TOKEN_DE_ACESSO"
Defines the header of Authorization
with the value of Bearer TOKEN_DE_ACESSO
.
-
-H "Accept-language: pt-BR"
: Sets the header of Accept-language
with value of pt-BR
-
"https://txttovoice.org/v/tts?v=1.1&text=Hello+world"
: Sets the website you want to connect to.
-
-o arquivo.wav
: Sets the output location of the result.
Now let's port this to PHP, in the same order:
// Inicia o CURL:
$ch = curl_init();
// Adiciona as opções:
curl_setopt_array($ch, [
// Modo inseguro:
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
// Adiciona os cabeçalhos:
CURLOPT_HTTPHEADER => [
'Authorization: Bearer TOKEN_DE_ACESSO',
'Accept-language: pt-BR',
],
// Define o URL para se conectar:
CURLOPT_URL => 'https://txttovoice.org/v/tts?v=1.1&text=Hello+world',
// Saída:
CURLOPT_RETURNTRANSFER => true
]);
// Executa:
$resultado = curl_exec($ch);
// Encerra CURL:
curl_close($ch);
The variable $resultado
will have the result returned by the website, so if you want to save somewhere use file_put_contents()
.
file_put_contents('nome_arquivo.formato', $resultado);
This is one of the easiest ways, but there may be memory-related issues and so you can handle storage directly in CURL, which will fix the problem.
For this the solution is to use CURLOPT_FILE
, which basically will do exactly what -o
does.
$escreverArquivo = fopen('nome_arquivo.formato', 'w+');
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer TOKEN_DE_ACESSO',
'Accept-language: pt-BR',
],
CURLOPT_URL => 'https://txttovoice.org/v/tts?v=1.1&text=Hello+world',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FILE => $escreverArquivo
]);
curl_exec($ch);
curl_close($ch);
fclose($escreverArquivo);
Remember that TOKEN_DE_ACESSO
must be token
, which is usually obtained in a previous request, in case of OAuth, for example. This should be mentioned in the API documentation, but this was not mentioned in the question so I could respond.