0

So if I'm browsing http://www.example.com/user1.jpg I see the user's picture. But if I'm making curl request via PHP from my localhost webserver (so the same IP) it throws 401 unauthorized. I even tried to change the user agent and still no success.

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://example.com/user1.jpg',
    CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0'
));
$resp = curl_exec($curl);

echo $resp;
curl_close($curl);

What can be wrong?

catalin
  • 946
  • 6
  • 14
  • 31
  • Well, the URL that you are trying to 'curl' is givinig you 401, you need to know why, do you own the URL? – Spoody Jan 19 '18 at 14:22
  • I don't own the URL, but it works if I'm browsing from Chrome? It's the same IP, the same machine. The curl is executed locally inside the same machine. Doesn't make sense to me – catalin Jan 19 '18 at 14:24
  • 1
    Check this answer: https://stackoverflow.com/a/9391270/2595450 – Spoody Jan 19 '18 at 14:25
  • 1
    usually, it means you need an (possibly authenticated) cookie session before downloading the image. – hanshenrik Jan 19 '18 at 14:30

1 Answers1

0

I used Fiddler tool analyzing the headers sawing 3 GET requests. First two were 401 Unauthorized, third was accepted without typing credentials (probably SSON implemented). It was using NTLM authentication protocol, so running curl from CLI with "--ntlm username:password" did the job for me.

catalin
  • 946
  • 6
  • 14
  • 31