Here's an example of how to submit content to a Joomla 5 site using its REST API:
To submit content via the Joomla 5 REST API, you'll need to make a POST request to the articles endpoint. Here's some sample PHP code demonstrating this:
php
<?php
// API endpoint URL
$apiUrl = 'https://your-joomla-site.com/api/index.php/v1/content/articles';
// Your API token
$apiToken = 'your-api-token-here';
// Article data
$articleData = [
'title' => 'My New Article',
'alias' => 'my-new-article',
'articletext' => 'This is the content of my new article.',
'catid' => 2, // Category ID
'state' => 1, // Published
'access' => 1, // Public
'language' => '*' // All languages
];
// Initialize cURL session
$ch = curl_init($apiUrl);
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Joomla-Token: ' . $apiToken
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($articleData));
// Execute the request
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch);
} else {
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode == 201) {
echo 'Article created successfully!';
$responseData = json_decode($response, true);
echo 'New article ID: ' . $responseData['data']['id'];
} else {
echo 'Error: ' . $httpCode . ' - ' . $response;
}
}
// Close cURL session
curl_close($ch);
This code does the following:
1. It sets up the API endpoint URL and your API token[2][5].
2. It prepares the article data in an associative array, including title, alias, content, category ID, and other metadata[5].
3. It initializes a cURL session to make the POST request to the API endpoint[2][5].
4. It sets the necessary headers, including the content type and the API token for authentication[2][5].
5. It sends the request with the article data encoded as JSON[5].
6. It then checks the response. If successful (HTTP code 201), it displays a success message and the new article ID. If there's an error, it displays the error code and message[5].
Remember to replace `'https://your-joomla-site.com'` with your actual Joomla site URL and `'your-api-token-here'` with your actual API token[2][5].
Also, ensure that you have the necessary permissions to create articles via the API, and that the category ID you're using exists on your Joomla site[5].
This example uses cURL, but you could also use Joomla's built-in HTTP class as demonstrated in one of the search results:
php
$apiRequest = Joomla\CMS\Http\HttpFactory::getHttp()->post($apiUrl, json_encode($articleData), $headers);
This approach might be preferable if you're working within a Joomla environment[2].
Citations:[1] https://forum.joomla.org/viewtopic.php?t=974563
[2] https://forum.joomla.org/viewtopic.php?t=1008200
[3] https://joomla.stackexchange.com/questions/779/how-to-build-a-restful-api-with-joomla
[4] https://forum.joomla.org/viewtopic.php?f=706&p=3671742
[5] https://github.com/techjoomla/docs.github.io/blob/master/docs/joomla-rest-api/com-api-mobile-app-use-case.md