Error testing Joomla Web Services (API) in Joomla 4

General questions relating to Joomla! 4.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Post Reply
dg78
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Thu Mar 28, 2024 9:41 am

Error testing Joomla Web Services (API) in Joomla 4

Post by dg78 » Thu Mar 28, 2024 10:14 am

I posted this in hopes that you professionals who are familiar with the Joomla API can help me.
I myself have been using Joomla for over 10 years, but this is the first time I have used the Joomla API.

I am not an engineer and have no technical knowledge, but with your specific instructions, I would like to fix the error and use the Joomla API.
Below is the status of the problem I am facing and the relevant information.


1. Problem
The simplest article posting API that I have created following the guidance below is not working.

The guidance I referred to:
https://slides.woluweb.be/api/api.html# ... our-script
https://magazine.joomla.org/all-issues/ ... api-part-1
https://magazine.joomla.org/all-issues/ ... api-part-2

API program that does not work:
https://roboblo.com/api-post.php


2. What I want to accomplish
I want to get "api-post.php" working, as exemplified in the following guidance.
https://slides.woluweb.be/api/api.html# ... our-script


3. Additional information
Joomla! Version Joomla! 4.4.3 Stable
PHP Version 8.1.27
Joomla API Token Using the correct information from Super User
Category ID Using the correct information from Article Category


4. Please tell me
What and how should I configure to resolve this error?
If there is any additional information I should include in this thread to determine the cause of this error, please let me know those as well.


Thank you very much for your consideration.

Best Regards,
dg78

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 24986
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: Error testing Joomla Web Services (API) in Joomla 4

Post by pe7er » Thu Mar 28, 2024 11:37 am

How did you try to access Joomla's API?
Via command line (CURL), via a PHP script or using Postman?

Do you get anything back when you try to access the API?
Kind Regards,
Peter Martin, Global Moderator
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com

dg78
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Thu Mar 28, 2024 9:41 am

Re: Error testing Joomla Web Services (API) in Joomla 4

Post by dg78 » Thu Mar 28, 2024 11:53 am

Dear Peter-san,

>How did you try to access Joomla's API?
>Via command line (CURL), via a PHP script or using Postman?

Thank you for your reply.
I followed the guidance below and created the following PHP script

guidance:
https://slides.woluweb.be/api/api.html# ... ost-script

PHP script:
-----------------------------------------------------------
<?php
// Before passing the HTTP METHOD to CURL
$curl = curl_init();

$url = 'https://roboblo.com/api/index.php/v1';

// Put your Joomla! Api token in a safe place, for example a password manager or a vault storing secrets
// We should not use environment variables to store secrets.
// Here is why: https://www.trendmicro.com/en_us/resear ... crets.html
$token = 'c2hhMjU2OjQxNjpmYjJiYzY5NDk3YzlhNzExZjUxZDExNjY0NjczMWMxNGRkYWRmYWRmMjEwMGQ2OWQ0NTQwMjJjYmY3N2YwYjAx';

$categoryId = 11; // Joomla's default "Uncategorized" Category


$data = [
'title' => 'How to add an article to Joomla via the API?',
'alias' => 'how-to-add-article-via-joomla-api',
'articletext' => 'I have no idea...',
'catid' => $categoryId,
'language' => '*',
'metadesc' => '',
'metakey' => '',
];

$dataString = json_encode($data);

// HTTP request headers
$headers = [
'Accept: application/vnd.api+json',
'Content-Type: application/json',
'Content-Length: ' . mb_strlen($dataString),
sprintf('X-Joomla-Token: %s', trim($token)),
];

curl_setopt_array($curl, [
CURLOPT_URL => sprintf('%s/%s',$url,'content/articles'),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => 'utf-8',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2TLS,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $dataString,
CURLOPT_HTTPHEADER => $headers,
]
);

$response = curl_exec($curl);
curl_close($curl);
echo $response;
-----------------------------------------------------------


>Do you get anything back when you try to access the API?

Although I am not a technical person, I also tried Postman according to the manual.
The results were the same as the error displayed at the following URL.

1. URL:
https://roboblo.com/api-post.php
{"errors":[{"code":500,"title":"Internal server error"}]}

2. Postman:

{
"errors": [
{
"code": 500,
"title": "Internal server error"
}
]
}


Best Regards,
dg78


Post Reply

Return to “General Questions/New to Joomla! 4.x”