Quantcast
Channel: Joomla! Forum - community, help and support
Viewing all articles
Browse latest Browse all 2381

Joomla! 5.x Coding • Re: Using the Webservices API to read an article

$
0
0
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:

$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

Statistics: Posted by Kubik-Rubik — Fri Apr 12, 2024 2:32 pm



Viewing all articles
Browse latest Browse all 2381

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>