0

I am trying to write a script that will loop through a set of URLs. Each URL points directly to a jpeg file. The images are not on the same server. I found this question but I can't use the CURL libraries (not active on my server) and when I use @getimagesize I found that my function returned false for all images; however, I worked out that the cause as follows: if I type the valid URL into a web browser, I am prompted to authenticate against the remote server. If I type an invalid URL against the remote server, then I receive 404 instead.

So my question becomes - given a remote URL, can PHP detect whether the remote server is prompting for login? If so, that would surely be faster than parsing the results of the valid and authenticated URL calls. If not, then how can I have the PHP script authenticate itself in order to check the remote image?

Community
  • 1
  • 1
youcantryreachingme
  • 1,065
  • 11
  • 17
  • See http://stackoverflow.com/questions/22002980/using-file-get-contents-to-authenticate-and-access-an-htaccess-protected-file for how to send the authentication headers with `file_get_contents`. But I don't think there's a way to find out that this is the reason for the failure. – Barmar Jan 05 '17 at 23:57
  • Thanks. I ran with the answer that explained to query $http_response_header and linked to the manual. – youcantryreachingme Jan 06 '17 at 00:55

2 Answers2

0

depending on the type of authentication, if you use get_headers() on the remote url it will return "WWW-Authenticate" and it should be returning a "401 Unauthorized".

really you should moev to a server with curl, as it can then send the correct credentials

  • Thanks for the answer - I haven't tested yet but accepted the other answer already. We're shutting down this server soon - just a last minute task before it goes. – youcantryreachingme Jan 06 '17 at 00:54
0

A HTTP call with file_get_contents() will automatically create the variable $http_response_header which is an array containing the HTTP response headers. From there you can detect if there was an error.

See the manual for details.

Havenard
  • 27,022
  • 5
  • 36
  • 62