Download API request file into server folder (localhost) with PHP

Asked

Viewed 476 times

-2

Dear friends, I have the code below that works as follows, it makes a request via Curl, brings me a file, but this being necessary to click save in the browser, I would like to simply open the command and the file was sent to a local folder "/images on localhost, could help?

*** the file is private only I will use on the server, download at scheduled times with a cron and make a future comparison with array_diff, it is not a public file.

<?php
 header("Content-Type: application/zip");
 header("Content-Disposition: inline; filename=test.zip");

$token = file_get_contents("http://localhost/novoprojeto/token.php");
$curl = curl_init();
$imagem = "084301";

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://api/api/imagem/download-zip/$imagem",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array( "Authorization: Bearer $token"
  ),
));

$response = curl_exec($curl);

curl_close($curl);

  • Is there a specific reason you use Curl instead of file_get_contents() to get the file? I don’t see a reason to use Curl.

2 answers

0

If I understand correctly, you must change the header in the third row to the following:

header("Content-Disposition: attachment; filename=filename=test.zip");

This way you will already automatically download.

0


Your question is a little confused, there are two interpretations:

  • If you want the client (who accesses the page) to download the content. If this is the case, the @Leonardo Ferreira reply is correct. An alternative would also be to add the attribute download in the <a> of HTML. The fact that you’re wearing the Content-Disposition: inline; filename=test.zip leads us to believe that this is what you seek.

  • If you want to download the file to your server (that is, download the API image and save it to your server). If this is the case, see the answer below.


To download the content to a file, use the CURLOPT_FILE

$imagem = "084301";

// Crie o diretório:
if (!is_dir("/imagens")) {
    mkdir("/imagens", 0777, true); // Cuidado com 0777
}

// Crie o arquivo:
$arquivo = fopen("/imagens/" . $imagem . ".jpg", 'w+'); 
// Note que é preciso saber e extensão, assumi `jpg`. Você poderá abrir o arquivo depois, sem extensão, e descobrir a extensão e renomear, mas isto é outro problema.

$token = file_get_contents("http://localhost/novoprojeto/token.php");

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "http://api/api/imagem/download-zip/$imagem",
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array("Authorization: Bearer $token"),

  // Faça o CURL usar o arquivo:
  CURLOPT_FILE => $arquivo,

  // Talvez precise alterar isto para `false`, não me recordo:
  CURLOPT_RETURNTRANSFER => true, 
));


curl_exec($curl);
curl_close($curl);

// Fecha o arquivo:
fclose($fp);

The idea is to get the file and save to imagens/{nome do arquivo}. For this, it uses the fopen and then add this fopen in the CURLOPT_FILE.

  • Hello Inkeliz, It comes to the second interpretation, I reformulated the doubt, rewrote my code worked, Thank you very much!

Browser other questions tagged

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