1

I run command like

wget https://odis.homeaway.com/odis/listing/5895882c-6c65-47f5-b782-e13b8246e4aa.c10.jpg

I try to get above image using curl, the returned image is invalid jpg image. When I try with browser and save image as, I get valid jpg image. Even when I try with file_get_contents it returns false.

    $image = 'https://odis.homeaway.com/odis/listing/5895882c-6c65-47f5-b782-e13b8246e4aa.c10.jpg'
    $image_data = file_get_contents($image);
    try {
        echo 'saving';
        $image1 = imagecreatefromjpeg($image_data);

    } catch (Exception $ex) {
        echo 'error';
    }

How to get this image with curl, wget, or file_get_contents?

user3783243
  • 5,368
  • 5
  • 22
  • 41
Aakash John
  • 301
  • 2
  • 12
  • Possible duplicate of [Save image from url with curl PHP](https://stackoverflow.com/questions/6476212/save-image-from-url-with-curl-php) – Adam May 09 '18 at 18:29
  • No I am trying same code in case of curl, but I get invalid jpg image saying Not a JPEG file: starts with 0x89, I tried saving in png/gif, still does not work – Aakash John May 09 '18 at 19:10
  • I have tried with all option 1. wget 2. curl 3. file_get_contents, In each case I get invalid image – Aakash John May 09 '18 at 19:11
  • Possible duplicate of [can't get image using php CURL with SSL](https://stackoverflow.com/questions/33555219/cant-get-image-using-php-curl-with-ssl) – user3783243 May 09 '18 at 19:37
  • Duplicate: https://stackoverflow.com/questions/26148701/file-get-contents-ssl-operation-failed-with-code-1-and-more – Misunderstood May 09 '18 at 20:46

1 Answers1

2

it's a bug with the odis.homeaway.com server, you should send their webmaster a bugreport.

their server ALWAYS send the image in https://odis.homeaway.com/odis/listing/5895882c-6c65-47f5-b782-e13b8246e4aa.c10.jpg gzip compressed, even if the client did NOT specify accept-encoding: gzip. wget doesn't support gzip compression at all, but curl does. until they fix their server, you can tell curl to decompress it for you automatically by setting the --compressed argument in the command line version, or setting CURLOPT_ENCODING=>'gzip' in the curl_ api, or running it through gzdecode(), eg $image_data = gzdecode(file_get_contents($image)); (the gzdecode() method is NOT recommended, because this will stop working once they actually fix their server, and only works now because their server is bugged). the reason your browser isn't affected by the bug, is that all mainstream browser always sends Accept-Encoding: gzip (and others) by default.

edit: originally i mentioned gzuncompress(), not gzdecode(), that was a mistake, the correct decompression function is gzdecode().

hanshenrik
  • 19,904
  • 4
  • 43
  • 89