Update Player Profile
The update profile method allows you to modify existing player information such as name, email, phone number, and profile picture. This is useful for keeping player profiles synchronized with your user management system.
Overview
The updatePlayerInfo method:
- Updates existing player profile information
- Supports partial updates (only provided fields are updated)
- Validates input data before updating
- Returns status code indicating success or failure
Method Signature
public function updatePlayerInfo(array $userDetails = []): int
Parameters
Required Parameters
userDetails(Array) - Array containing player information to update- All fields are optional - only provided fields will be updated
- Supported keys:
name(String) - Player's display nameemailid(String) - Player's email addressphoneno(String) - Phone number (format: countrycode-number, no +)pwd(String) - Player's passwordprofilehiresimg(String) - Base64 encoded profile image
Return Value
Returns an integer status code:
- Positive value - Update successful
- Zero or negative - Update failed
Basic Usage
Update Player Name
use Veniso\Moitribe\Sdk\modules\classes\MoitribeApi;
$moitribe = new MoitribeApi([
'gameid' => 'your-game-id',
'channelid' => 'your-channel-id',
'playerid' => 'player-123'
]);
try {
$result = $moitribe->profRequestsHandler->updatePlayerInfo([
'name' => 'John Smith'
]);
if ($result > 0) {
echo "Player name updated successfully!\n";
} else {
echo "Failed to update player name\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
Update Multiple Fields
$userDetails = [
'name' => 'Jane Doe',
'emailid' => 'jane.doe@example.com',
'phoneno' => '1-5559876543' // Format: countrycode-number
];
try {
$result = $moitribe->profRequestsHandler->updatePlayerInfo($userDetails);
if ($result > 0) {
echo "Player profile updated successfully!\n";
echo "Updated name, email, and phone number\n";
} else {
echo "Failed to update player profile\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
Update Profile Image
// Load and encode image
$imagePath = '/path/to/new/profile.jpg';
$imageData = base64_encode(file_get_contents($imagePath));
try {
$result = $moitribe->profRequestsHandler->updatePlayerInfo([
'profilehiresimg' => $imageData
]);
if ($result > 0) {
echo "Profile image updated successfully!\n";
} else {
echo "Failed to update profile image\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
Integration Examples
Profile Update Form Handler
function handleProfileUpdate($postData, $fileData = null) {
global $moitribe;
$userDetails = [];
// Validate and collect form data
if (!empty($postData['name'])) {
$userDetails['name'] = sanitizeInput($postData['name']);
}
if (!empty($postData['email'])) {
if (filter_var($postData['email'], FILTER_VALIDATE_EMAIL)) {
$userDetails['emailid'] = $postData['email'];
} else {
return ['success' => false, 'message' => 'Invalid email format'];
}
}
if (!empty($postData['phone'])) {
// Validate phone format (countrycode-number)
if (preg_match('/^\d{1,3}-\d+$/', $postData['phone'])) {
$userDetails['phoneno'] = $postData['phone'];
} else {
return ['success' => false, 'message' => 'Invalid phone format. Use: countrycode-number'];
}
}
// Handle profile image upload
if ($fileData && $fileData['error'] === UPLOAD_ERR_OK) {
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
if (in_array($fileData['type'], $allowedTypes)) {
$imageData = base64_encode(file_get_contents($fileData['tmp_name']));
$userDetails['profilehiresimg'] = $imageData;
} else {
return ['success' => false, 'message' => 'Invalid image type'];
}
}
try {
if (empty($userDetails)) {
return ['success' => false, 'message' => 'No fields to update'];
}
$result = $moitribe->profRequestsHandler->updatePlayerInfo($userDetails);
if ($result > 0) {
return ['success' => true, 'message' => 'Profile updated successfully'];
} else {
return ['success' => false, 'message' => 'Failed to update profile'];
}
} catch (Exception $e) {
error_log("Profile update error: " . $e->getMessage());
return ['success' => false, 'message' => 'Server error occurred'];
}
}
// Usage in your application
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$result = handleProfileUpdate($_POST, $_FILES['profile_image'] ?? null);
if ($result['success']) {
echo "<div class='success'>" . $result['message'] . "</div>";
} else {
echo "<div class='error'>" . $result['message'] . "</div>";
}
}
Sync with External User System
function syncPlayerProfile($externalUserId, $externalUserData) {
global $moitribe;
try {
// First, get the Moitribe player ID for this user
$player = $moitribe->profRequestsHandler->playerForceLoginWithUniqueId($externalUserId);
if (!$player) {
throw new Exception("Player not found in Moitribe");
}
// Prepare update data
$updateData = [];
if (isset($externalUserData['name']) && $externalUserData['name'] !== $player->getName()) {
$updateData['name'] = $externalUserData['name'];
}
if (isset($externalUserData['email']) && $externalUserData['email'] !== $player->getEmail()) {
$updateData['emailid'] = $externalUserData['email'];
}
if (isset($externalUserData['phone']) && $externalUserData['phone'] !== $player->getPhone()) {
$updateData['phoneno'] = $externalUserData['phone'];
}
// Only update if there are changes
if (!empty($updateData)) {
$result = $moitribe->profRequestsHandler->updatePlayerInfo($updateData);
if ($result > 0) {
echo "Profile synchronized successfully for user: $externalUserId\n";
return true;
} else {
echo "Failed to synchronize profile for user: $externalUserId\n";
return false;
}
} else {
echo "No profile updates needed for user: $externalUserId\n";
return true;
}
} catch (Exception $e) {
error_log("Profile sync error for user $externalUserId: " . $e->getMessage());
return false;
}
}
// Example usage with external user data
$externalUsers = [
['id' => 'user123', 'name' => 'Alice Johnson', 'email' => 'alice@example.com'],
['id' => 'user456', 'name' => 'Bob Wilson', 'phone' => '1-5551234567']
];
foreach ($externalUsers as $user) {
syncPlayerProfile($user['id'], $user);
}
Error Handling
Common Error Scenarios
- Empty user details array - No fields to update
- Invalid email format - Email validation failed
- Invalid phone format - Phone number doesn't match expected format
- Image upload errors - File too large or invalid format
- Network issues - API communication problems
Error Handling Example
function safeUpdateProfile($userDetails) {
global $moitribe;
try {
// Validate input
if (empty($userDetails)) {
throw new InvalidArgumentException("No user details provided");
}
// Validate email if provided
if (isset($userDetails['emailid'])) {
if (!filter_var($userDetails['emailid'], FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException("Invalid email format");
}
}
// Validate phone if provided
if (isset($userDetails['phoneno'])) {
if (!preg_match('/^\d{1,3}-\d+$/', $userDetails['phoneno'])) {
throw new InvalidArgumentException("Invalid phone format. Use: countrycode-number");
}
}
$result = $moitribe->profRequestsHandler->updatePlayerInfo($userDetails);
if ($result > 0) {
return ['success' => true, 'message' => 'Profile updated'];
} else {
return ['success' => false, 'message' => 'Update failed'];
}
} catch (InvalidArgumentException $e) {
return ['success' => false, 'message' => $e->getMessage()];
} catch (Exception $e) {
error_log("Profile update error: " . $e->getMessage());
return ['success' => false, 'message' => 'Server error'];
}
}
Best Practices
-
Validate Input Data
- Always validate email format before sending
- Ensure phone numbers follow the correct format
- Check image file types and sizes
-
Use Partial Updates
- Only send fields that need to be updated
- Don't send unchanged data unnecessarily
-
Handle Image Uploads Carefully
- Validate image file types
- Limit file sizes to prevent issues
- Use proper error handling for file operations
-
Log Update Attempts
- Log successful updates for auditing
- Log failures for debugging
- Monitor for unusual update patterns
Related Methods
- Force Login with Unique ID - Create or authenticate players
- Get Player Friends - Get player's friends list
- Get Multiple Players - Get multiple player profiles