Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

file_get_contents() gets HTTP 403 Forbidden from a URL

ErrorHTTP/1.1 403 Forbidden

Short 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.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1The 403 page mentions blocked clients, bots or missing browserNo User-Agent header is sentFix
    2. 2The 403 body names an API key, token or required headerThe endpoint needs a header you did not sendFix

What does the error look like?

  • HTTP/1.1 403 Forbidden
  • the 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 200

Confirmed by php.net

Did this fix it?

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.php

Confirmed by php.net

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.