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.
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 " );
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.