You can do using file_get_contents and file_put_contents or Curl.
The file_get_contents
can read a local or external file. It is especially suitable for simpler cases. Although you have the freedom to pass some headlines, it’s not nearly as complete as the cURL
, but it’s a good option.
You can use it as follows.
<?php
/* ID's da imagem */
$ids = [1,2,3,4,5,6,7,8,9,10];
foreach($ids as $id) {
/* Acessa, baixa e armazena o arquivo na variável $content */
$content = file_get_contents("https://url.com/get_img.php?user={$id}");
/* Escreve o conteúdo da variável $content em 1.png, 2.png, 3.png ... */
file_put_contents("{$id}.png", $content);
}
With the cURL
you can work better with Cookies, Headers, Follow Location, Authentication etc. It is a complete tool for requests. I will use the basic as an example.
<?php
/* ID's da imagem */
$ids = [1,2,3,4,5,6,7,8,9,10];
foreach($ids as $id) {
/* Cria um novo arquivo e caso o arquivo exista, substitui */
$file = fopen("{$id}.png", "w+");
/* Cria uma nova instância do cURL */
$curl = curl_init();
curl_setopt_array($curl, [
/* Define a URL */
CURLOPT_URL => "https://url.com/get_img.php?user={$id}",
/* Informa que você quer receber o retorno */
CURLOPT_RETURNTRANSFER => true,
/* Define o "resource" do arquivo (Onde o arquivo deve ser salvo) */
CURLOPT_FILE => $file,
/* Header Referer (Opcional) */
CURLOPT_REFERER => "https://url.com",
/* Desabilita a verificação do SSL */
CURLOPT_SSL_VERIFYPEER => false
]);
/* Envia a requisição e fecha as conexões em seguida */
curl_exec($curl);
curl_close($curl);
fclose($file);
}
Obs.: You can do a lot of things with C, including this. Remember that the
PHP is developed with C.