8

I am getting the following error:

getimagesize(https://static1.squarespace.com/static/570d03d02b8dde8b2642afca/570f74e87c65e4819dec6812/57272dbfb6aa606f78a5d8b5/1470397291105/4XTRYCK3.jpg): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

The image opens without problems in my browser.

Does anyone understand why this fails?

Ekta Padaliya
  • 5,553
  • 2
  • 37
  • 46
alieninlondon
  • 860
  • 2
  • 11
  • 33
  • [this post](http://stackoverflow.com/questions/23193870/php-function-getimagesize-gives-read-error-when-trying-to-get-https-url) may provide some help for you – dpp Dec 22 '16 at 21:21
  • This is incredibly strange. I just tested again, an now that URL works (both HTTP and HTTPS). But I try another similar one, it fails again in same way for both HTTP and HTTPS. For example: https://static1.squarespace.com/static/570d03d02b8dde8b2642afca/570f74e87c65e4819dec6812/57272d96b6aa606f78a5d80a/1470397194245/4XTRYCK2.jpg – alieninlondon Dec 22 '16 at 21:34
  • @dpp Also, the solution in the post you mentioned unfortunately seems to fail in the same way. – alieninlondon Dec 22 '16 at 21:41
  • Thought about if the target server is blocking crawl somehow, but [this solution](http://stackoverflow.com/questions/25230949/php-getimagesize-not-working) didn't work either, still bad request. – alieninlondon Dec 22 '16 at 21:57
  • Now convinced the target server is blocking my client because it is using certain user agent headers. Tried connecting with curl and editing the headers to look like Firefox web browser, then I got through. But encountered other errors. Seems like a simple solution here is not possible, ideally I would like to edit the user agent headers of getimagesize(). – alieninlondon Dec 23 '16 at 22:16
  • No, I'm having this issue with both one of my feed readers (server-based) and my randomly connected IP. I've made a little progress. I've found that using CURL with a forged User-Agent retrieves the file. – Eric_WVGG Dec 28 '16 at 00:02
  • This error could be the same probably, got it from another site: "HTTP request failed! HTTP/1.1 406 Not Acceptable" – alieninlondon Jan 01 '17 at 09:04

1 Answers1

4

Squarespace is rejecting any connection where the User-Agent header is not a web browser. This includes CURL and getimagesize.

You can set the user-agent header by inserting this into your code:

ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)');
Eric_WVGG
  • 2,781
  • 3
  • 24
  • 25
  • This resolved my problem! Addition is that the server seems to remember/cache a valid user agent. When I first run getimagesize without ini_set per above it blocks like it should, when I run getimagesize with ini_set per above it works, and the final funny thing is that when I turn off ini_set again getimagesize still works for a few days/hours (not sure) until it reverts to the original error. Can anyone explain this? Not important though, just curious. – alieninlondon Dec 29 '16 at 07:48