file_get_contents() gets HTTP 403 Forbidden from a URL
HTTP/1.1 403 ForbiddenShort answer
The server answered 403 to a request PHP built itself. PHP sends no user agent by default, and many servers refuse that. Send one with a stream context, add any required header such as Authorization, and use ignore_errors to read the response body, which usually says why.
Fix it fast
1Find out which cause you have
Read the body of the 403 response. ignore_errors makes PHP return it instead of false.
<?php $ctx = stream_context_create(['http' => ['ignore_errors' => true]]); $body = file_get_contents('https://example.com/api', false, $ctx); echo $body, "\n"; print_r($http_response_header);2Match what you see, then jump to its fix
What does the error look like?
HTTP/1.1 403 Forbiddenthe same URL opens fine in the browser
Cause 1 of 2
No User-Agent header is sent
1Check
The user_agent setting defaults to NULL, so PHP sends none unless you set it.
php -r 'var_dump(ini_get("user_agent"));'2Fix
Set a descriptive user agent in the stream context. The context option is used only when no User-Agent is in the header option.
<?php
$ctx = stream_context_create(['http' => [
'user_agent' => 'MyApp/1.0 (+https://example.com/contact)',
]]);
echo file_get_contents('https://example.com/data.json', false, $ctx);3Verify
The call returns the body and $http_response_header starts with HTTP/1.1 200.
php fetch.php | head -c 200Confirmed by php.net
Cause 2 of 2
The endpoint needs a header you did not send
1Check
Read the API documentation for the header it expects, then compare it with the headers your script sends.
php -r '$c=stream_context_create(["http"=>["ignore_errors"=>true]]); file_get_contents("https://example.com/api",false,$c); print_r($http_response_header);'2Fix
Pass the header through the header option. Values there override defaults such as User-Agent, so do not repeat one you set elsewhere.
<?php
$ctx = stream_context_create(['http' => [
'header' => ["Authorization: Bearer " . getenv('API_TOKEN'),
"Accept: application/json"],
]]);
echo file_get_contents('https://example.com/api', false, $ctx);3Verify
The response status line shows 200 and the body is the JSON you expected.
php fetch.phpConfirmed by php.net
How do I stop it coming back?
- Always create a stream context for outside calls, with a user agent that names your app.
- Check $http_response_header, not only the return value.
- For anything beyond one simple call, use the cURL extension, which gives clearer errors.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix