You can do this using file_get_contents and file_put_contents or # .
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 headers, it is not as complete as cURL
, but it is 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 cURL
you can work better with Cookies , Headers , Follow Location , Authentication etc . It is a complete tool for requisitions. I'll use the basics 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);
}
Note: You can do many things with C , including this. Remember that the
PHP is developed with C .