Hem cURL kullanımını hemde indirip kaydetmeyi, hataları yakalamayı içeren ufak bi' örnek hazırladım. İyi kodlamalar
<?php
function curl_put_contents($url, $file)
{
if(extension_loaded("curl"))
{
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HEADER, false);
curl_setopt($handle, CURLOPT_NOBODY, false);
curl_setopt($handle, CURLOPT_BINARYTRANSFER, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_TIMEOUT, 10);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
$request = (object) array(
"body" => curl_exec($handle),
"info" => curl_getinfo($handle),
"error" => (object) array(
"ID" => curl_errno($handle),
"message" => curl_error($handle)
)
);
curl_close($handle);
unset($handle, $url);
if($request->error->ID === 0)
{
$handle = @fopen($file, "w+"); # Hataları gizleyelim.
if(is_resource($handle))
{
fwrite($handle, $request->body);
fclose($handle);
unset($handle, $request, $file);
return true;
}
else
{
return "File write handler was couldn't open.";
}
}
else
{
return $request->error;
}
}
else
{
return false;
}
}
$url = "https://www.gravatar.com/avatar/8d9fdad045029154982032c6059c685a.png?s=468&d=mm&r=g";
$file = "ogun-saintx-karakus.png";
if($process = curl_put_contents($url, $file))
{
echo "{$url} adresindeki {$file} dosyası başarıyla indirildi.";
}
else
{
echo "{$url} adresindeki {$file} dosyası indirilmeye çalışılırken bi' hata oluştu.";
echo is_bool($process) ? "Hata anlaşılamadı." : $process;
}