Error downloading images from a CURL site via PHP

Asked

Viewed 296 times

1

I have a pack of images in a folder that works like this: nome-do-produto-m.jpg, for average size.

Now I want to download the big size.

I made a code to read images from the local directory, change the letter of m for g and download the new images. Apparently it works, but the images come all with size 0kb.

My code:

<?php
$path = "arquivos/";
$diretorio = dir($path);

echo "Lista de Arquivos do diretório '<strong>".$path."</strong>':<br />";
while($arquivo = $diretorio -> read()){
//echo "<a href='".$path.$arquivo."'>".$arquivo."</a><br />";
    $fotogrande = str_replace('-m.', '-g.', $arquivo);

    //echo $fotogrande;

    $url = "www.site.com.br" . $fotogrande;

    $ch = curl_init($url);

    $fp = fopen('fotogrande/'. $fotogrande, 'wb');

    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);


}
$diretorio -> close();
?>

Example of Valid URL generated by my script: https://www.site.com.br/product/14667-sal-temperado-gostinho-alho-e-sal-200g-g.jpg

1 answer

2


"- but the images all come with 0kb"

Does not come! You’re creating this image here:

$fp = fopen('fotogrande/'. $fotogrande, 'wb');

The problem is that your curl is not reaching the "big image" on that site. What results in a file created and without being filled with the "supposed" content coming from curl.

To understand what is actually wrong, it is interesting to add an error check in the execution of curl:

curl_exec($ch);
echo curl_error($ch);

In your specific case, the mistake is:

SSL Certificate problem: self Signed Certificate in Certificate chain

To resolve this issue, you need to add an updated certification chain to your request.

A simple and effective way that has been recommended in a question on Soen for @Schizoduckie is the following:

  1. Download the certificate chain on: http://curl.haxx.se/ca/cacert.pem;

  2. Move/paste a copy to the root of your script - side-by-side;

  3. Add to request from curl:

    curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__).'/cacert.pem');
    

Remember that the most correct is to set this file directly in the directive curl.cainfo of php.ini. What will take away the need to add one by one to all your requests with curl using SSL.

Example of functional code and reproducible:

<?php

$url = 'https://treichel-img.azureedge.net/product/14667-sal-temperado-gostinho-alho-e-sal-200g-g.jpg';

$curl = curl_init($url);

$arquivo = fopen('imagem.png', 'wb');

curl_setopt($curl, CURLOPT_FILE, $arquivo);
curl_setopt($curl, CURLOPT_HEADER, 0);

curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__).'/cacert.pem');
//      http://curl.haxx.se/ca/cacert.pem <<<<------------^

curl_exec($curl);
echo curl_error($curl);

curl_close($curl);
fclose($arquivo);
  • can we chat? I don’t understand what you mean, because if I give an echo on the line, it’s riding the right way

  • @Ricardogonçalves Arrives more on chat: https://chat.stackexchange.com/rooms/90925/downloading-imagens-de-um-site-com-php

  • 1

    function that is a doja :P

  • 1

    @Ricardogonçalves Taking PHP to the extreme, right?! Kkkkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.