Skip to content

Commit 647588e

Browse files
authored
Update webservices.md
Added PHP code examples to communicate with API endpoint of Content > Articles
1 parent 52da241 commit 647588e

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

docs/general-concept/webservices.md

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,160 @@ Description of the webservices concept
66
This page is unfinished, please use the **Edit this Page** link at the bottom of this page to help make it more useful.
77

88
:::
9+
10+
# Communicate with the Joomla 4 Web Services API
11+
The communication with Joomla's Web Services API takes place via specified endpoints.
12+
- Joomla's core endpoints: https://docs.joomla.org/J4.x:Joomla_Core_APIs
13+
- A collection of Joomla endpoints to use in Postman: https://github.com/alexandreelise/j4x-api-collection
14+
15+
## Using the Joomla framework
16+
:::caution TODO
17+
18+
## Using the PHP cURL Functions
19+
The cURL functions needs to be available and enabled in your PHP configuration, check phpinfo();
20+
21+
### Define some variables
22+
First we define some variables that we use in all our cURL requests:
23+
- the URL of your Joomla 4 website and
24+
- the Joomla's API Token of the Super User account.
25+
26+
```php
27+
// Before passing the HTTP METHOD to CURL
28+
$curl = curl_init();
29+
$url = 'http://example.com/api/index.php/v1';
30+
$token = 'abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz12';
31+
```
32+
33+
### POST - Create an Article in the Category "Uncategorized" (Category ID = 2)
34+
```php
35+
$categoryId = 2; // Joomla's default "Uncategorized" Category
36+
curl_setopt_array($curl, [
37+
CURLOPT_URL => $url . '/content/articles',
38+
CURLOPT_RETURNTRANSFER => true,
39+
CURLOPT_ENCODING => '',
40+
CURLOPT_MAXREDIRS => 10,
41+
CURLOPT_TIMEOUT => 0,
42+
CURLOPT_FOLLOWLOCATION => true,
43+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
44+
CURLOPT_CUSTOMREQUEST => 'POST',
45+
CURLOPT_POSTFIELDS => [
46+
'id' => 0,
47+
'title' => 'How to add an article to Joomla via the API?',
48+
'articletext' => 'I have no idea...',
49+
'catid' => $categoryId,
50+
'language' => '*',
51+
'metadesc' => '',
52+
'metakey' => '',
53+
],
54+
CURLOPT_HTTPHEADER => [
55+
'Content-Type: application/json',
56+
'Authorization: Bearer ' . $token
57+
],
58+
]
59+
);
60+
61+
$response = curl_exec($curl);
62+
curl_close($curl);
63+
echo $response;
64+
```
65+
66+
### GET - Retrieve all articles from the "Uncategorized" Category
67+
```php
68+
$categoryId = 2; // Joomla's default "Uncategorized" Category
69+
curl_setopt_array($curl, [
70+
CURLOPT_URL => $url . '/content/articles?filter[category_id]=' . $categoryId,
71+
CURLOPT_RETURNTRANSFER => true,
72+
CURLOPT_ENCODING => '',
73+
CURLOPT_MAXREDIRS => 10,
74+
CURLOPT_TIMEOUT => 0,
75+
CURLOPT_FOLLOWLOCATION => true,
76+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
77+
CURLOPT_CUSTOMREQUEST => 'GET',
78+
CURLOPT_HTTPHEADER => [
79+
'Content-Type: application/json',
80+
'Authorization: Bearer ' . $token
81+
],
82+
]
83+
);
84+
85+
$response = curl_exec($curl);
86+
curl_close($curl);
87+
echo $response;
88+
```
89+
90+
### GET - Retrieve one specific Article
91+
```php
92+
$articleId = 1; // The Article ID of a specific Article
93+
curl_setopt_array($curl, [
94+
CURLOPT_URL => $url . '/content/articles/' . $articleId,
95+
CURLOPT_RETURNTRANSFER => true,
96+
CURLOPT_ENCODING => '',
97+
CURLOPT_MAXREDIRS => 10,
98+
CURLOPT_TIMEOUT => 0,
99+
CURLOPT_FOLLOWLOCATION => true,
100+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
101+
CURLOPT_CUSTOMREQUEST => 'GET',
102+
CURLOPT_HTTPHEADER => [
103+
'Content-Type: application/json',
104+
'Authorization: Bearer ' . $token
105+
],
106+
]
107+
);
108+
109+
$response = curl_exec($curl);
110+
curl_close($curl);
111+
echo $response;
112+
```
113+
114+
### PATCH - Modify a specific Article
115+
```php
116+
$articleId = 1; // The Article ID of a specific Article
117+
curl_setopt_array($curl, [
118+
CURLOPT_URL => $url . '/content/articles/' . $articleId,
119+
CURLOPT_RETURNTRANSFER => true,
120+
CURLOPT_ENCODING => '',
121+
CURLOPT_MAXREDIRS => 10,
122+
CURLOPT_TIMEOUT => 0,
123+
CURLOPT_FOLLOWLOCATION => true,
124+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
125+
CURLOPT_CUSTOMREQUEST => 'PATCH',
126+
CURLOPT_POSTFIELDS => [
127+
'id' => $articleId,
128+
'title' => 'How to add an article via the Joomla 4 API?',
129+
'articletext' => 'Use the HTTP POST method at the /content/articles endpoint.'
130+
],
131+
CURLOPT_HTTPHEADER => [
132+
'Content-Type: application/json',
133+
'Authorization: Bearer ' . $token
134+
]
135+
]
136+
);
137+
138+
$response = curl_exec($curl);
139+
curl_close($curl);
140+
echo $response;
141+
```
142+
143+
### DELETE - Remove a specific Article
144+
```php
145+
$articleId = 1; // The Article ID of a specific Article
146+
curl_setopt_array($curl, [
147+
CURLOPT_URL => $url . '/content/articles/' . $articleId,
148+
CURLOPT_RETURNTRANSFER => true,
149+
CURLOPT_ENCODING => '',
150+
CURLOPT_MAXREDIRS => 10,
151+
CURLOPT_TIMEOUT => 0,
152+
CURLOPT_FOLLOWLOCATION => true,
153+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
154+
CURLOPT_CUSTOMREQUEST => 'DELETE',
155+
CURLOPT_HTTPHEADER => [
156+
'Content-Type: application/json',
157+
'Authorization: Bearer ' . $token
158+
]
159+
]
160+
);
161+
162+
$response = curl_exec($curl);
163+
curl_close($curl);
164+
echo $response;
165+
```

0 commit comments

Comments
 (0)