0

I try to catch an 403 error from a file_get_contents. Please don't answer that i should use curl. Here is my code snippet:

$url     = "https://authserver.mojang.com/authenticate";
$data    = array(
        "agent"      => array(
                "name"       => "Minecraft",
                "version"    => 1
        ),
        "username"   => $username,
        "password"   => $password
);

$options = array(
        "http" => array(
                "header"     => "Content-Type: application/json\n",
                "method"     => "POST",
                "content"    => json_encode( $data )
        )
);

$context = stream_context_create( $options );
$result  = file_get_contents( $url, false, $context );

$response = json_decode( $result );

My problem is, when i do a request with valid credentials, mojang answer with a valid json string. But when i do a request with invalid credentials i get an 403 HTTP error.

How can i catch them without a warning on my site.

dario
  • 5,149
  • 12
  • 28
  • 32

1 Answers1

1

I found a solution. See Good error handling with file_get_contents for further information.

Here is my code now:

[...]
$result  = @file_get_contents( $url, false, $context );

if( ( $response = json_decode( $result ) ) === NULL )
{
    exit( "invalid credentials" );
}
else
{
    exit( "Go on " );
}
Community
  • 1
  • 1