How to make an http request with php with authentication and waiting for a file in response?

3

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

The problem is that I have never done this before and I do not 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

How do I make a php send these headers in this link and receive a download of a file in exchange and save in the same folder as my php file?

In 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 it work?

    
asked by anonymous 17.02.2017 / 15:17

1 answer

3

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.

    
20.02.2017 / 17:26