I am trying to download images through an API and place them in a directory, in order to do this I have to send the api authorization through a generated token.
This is my function:
function urlToFile($url){
$options = array(
CURLOPT_FILE => fopen('/var/www/html/carpeta/custom/modules/carpetas/' . 'imagen.jpg', 'w'),
CURLOPT_TIMEOUT => 28800, // set this to 8 hours so we dont timeout on big files
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array(
'Authorization: Token XXXXX',
));
$ch = curl_init();
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
The function downloads a file and saves it, but it won't let me open it because it appears damaged, which makes me think that perhaps the request is not reaching the correct path and perhaps it is due to authorization. Is there something wrong with that?
the logic that you share seems correct, the detail will be in how you save what you receive, if the file is being saved with the correct extension or the content is correct, you can try showing in the console the http status that you receive when making the request and print it that you receive in the response. It would also help if you put that code that you are using.