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,743
  • 3
  • 39
  • 51
  • [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 –  Dec 22 '16 at 21:34
  • @dpp Also, the solution in the post you mentioned unfortunately seems to fail in the same way. –  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. –  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(). –  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" –  Jan 01 '17 at 09:04

2 Answers2

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,957
  • 3
  • 28
  • 29
  • 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. –  Dec 29 '16 at 07:48
0

In my case (clearly not the same as the OP based on their URL) there were two problems.

The first was that the URL I was being given was HTTP, but the browser would successful redirect to HTTPS while my getimagesize() would fail with the given error. I had to replace "http" with "https" in my URL.

$url = "http://test.com/file name.jpg";
$url = preg_replace('/^http:/', 'https:', $url);

The second was that the URL I was given had a space in it. I had to encode the space as %20.

$url = (dirname($url) . '/'. rawurlencode(basename($url)));
Final URL: https://test.com/file%20name.jpg

With those two replacements, the call to getimagesize() was successful.

Mark Bench
  • 111
  • 3