Using the Webservices API to read an article Topic is solved

For Joomla! 5.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
davidascher
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Tue Feb 28, 2006 3:23 pm

Using the Webservices API to read an article

Post by davidascher » Thu Mar 28, 2024 9:39 pm

I've been attempting to use the sample code in a module at
https://github.com/alexandreelise/Manua ... ervices.md
using the Joomla Framework to read the text of a specific article.

Things seem to go find until I get the response. It appears from this part of the response:

["reasonPhrase":"Laminas\Diactoros\Response":private]=>
string(9) "Not Found"
["statusCode":"Laminas\Diactoros\Response":private]=>
int(404)

I have tried getting the content of other articles by changing the articleId in my code, but I get the same result.

that the article I asked for was not found - however, I KNOW the article exists. Is there some common dumb mistake I might be making?

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

Re: Using the Webservices API to read an article

Post by pe7er » Fri Mar 29, 2024 8:05 am

Could you try to access the article using Postman and the Joomla 4 Web Services API Collection for Postman:
https://github.com/alexandreelise/j4x-api-collection

Or try Using the PHP cURL Functions:
https://manual.joomla.org/docs/general- ... -functions
Kind Regards,
Peter Martin, Global Moderator
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17445
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Using the Webservices API to read an article

Post by toivo » Fri Mar 29, 2024 1:35 pm

This is an interesting topic! The Postman app worked all right when accessing a Joomla 5 article from a browser, as soon as the Joomla API token was set up as Bearer Token.

Also tested a quick and dirty custom component from Joomla 4 test site to display the text of an article from a Joomla 5 site using the PHP cURL functions, as documented in the link @pe7er posted.
Toivo Talikka, Global Moderator

davidascher
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Tue Feb 28, 2006 3:23 pm

Re: Using the Webservices API to read an article

Post by davidascher » Fri Mar 29, 2024 9:31 pm

I can successfully read the contents of an article with cURL (I think I may have already written that). I can also read the contents of the same article with Postman. That would seem to indicate that everything is set up as it should be at the webservices server side. I think it is safe to assume that Postman does not use the Joomla Framework - and that's where I'm have a problem.

I am suspicious that variable $response in the line
$response = $http->request('GET', $uri, $dataString, $headers, $timeout)   ;
needs to be initialized as something that it is not currently. It is apparently a "Http Response Object". I cannot see how to see its contents. I am especially interested in the 'text' field but ... What am I missing?

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2909
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Using the Webservices API to read an article

Post by SharkyKZ » Fri Mar 29, 2024 10:20 pm

Use $response->getBody() to read the body. It will be in the form of Psr\Http\StreamInterface object. Cast it to a string.

davidascher
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Tue Feb 28, 2006 3:23 pm

Re: Using the Webservices API to read an article

Post by davidascher » Sat Mar 30, 2024 10:18 pm

I tried casting to string and still don't get a usable result from
$response->getBody()

The result of that is NULL.

I must still be missing something.

Things work perfectly using curl.

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17445
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Using the Webservices API to read an article

Post by toivo » Sat Mar 30, 2024 10:41 pm

Can't you then just use the cURL method? It is not going to disappear overnight.
Toivo Talikka, Global Moderator

davidascher
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Tue Feb 28, 2006 3:23 pm

Re: Using the Webservices API to read an article

Post by davidascher » Sat Mar 30, 2024 11:28 pm

toivo wrote:
Fri Mar 29, 2024 1:35 pm
This is an interesting topic! The Postman app worked all right when accessing a Joomla 5 article from a browser, as soon as the Joomla API token was set up as Bearer Token.

Also tested a quick and dirty custom component from Joomla 4 test site to display the text of an article from a Joomla 5 site using the PHP cURL functions, as documented in the link @pe7er posted.
Both Postman and your quick and dirty component are using curl. I thought the Joomla framework for webservices has been working for over a year and is well tested and reliable. It's not working for me. Only curl is working for me

davidascher
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Tue Feb 28, 2006 3:23 pm

Re: Using the Webservices API to read an article

Post by davidascher » Sat Mar 30, 2024 11:30 pm

toivo wrote:
Sat Mar 30, 2024 10:41 pm
Can't you then just use the cURL method? It is not going to disappear overnight.
Yes, I could do that and will do that unless I have a sudden insight into why the webservices stuff isn't working for me. From what I've read, it has worked for other people - so why not for me?

davidascher
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Tue Feb 28, 2006 3:23 pm

Re: Using the Webservices API to read an article

Post by davidascher » Tue Apr 09, 2024 9:22 pm

I've managed to get the GET and PATCH webservices working using the PHP Curl interface - no Joomla and now I have a related question.

I can see from the samples that I can request all the articles in a category by using something like:
https://test.peacecoalition.org/api/ind ... ategory]=2

Is 'category' the ONLY filter available?? it would be very nice to be able to retrieve articles filtered by other attributes - and/or to retrieve an article by its alias or by its name. I have tried
https://test.peacecoalition.org/api/ind ... alias-name'
and
https://test.peacecoalition.org/api/ind ... alias-name
and neither of those seem to do anything at all. I get back ALL the articles on the site.

Have I missed something? Is this kind of filter thing implemented ONLY for category (which should probably really have been catId)?

Is there documentation of this filter thing beyond its appearance in the sample code??

davidascher
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Tue Feb 28, 2006 3:23 pm

Re: Using the Webservices API to read an article

Post by davidascher » Thu Apr 11, 2024 11:39 pm

I have apparently violated some forum rule by posting my last reply... I would be more than happy to do "the right thing", whether that is opening a new post, checking out the documentation (if I could locate it") or something else.

I find it difficult to understand the complete silence in response to my inquiry and equally difficult to believe that there is no way to retrieve the attributes of a articles filtered by attribute values other than category.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2909
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Using the Webservices API to read an article

Post by SharkyKZ » Fri Apr 12, 2024 5:33 am

I'm guessing you your last post was a self-bump. A response is not guaranteed on this forum. Or it could be days, weeks, even months until someone decides to respond. In this case I think most people are in the same boat as you — sadly API filters are not documented anywhere, as far as I know. Anyways, these are the supported filters for articles: author, category, search, state, featured, tag, and language.

davidascher
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Tue Feb 28, 2006 3:23 pm

Re: Using the Webservices API to read an article

Post by davidascher » Fri Apr 12, 2024 1:28 pm

SharkyKZ - thank you very much for breaking the silence. I'm not enough of a forum user to have intentionally 'self bumped' - but I now get the idea. Some people seem to know quite a bit about the Webservices stuff despite the lack of documentation. I presume they are either among the developers of this stuff or people who have spent much more time than most of us have poking around in the code and experimenting to figure out what kind of magic incantation they should send in a Webservices request to get anything beyond the same code examples to do what they want to do. I'd have sort of expected that somebody from one of those two cohorts would have written a (possibly impolite) note to let me know that what I was wanting to do cannot be done - yet? - or that it could be done and how to do it... Or at least a pointer to some presentation/documentation/video that explains it.

I understand that this is all voluntary and I don't have expectations that somebody owes me a response. I was just hoping that human decency and pride would be enough to stimulate responses from the appropriate individuals.

Now that we have that of of the way - I understand filter by author, category, state, featured, tag, and language. What is filter by "search"?? Does it search ALL the attributes of ALL the articles looking for a match to some string? Or just search the text attribute? What's the structure of a search request? (Can it be told in which attribute[s] it should search?)

How do you know this? The example code only shows filter by category. The rest seems to be up to the 'student' to figure out.

Thank you very much for your kind reply - and anything else you can share.

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

Re: Using the Webservices API to read an article

Post by pe7er » Fri Apr 12, 2024 1:58 pm

davidascher wrote:
Fri Apr 12, 2024 1:28 pm
Some people seem to know quite a bit about the Webservices stuff despite the lack of documentation. I presume they are either among the developers of this stuff or people who have spent much more time than most of us have poking around in the code and experimenting to figure out what kind of magic incantation they should send in a Webservices request to get anything beyond the same code examples to do what they want to do. I'd have sort of expected that somebody from one of those two cohorts would have written a (possibly impolite) note to let me know that what I was wanting to do cannot be done - yet? - or that it could be done and how to do it... Or at least a pointer to some presentation/documentation/video that explains it.
See Joomla Community Magazine:
https://magazine.joomla.org/all-issues/ ... api-part-1
https://magazine.joomla.org/all-issues/ ... api-part-2
https://magazine.joomla.org/all-issues/ ... api-part-3
https://magazine.joomla.org/all-issues/ ... api-part-4

or see https://slides.woluweb.be/ "Playing with the Joomla Web Services (API) in Joomla 4 or 5 or 6"

Now that we have that of of the way - I understand filter by author, category, state, featured, tag, and language. What is filter by "search"?? Does it search ALL the attributes of ALL the articles looking for a match to some string? Or just search the text attribute? What's the structure of a search request? (Can it be told in which attribute[s] it should search?)
The filters that you can use are the same as in the Joomla core code, e.g. the Articles Model:
https://github.com/joomla/joomla-cms/bl ... sModel.php
Kind Regards,
Peter Martin, Global Moderator
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com

User avatar
Kubik-Rubik
Joomla! Explorer
Joomla! Explorer
Posts: 269
Joined: Wed Aug 25, 2010 1:59 pm
Location: Karlsruhe - Germany
Contact:

Re: Using the Webservices API to read an article

Post by Kubik-Rubik » Fri Apr 12, 2024 2:32 pm

Hey David,

This is how you can use the HTTP class provided by the core for your API requests to retrieve the article text:

Code: Select all

$apiKey = 'API-KEY';
$apiUrl = 'https://DOMAIN/api/index.php/v1/content/articles/ARTICLE-ID';
$headers = ['X-Joomla-Token' => $apiKey];

try {
    $apiRequest = Joomla\CMS\Http\HttpFactory::getHttp()->get($apiUrl, $headers);
} catch (Exception $e) {
    // Error handling - Log error message
}

$returnCode = $apiRequest->getStatusCode();

if ($returnCode !== 200) {
    // Error handling - throw Exception() or log error message
}

$returnBody = $apiRequest->body;

if ($returnBody === '') {
    // Error handling - throw Exception() or log error message
}

$body = json_decode($returnBody, true, 512, JSON_THROW_ON_ERROR);

$articeText = $body['data']['attributes']['text'] ?? '';
Replace the placeholders in your code: API-KEY, DOMAIN and ARTICLE-ID
Cheers
Viktor

Kubik-Rubik Joomla! Extensions https://kubik-rubik.de
Former member of Joomla! Production Leadership Team, Security Strike Team and Bug Squash Team

User avatar
Kubik-Rubik
Joomla! Explorer
Joomla! Explorer
Posts: 269
Joined: Wed Aug 25, 2010 1:59 pm
Location: Karlsruhe - Germany
Contact:

Re: Using the Webservices API to read an article

Post by Kubik-Rubik » Fri Apr 12, 2024 2:34 pm

Optionally, you may use the getBody() method and cast it to a string:

Code: Select all

(string)$apiRequest->getBody()
Cheers
Viktor

Kubik-Rubik Joomla! Extensions https://kubik-rubik.de
Former member of Joomla! Production Leadership Team, Security Strike Team and Bug Squash Team

davidascher
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Tue Feb 28, 2006 3:23 pm

Re: Using the Webservices API to read an article

Post by davidascher » Sun Apr 14, 2024 7:29 pm

Victor - thank you for that sample code. I will give it a shot.

davidascher
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Tue Feb 28, 2006 3:23 pm

Re: Using the Webservices API to read an article

Post by davidascher » Sun Apr 14, 2024 7:37 pm

I'm still unable to get ANY filter to work at all.
For example, I have tried using Postman with
https://t2.peacecoalition.org/api/index ... featured=1
or
https://t2.peacecoalition.org/api/index ... ewsletters
or anything else I could think of for the filter value and in every case I got back 20 articles and this

Code: Select all

  "links": {
        "self": "https://t2.peacecoalition.org/api/index.php/v1/content/articles?[filter]alias=newsletters",
        "next": "https://t2.peacecoalition.org/api/index.php/v1/content/articles?page%5Boffset%5D=20&page%5Blimit%5D=20",
        "last": "https://t2.peacecoalition.org/api/index.php/v1/content/articles?page%5Boffset%5D=560&page%5Blimit%5D=20"
    }
As far as I can tell the filter in my request was completely ignored and ALL the articles would eventually be retrieved if I followed the "next" links. Is there still something I am doing incorrectly when I submit my innitial request? I'm guessing that the limit to 20 articles at a shot is a Postman thing... but I have ONLY ONE Article with the alias "newsletters" and that's the only one I want to retrieve.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2909
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Using the Webservices API to read an article

Post by SharkyKZ » Sun Apr 14, 2024 8:13 pm

It should be filter[featured], not [filter]featured.

User avatar
Kubik-Rubik
Joomla! Explorer
Joomla! Explorer
Posts: 269
Joined: Wed Aug 25, 2010 1:59 pm
Location: Karlsruhe - Germany
Contact:

Re: Using the Webservices API to read an article

Post by Kubik-Rubik » Mon Apr 15, 2024 9:56 am

Hey David,

you are welcome. As SharkyKZ said, you need to send the query parameter properly.

Example:

Code: Select all

?filter[tag]=Joomla
You may set the following filter elements:

Code: Select all

author
category
search
state
featured
tag
language
Example code for all featured and published articles with the tag Joomla from the author with the ID 42:

Code: Select all

DOMAIN/api/index.php/v1/content/articles?filter[tag]=Joomla&filter[author]=42&filter[featured]=1&filter[state]=1
Replace the placeholder DOMAIN with your domain.

Have success!
Cheers
Viktor

Kubik-Rubik Joomla! Extensions https://kubik-rubik.de
Former member of Joomla! Production Leadership Team, Security Strike Team and Bug Squash Team

davidascher
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Tue Feb 28, 2006 3:23 pm

Re: Using the Webservices API to read an article

Post by davidascher » Mon Apr 15, 2024 1:50 pm

Kubik-Rubik wrote:
Mon Apr 15, 2024 9:56 am
Hey David,

you are welcome. As SharkyKZ said, you need to send the query parameter properly.

Example:

Code: Select all

?filter[tag]=Joomla
You may set the following filter elements:

Code: Select all

author
category
search
state
featured
tag
language
Thank you very much for this information! Other than through examining the code (and understanding it) how would somebody know this??

It seems a pity that one cannot retrieve a single article by its name or alias. That seems not unreasonable for the human interface (listing articles in the backend) but for the Webservices API I'd have expected to see at least get article by alias. I suppose that could be done with a little override - if one could figure out how the code actually works.

User avatar
Kubik-Rubik
Joomla! Explorer
Joomla! Explorer
Posts: 269
Joined: Wed Aug 25, 2010 1:59 pm
Location: Karlsruhe - Germany
Contact:

Re: Using the Webservices API to read an article

Post by Kubik-Rubik » Mon Apr 15, 2024 2:26 pm

You are welcome!

For the alias, you may use the normal search filter:

Code: Select all

DOMAIN/api/index.php/v1/content/articles?filter[search]=newsletters
Cheers
Viktor

Kubik-Rubik Joomla! Extensions https://kubik-rubik.de
Former member of Joomla! Production Leadership Team, Security Strike Team and Bug Squash Team

User avatar
Kubik-Rubik
Joomla! Explorer
Joomla! Explorer
Posts: 269
Joined: Wed Aug 25, 2010 1:59 pm
Location: Karlsruhe - Germany
Contact:

Re: Using the Webservices API to read an article

Post by Kubik-Rubik » Mon Apr 15, 2024 2:38 pm

By the way, the search filter is quite powerful. You may define a specific article directly using the id: prefix.

Code: Select all

DOMAIN/api/index.php/v1/content/articles?filter[search]=id:X
Replace X with the correct ID of your newsletter article.

A list of possible prefixes:

Code: Select all

id:
author:
content:
Have success!
Cheers
Viktor

Kubik-Rubik Joomla! Extensions https://kubik-rubik.de
Former member of Joomla! Production Leadership Team, Security Strike Team and Bug Squash Team

davidascher
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Tue Feb 28, 2006 3:23 pm

Re: Using the Webservices API to read an article

Post by davidascher » Mon Apr 15, 2024 2:50 pm

Kubik-Rubik wrote:
Mon Apr 15, 2024 2:38 pm
By the way, the search filter is quite powerful. You may define a specific article directly using the id: prefix.

Code: Select all

DOMAIN/api/index.php/v1/content/articles?filter[search]=id:X
Given that there is already the possibility of using

Code: Select all

DOMAIN/api/index.php/v1/content/articles/articleId
Why would anybody want to use a filter like that?
Last edited by toivo on Mon Apr 15, 2024 10:05 pm, edited 1 time in total.
Reason: mod note: added /quote tag

User avatar
Kubik-Rubik
Joomla! Explorer
Joomla! Explorer
Posts: 269
Joined: Wed Aug 25, 2010 1:59 pm
Location: Karlsruhe - Germany
Contact:

Re: Using the Webservices API to read an article

Post by Kubik-Rubik » Mon Apr 15, 2024 2:58 pm

davidascher wrote:
Mon Apr 15, 2024 2:50 pm
Why would anybody want to use a filter like that?
This feature was implemented for the search in the UI. You can use the search to filter by IDs with this prefix. So, this prefix also works in the API.
Cheers
Viktor

Kubik-Rubik Joomla! Extensions https://kubik-rubik.de
Former member of Joomla! Production Leadership Team, Security Strike Team and Bug Squash Team

davidascher
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Tue Feb 28, 2006 3:23 pm

Re: Using the Webservices API to read an article

Post by davidascher » Mon Apr 15, 2024 3:06 pm

Kubik-Rubik wrote:
Mon Apr 15, 2024 2:26 pm
You are welcome!

For the alias, you may use the normal search filter:

Code: Select all

DOMAIN/api/index.php/v1/content/articles?filter[search]=newsletters
This request would return all articles with the string "newsletters" in their name or alias. Additional filtering by category or author could narrow it down, or additional code after the response comes back could do that, but it seems to me that the most common use case would be search for an exact match (to an alias at least). Unless there are additional arcane incantations available to to that, this seems to me to be a most useful addition for the next version of Joomla webservices.

Thank you again for putting up with my questions.

davidascher
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Tue Feb 28, 2006 3:23 pm

Re: Using the Webservices API to read an article

Post by davidascher » Mon Apr 15, 2024 8:08 pm

It STILL seems a pity that one cannot retrieve a single article by its EXACT name or alias. Returning all articles with a substring match seems not unreasonable for the human interface (listing articles in the backend) but for the Webservices API I'd have expected to see at least get article by EXACT alias - and/or maybe with wildcard capabilities. I suppose that could be done with a little override - if one could figure out how the code actually works.

It also seems that if you have an /content/articles/articleId it's not likely that anybody is going to want to use /content/articles?filter[search]=id:X. Perhaps it wouldn't be difficult to extend the webservices to support
/content/articles/alias similar to how /content/articles/articleId is implemented. If the 'basename' is numeric, then it is an articleId; if it is alphanumeric then it is an alias.

User avatar
Kubik-Rubik
Joomla! Explorer
Joomla! Explorer
Posts: 269
Joined: Wed Aug 25, 2010 1:59 pm
Location: Karlsruhe - Germany
Contact:

Re: Using the Webservices API to read an article

Post by Kubik-Rubik » Mon Apr 15, 2024 8:15 pm

What's wrong with using the article's ID directly instead of the title or alias?

PS: You don't need to use the prefix filter (?filter[search]=id:X) to retrieve a specific article, always prefer the corresponding endpoint for it. It was just an example to show you what is possible with the search filter parameter.
Cheers
Viktor

Kubik-Rubik Joomla! Extensions https://kubik-rubik.de
Former member of Joomla! Production Leadership Team, Security Strike Team and Bug Squash Team

davidascher
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Tue Feb 28, 2006 3:23 pm

Re: Using the Webservices API to read an article

Post by davidascher » Mon Apr 15, 2024 8:42 pm

Shouldn't a request with a bad value for filter return some kind of error indication? For example,

Code: Select all

https://test.peacecoalition.org/api/index.php/v1/content/articles?filter[title]=newsletters
does not return an error. I understand that 'title' is not supported. For webservices to be ready to unleash on an unsuspecting public, I'd have expected it to be a bit more bulletproofed - some return message like 'invalid filter' or something. Instead, it returns all the articles (unfiltered) as if there is no problem.

Are you two guys part of the team that worked on the webservices implementation? I'd be happy to submit a "formal suggestion for improvement" if there is a mechanism for doing that.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2909
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Using the Webservices API to read an article

Post by SharkyKZ » Mon Apr 15, 2024 8:48 pm

You can submit the suggestion on the issue tracker https://github.com/joomla/joomla-cms/issues.


Post Reply

Return to “Joomla! 5.x Coding”