Advertisement

Help required with Joomla API connection Topic is solved

For Joomla! 4.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.
Post Reply
ontarget
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Mon Sep 17, 2012 8:29 am

Help required with Joomla API connection

Post by ontarget » Wed Dec 04, 2024 9:02 am

Dear Joomla! community,
I have a j!4.4.9 website using a popular events booking component.
I want to be able to display events from this "parent" website onto several other (joomla) websites.
Ideally i would do this using a module on each third party site.
Rather than writing a db connection to the parent site table, on every third party site to pull data, I would like to use an API.
I have tried the following method but cannot get the connection to work.
Created dir and files:
public_html/plugins/webservices/ontarget
-ontarget.php
-ontarget.xml

Zipped them and installed and enabled the plugin.

php code

Code: Select all

<?php
defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Input\Input;
use Joomla\CMS\Application\WebApplication;

class PlgWebservicesOntarget extends CMSPlugin
{
    protected $autoloadLanguage = true;

    public function onWebserviceGetOntarget(Input $input, WebApplication $app)
    {
        try {
            
            $db = Factory::getDbo();
            
       
            $query = $db->getQuery(true)
                        ->select('*')
                        ->from($db->quoteName('#__eb_events'));
            $db->setQuery($query);

            $results = $db->loadObjectList();

           
            $app->setHeader('Content-Type', 'application/json', true);
            echo json_encode(['status' => 'success', 'data' => $results]);
            $app->close();
        } catch (Exception $e) {
            
            $app->setHeader('Content-Type', 'application/json', true);
            echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
            $app->close();
        }
    }
}
xml code

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<extension type="plugin" version="4.0" group="webservices" method="upgrade">
    <name>PLG_WEBSERVICES_ONTARGET</name>
    <author>OnTarget</author>
    <creationDate>2024-12-04</creationDate>
    <copyright>Copyright (C) 2024 OnTarget. All rights reserved.</copyright>
    <license>GNU General Public License version 2 or later; see LICENSE.txt</license>
    <version>1.0.0</version>
    <description>Plugin to expose eb_events data via Joomla API.</description>
    <files>
        <filename plugin="ontarget">ontarget.php</filename>
    </files>
</extension>
I then call up the API using
https://esci.ie/api/index.php/v1/ontarget

However the json return is:

Code: Select all

{
  "errors": [
    {
      "title": "Resource not found",
      "code": 404
    }
  ]
}
Unfortunately I cant find any documentation on how to do this and I'm not sure what my problem is - is joomla blocking the connection ?
I appreciate any guidance on this issue.

Advertisement
SharkyKZ
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 3127
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Help required with Joomla API connection

Post by SharkyKZ » Wed Dec 04, 2024 9:09 am

There is some documentation on the matter. Apparently, a component is also required https://manual.joomla.org/docs/web-serv ... ces-plugin.

ontarget
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Mon Sep 17, 2012 8:29 am

Re: Help required with Joomla API connection

Post by ontarget » Wed Dec 04, 2024 1:27 pm

Thanks for the link to the documentation.
I have managed to create the API (but i didnt use the joomla component / plugin method)
I am now having another issue.
Using code mirror I put my code to call the API into a joomla module.
Despite using Absolute URLs in my code to generate the links and the img src the joomla site that is calling my API always prepend these links with the site url.
My API call is fetching the event id from the table and i wish to link to it.

Code: Select all

const id = event.id;
const eventLink = `https://esci.ie/index.php?option=com_eventbooking&view=event&id=${id}`;

<a href="${eventLink}" target="_blank" rel="noopener noreferrer">View Event</a>
The link renders as :
https://www.mywebsite.com/https://esci. ... event&id=1
Despite using an Absolute URL

If i put my code into the root directory of the server as a bog standard html page the links work perfectly.
Why does joomla ignore my absolute url reference and prepend it with the site url?

ontarget
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Mon Sep 17, 2012 8:29 am

SOLVED Re: Help required with Joomla API connection

Post by ontarget » Wed Dec 04, 2024 4:30 pm

Ok I solved this:

Code: Select all

const eventLink = `href ="${esciURL}index.php?option=com_eventbooking&view=event&id=${id}"`;

<a ${eventLink}>View Event</a>

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

Re: Help required with Joomla API connection

Post by SharkyKZ » Wed Dec 04, 2024 6:24 pm

You should avoid concatenating HTML in JavaScript. This could easily lead to XSS vulnerabilities. Use document.createElement() to create new elements instead https://developer.mozilla.org/en-US/doc ... ateElement.

ontarget
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Mon Sep 17, 2012 8:29 am

Re: Help required with Joomla API connection

Post by ontarget » Wed Dec 04, 2024 11:17 pm

Thanks for the heads up js isnt my strong point! I changed that now and it works great.

Advertisement

Post Reply

Return to “Joomla! 4.x Coding”