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?
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:
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.
Perhaps the server is restricting access from non-browsers by examining the user-agent string?
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.