1

I have following code:

$url = 'http://' . $host . ':' . $port . $params;
$results = file( $url );

I am getting following exception:

file(http://someurl.com/asd/asd): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden

But I access URL(stored in $url variable) directly in browser, it is working perfectly fine. Why am I getting problem while accessing it from PHP?

5
  • 1
    URL file-access is maybe disabled in the server configuration (php.ini) --> allow_url_fopen = Off (change to On) Commented Jul 23, 2013 at 6:52
  • @0xBAADF00D I think that is a different error message... the server he is accessing is probably either taking issue with his UA string or server ip. Probably some sort of anti-bot measure Commented Jul 23, 2013 at 6:54
  • As a completion to @0xBAADF00D maybe it's blocked if you don't send an User Agent, try cURL. Commented Jul 23, 2013 at 6:55
  • @Orangepill try to access url from cURL with a "wtf" user agent : curl -A "kljslkdfjslfjdsklfjsldkf" http://... to see if the serveur block unknow user agent. Commented Jul 23, 2013 at 6:55
  • @0xBAADF00D think that should have gone to Awan :) Commented Jul 23, 2013 at 6:57

1 Answer 1

2

This error message:

HTTP/1.0 403 Forbidden

...is actually coming directly from the server that you're trying to access. So it's not a problem with PHP getting out to access an outside file.

It's instead more a problem of convincing the server to actually give you the file just like it would give it to a web browser.

There's a couple issues I would check:

  1. Is the file password-protected? A web browser will save the password you enter, and allow you to re-access the URL without entering the password again, but PHP doesn't know what password to use.

  2. Perhaps the server is restricting access from non-browsers by examining the user-agent string?

  3. Perhaps the server is restricting access unless you're referred from another page on the same site?

One thing you could try would be using an online HTTP header viewer tool to try requesting the file directly. You can experiment with different headers, user-agents, etc to see if you can reproduce the problem.

If I had to guess, my money would be on the first problem - requiring a password. There's no way to know without knowing the actual URL though.


If you do figure out what it is that's blocking you from accessing the file, you should read up on the http wrapper, stream contexts, and particularly the http stream context options to find out how to set the missing information from PHP. There are ways to specify a password, a user-agent, a referrer header, or even any random HTTP header you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.