0

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 :)

Sacha
  • 3
  • 2
  • Does this answer your question? [Copy Image from Remote Server Over HTTP](https://stackoverflow.com/questions/909374/copy-image-from-remote-server-over-http) – Stender Aug 17 '20 at 11:52
  • Does this answer your question? [Concatenate PHP function output to a string like variables](https://stackoverflow.com/questions/27494038/concatenate-php-function-output-to-a-string-like-variables) - The line `$file = ' – M. Eriksson Aug 17 '20 at 11:56

0 Answers0