By Marc Dechèvre on Thursday, 20 April 2023
Category: April

Playing with the Joomla Web Services (API) - part 2

[ this article is part of a series - see part 1 here ]

4. Create, Update, Fetch and Delete Joomla Articles with the API

4.1. Create Joomla Articles with the API

4.1.1. Terminology

Some basic terminology first: When using the Joomla API

4.1.2. See the Articles Manager

This is a fresh website so I see that “No Articles have been created yet”

4.1.3. Prepare the POST script

Create your POST script on your website

Customize your POST script on your website

4.1.4. articletext vs introtext and fulltext

Good to know: when you Create an article

4.1.5. The 4 necessary fields

To create an article

4.1.6. Execute your script

Simply

4.1.7. Go back to or refresh the Articles Manager

Tadam you can see that the Article was indeed created!

4.2. Update Joomla Articles with the API

4.2.1. Prepare the PATCH script

The procedure is totally similar to the one of the previous script:

Create your PATCH script on your website

Customize your PATCH script on your website

Good to know: when you Update an article

4.2.2. Execute your script

Simply execute your script as already explained above.

4.2.3. Go back to or refresh the Articles Manager

When opening the Article, we can indeed check that

4.3. Playing with Custom Fields

Adding a value to some Custom Field of an Article directly in the database using a query in PHPMyAdmin is not so obvious because it involves several Tables which are linked.

But as you will see, adding a value to some Custom Field of an Article via the Joomla API is super easy.

4.3.1. Create a Custom Field

In this example I create a Custom Field of Type “Text”

4.3.2. Adapt the PATCH script

Now I need to edit my api-patch script as follows

4.3.3. Execute your script

Simply execute your script as already explained above.

You already see on what is displayed that “temperature” is mentioned.

4.3.4. Go back to or refresh the Articles Manager

When opening the Article, we can indeed check that

4.4. Fetch Joomla Articles with the API

A good usecase for this script: an intranet or an app where one wants to display some news (articles) from your Joomla website

4.4.1. Prepare the GET script

Create your GET script on your website

Customize your GET script on your website

4.4.2. Bonus 1: select another Category

In this example, we suppose we want to get all articles from Category 2

Of course, you can easily change the ID of the Category you need by adapting the following line of code:

$categoryId = 2;

4.4.3. Bonus 2: only get Published Articles

What is you only want to get Articles which are Published.

In the database, you probably know that technically being

So all we need to change in the script is to change

/content/articles?filter[category]=

into

/content/articles?filter[state]=1&filter[category]=

4.4.4. Execute the script

Simply execute your script as already explained above.

4.5. Delete Joomla Articles with the API

Create your DELETE script on your website

Customize your DELETE script on your website

We don’t provide screenshots here. By now you know how it works!

 
Leave Comments