Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to CURL a remote file

Tags:

curl

I have a piece of code that is designed to receive any URL and rip it down from the web. So far it's been working fine, until someone gave it this URL:

http://www.aspensurgical.com/static/images/aspen_hill-rom_logo.png

If I hit it from my browser, it shows just fine. But when I try to CURL it down, I get:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /static/images/aspen_hill-rom_logo.png
on this server.</p>
<hr>
<address>  Server at www.aspensurgical.com Port 80</address>
</body></html>

The CURL code I'm using is:

$ch = curl_init(str_replace(' ', '%20', $url));
$fh = fopen($local_file, "w");
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_exec($ch);
curl_close($ch);

Is their server somehow realizing I'm not a normal browser and booting me?

like image 536
Anthony Avatar asked Mar 21 '13 05:03

Anthony


1 Answers

They have useragent check to see who you are. Add normal browser's useragent and you should be fine.

curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0");

Here's working example in codepad.

like image 193
Ranty Avatar answered Oct 09 '22 21:10

Ranty