I tried to save image that I catch from a get request on an API on the server. I catch data images and i did not find the way to save it, after hours of trying.
Here is my code:
foreach ($photos as $photo) {
$photo_uuid=$photo['uuid'];
$request = wp_remote_get('https://api.??????.fr/annonces/'.$uuid.'/photos/'.$photo_uuid,
array( 'timeout' => 10,
'headers' => array( 'AUTH-TOKEN' => '---------------'),
'uuid_annonce' => $uuid,
'uuid_photo' => $photo_uuid
));
if( is_wp_error( $request ) ) {
return false; // Si il y a une erreur, on s'arrête là
}
$body = wp_remote_retrieve_body( $request );
$src = 'data:image/jpg;base64,'. base64_encode($body);
$data = str_replace('data:image/jpg;base64,', '', $src);
$data = str_replace(' ', '+', $data);
$data = base64_decode($data);
$file = '<?php echo esc_url( home_url( '/' ) ); ?>wp-content/themes/--------/img/'. uniqid() . '.jpg';
file_put_contents($file, $data);
// $re = '<img src="'. $src .'" alt="jpg" />';
// echo $re;
}
I can display photos on the page (it works) but i didn't manage to save them.
Sorry for my english,
Thank you :)