How do I make an http request with php with authentication and waiting for a file to respond?

Asked

Viewed 1,661 times

3

I am using a Text to Speech service and would like to use it in a php that I’m riding.

The problem is that I never did what the api asks and I don’t even know where to start.

this type of information is provided in the documentation

GET https://txttovoice.org/v/tts?v=1.1&text=Hello+world

Headers:
Authorization: Bearer TOKEN_DE_ACESSO
Accept-Language: pt-BR

with which I make a php send these headers on this link and receive a download of a file in return and save in the same folder as my php file this?

in the api it provides a Curl tbm

curl -k -H "Authorization: Bearer TOKEN_DE_ACESSO" -H "Accept-language: pt-BR" https://txttovoice.org/v/tts?v=1.1&text=Hello+world" -o arquivo.wav

what would be the simplest way for me to make this work?

  • This link It’s not the chewed solution, but it might get you in the way. You make the request by Curl and you can save the reply to a file or process the content of the request response however you want.

  • you try to thank

1 answer

3


Let’s translate the CURL:

  • curl : Starts the CURL.
  • -k : Set to unsafe mode, not checking SSL.
  • -H "Authorization: Bearer TOKEN_DE_ACESSO" Sets 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" : Define 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 the file_put_contents().

file_put_contents('nome_arquivo.formato', $resultado);

This is one of the easiest ways, but there may be problems related to memory and so can treat the storage directly in the CURL, which will fix the problem.


For this the solution is to use the CURLOPT_FILE, which will basically do exactly what the -o ago.

$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 the TOKEN_DE_ACESSO must be the token, which is usually obtained in a previous request, in the case of Oauth, for example. This must be mentioned in the API documentation, but this was not mentioned in the question so you could answer.

  • right, you are already working here . thank you very much for your time

Browser other questions tagged

You are not signed in. Login or sign up in order to post.