A simple REST API component for Joomla 5 that retrieves Community Builder user profile data. This component allows you to fetch CB user fields from one Joomla site to another via a secure API endpoint.
1. Create the component files in this structure:
2. Zip the com_cbuserapi folder
3. Install via Joomla Administrator → System → Extensions → Install
4. The component will be installed and ready to use
site/src/Controller/DisplayController.phpvalidateToken() method:
$validToken = 'YOUR_SECRET_API_TOKEN_HERE'; // Change this!
YOUR_SECRET_API_TOKEN_HERE with a strong, random token$validToken = 'k9mP2xL8vQ4wN7rT3jH6yU1zF5bS0aD9';openssl rand -hex 32bin2hex(random_bytes(32))If you want to use your own namespace instead of YourName:
YourName with your namespace (e.g., MyCompany)cbuserapi.xml:
<namespace path="src">MyCompany\Component\Cbuserapi</namespace>
GET https://yoursite.com/index.php?option=com_cbuserapi&task=getUser&user_id={USER_ID}&token={YOUR_TOKEN}
| Parameter | Type | Description |
|---|---|---|
option |
string | Component name (always com_cbuserapi) |
task |
string | Action to perform (currently only getUser) |
user_id |
integer | The Joomla user ID to retrieve |
token |
string | Your authentication token |
https://yoursite.com/index.php?option=com_cbuserapi&task=getUser&user_id=123&token=k9mP2xL8vQ4wN7rT3jH6yU1zF5bS0aD9
Success (200):
{
"success": true,
"data": {
"id": 123,
"email": "john.doe@example.com",
"firstname": "John",
"lastname": "Doe",
"avatar": "tn123_avatar.jpg",
"avatar_url": "https://yoursite.com/images/comprofiler/tn123_avatar.jpg",
"cb_address": "123 Main Street",
"cb_city": "Dallas",
"cb_state": "TX",
"cb_country": "United States"
}
}
Invalid Token (401):
{
"success": false,
"message": "Invalid or missing authentication token"
}
User Not Found (404):
{
"success": false,
"message": "User not found"
}
<?php
$userId = 123;
$token = 'k9mP2xL8vQ4wN7rT3jH6yU1zF5bS0aD9';
$apiUrl = 'https://yoursite.com/index.php?option=com_cbuserapi&task=getUser&user_id=' . $userId . '&token=' . $token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
$data = json_decode($response, true);
if ($data['success']) {
$user = $data['data'];
echo "Name: " . $user['firstname'] . " " . $user['lastname'];
echo "Email: " . $user['email'];
}
}
?>
const userId = 123;
const token = 'k9mP2xL8vQ4wN7rT3jH6yU1zF5bS0aD9';
const apiUrl = `https://yoursite.com/index.php?option=com_cbuserapi&task=getUser&user_id=${userId}&token=${token}`;
fetch(apiUrl)
.then(response => response.json())
.then(data => {
if (data.success) {
console.log('User:', data.data);
console.log('Name:', data.data.firstname, data.data.lastname);
} else {
console.error('Error:', data.message);
}
})
.catch(error => console.error('Request failed:', error));
IP Whitelisting: Add allowed IP addresses to restrict access
Database Token Storage: Store tokens in a database table instead of hardcoding
Rate Limiting: Prevent abuse by limiting requests per time period
| Issue | Solution |
|---|---|
| "Invalid or missing authentication token" | Verify the token in the URL matches the one in DisplayController.php. Check for extra spaces or special characters. |
| "User not found" | Verify the user ID exists in the #__users table. Check that the user has a Community Builder profile. |
| "Database error" | Verify Community Builder is installed. Check that CB tables exist (#__comprofiler). |
| Component won't install | Check that all files are in the correct folder structure. Verify XML syntax in cbuserapi.xml. |
| Empty or null field values | Some CB fields may be optional and empty. Check the CB field names match your installation. |
To retrieve additional Community Builder fields, edit the SELECT query in DisplayController.php and add the desired field names.
To add new functionality (e.g., update user data), create a new method in DisplayController.php and call it with a different task parameter.
GNU General Public License version 2 or later