Difference between revisions of "API:User::get user name"

(New page: Get a user name of a user given a user id. ===Calling=== <code php>$api->user->get_username($user_id);</code> ===Parameters=== '''<nowiki>$</nowiki>user_id''' - An integer user ID corres...)
 
(Example)
 
(2 intermediate revisions by 2 users not shown)
Line 24: Line 24:
 
{
 
{
 
     echo $api->user->get_username(1163) . "<br />\n";
 
     echo $api->user->get_username(1163) . "<br />\n";
     echo $api->user->get_username(1000000) . "<br />\n";
+
     echo $api->user->get_username(5000000) . "<br />\n";
     echo $api->user->get_username(1000001) . "<br />\n";
+
     echo $api->user->get_username(5000001) . "<br />\n";
 
}
 
}
 
catch (AoPSAPI_Exception $ex)
 
catch (AoPSAPI_Exception $ex)
Line 47: Line 47:
  
 
===Output===
 
===Output===
  rruszyk
+
  rrusczyk
  1000000 is an invalid user id.
+
  5000000 is an invalid user id.

Latest revision as of 07:37, 17 November 2022

Get a user name of a user given a user id.

Calling

$api->user->get_username($user_id);

Parameters

$user_id - An integer user ID corresponding to the user ID used on artofproblemsolving.com

Returns

The user name associated with the user id.

Exceptions

AoPSAPI_Exception::MISSING_PARAMETER - Missing User ID

AoPSAPI_Exception::INVALID_PARAMETER - Invalid parameter. Triggered when there are non-numeric characters in user id parameter

AoPSAPI_Exception::INVALID_USER - User not found in Art of Problem Solving database

Example

require_once 'AoPSAPI.php';

$api = new AoPSAPI('<api_key>', '<secret_key>'); try {

   echo $api->user->get_username(1163) . "
\n"; echo $api->user->get_username(5000000) . "
\n"; echo $api->user->get_username(5000001) . "
\n";

} catch (AoPSAPI_Exception $ex) {

   switch ( $ex->getCode() )
   {
       case AoPSAPI_Exception::MISSING_PARAMETER:
           echo "I should never be seen since the API sends in correct parameters.
\n"; break; case AoPSAPI_Exception::INVALID_PARAMETER: echo "Oh no, I sent in the wrong parameter " . implode(" ", $ex->getParams()) . ".
\n"; break; case AoPSAPI_Exception::INVALID_USER: echo implode(" ", $ex->getParams()) . " is an invalid user id.
\n"; break; default: echo $ex->getMessage() . "
\n"; break; }

}

Output

rrusczyk
5000000 is an invalid user id.