Reputation: 1166
I'm using curl to download an image from url in this way:
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);
if (!$html) {
echo "<br />cURL error number:" .curl_errno($ch);
echo "<br />cURL error:" . curl_error($ch);
return false;
}
else{
return $html;
}
If the $url
is like http://example.com/image.jpg
my function works and i'm able to use the html returned.
If the $url
is like http://example.com/imagè.jpg
my function doesn't work and print this lines (obviously the file exists and i'm able to see it in browser!):
cURL error number:22
cURL error:The requested URL returned error: 404 Not Found
Upvotes: 1
Views: 541
Reputation: 9518
You just need to encode the image name part of the URL before you use it in your cURL request:
$rest_of_the_url = "http://example.com/";
$image_name = "imagè.jpg";
$url = $rest_of_the_url . urlencode($image_name);
Upvotes: 1