Error making custom API OJS with plugin

Im getting this error in 3.4 stable OJS:

api.404.endpointNotFound

when i go to http://localhost:8081/index.php/test/api/v1/custom/ping

this is my code

<?php
namespace APP\plugins\generic\apiExtensionP;

use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;

use APP\core\Application;
use APP\facades\Repo;
use PKP\security\Role;

class ApiExtensionP extends GenericPlugin
{
    public function register($category, $path, $mainContextId = NULL)
	{

        $success = parent::register($category, $path);

        if ($success && $this->getEnabled()) {

            Hook::add('APIHandler::getAPIEndpoints', [$this, 'addEndpoints']);
        }

        return $success;
    }

  
    public function addEndpoints($hookName, $params) {
        $endpoints =& $params[0];
        $endpoints['GET custom/v1/ping'] = [
            'handler' => [$this, 'handlePing'],
            'roles' => [Role::ROLE_ID_READER]
        ];
    }

    public function handlePing($args, $request) {
        return new JSONMessage(true, ['message' => 'test']);
    }
}

What I’m missing?

Hi @benrusza,

Have you verified that the hook registration function is getting called? If not, maybe you need to register the plugin with your installtion. See:

Regards,
Alec Smecher
Public Knowledge Project Team

1 Like

First of all ty for your fast answer :blush:

I try installing with uploading a zip and seems to work bc is in the plugin list , but, if i try to install with installPluginVersion.php, i get this error:

/var/www/html # php lib/pkp/tools/installPluginVersion.php plugins/apiExtension.tar.gz 
PHP Warning:  SimpleXMLElement::__construct(): Entity: line 1: parser error : Start tag expected, '<' not found in /var/www/html/lib/pkp/classes/site/VersionCheck.php on line 93
PHP Warning:  SimpleXMLElement::__construct():  in /var/www/html/lib/pkp/classes/site/VersionCheck.php on line 93
PHP Warning:  SimpleXMLElement::__construct(): ^ in /var/www/html/lib/pkp/classes/site/VersionCheck.php on line 93
PHP Fatal error:  Uncaught Exception: String could not be parsed as XML in /var/www/html/lib/pkp/classes/site/VersionCheck.php:93
Stack trace:
#0 /var/www/html/lib/pkp/classes/site/VersionCheck.php(93): SimpleXMLElement->__construct()
#1 /var/www/html/lib/pkp/lib/vendor/elcobvg/laravel-opcache/src/Repository.php(25): PKP\site\VersionCheck::PKP\site\{closure}()
#2 /var/www/html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Cache/CacheManager.php(408): ElcoBvg\Opcache\Repository->remember()
#3 /var/www/html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(338): Illuminate\Cache\CacheManager->__call()
#4 /var/www/html/lib/pkp/classes/site/VersionCheck.php(109): Illuminate\Support\Facades\Facade::__callStatic()
#5 /var/www/html/lib/pkp/tools/installPluginVersion.php(61): PKP\site\VersionCheck::parseVersionXML()
#6 /var/www/html/lib/pkp/tools/installPluginVersion.php(111): InstallPluginVersionTool->execute()
#7 {main}
  thrown in /var/www/html/lib/pkp/classes/site/VersionCheck.php on line 93
/var/www/html # 

Seems like the file is empty idk… maybe is a docker related problem?

:confounded:

Hi @benrusza,

You need to specify the path and filename to the plugin’s version.xml, not the .tar.gz file.

Regards,
Alec Smecher
Public Knowledge Project Team

1 Like

The plugin installs well with installPluginVersion.php,and the template hooks works well, but addEndpoints seems never called :
And I have tried different ways, one at a time… Maybe I’m miss spelling something?, I need to reboot the server? sorry for the inconvenience, thanks

Hook::add('APIHandler::getAPIEndpoints', [$this, 'addEndpoints']);
Hook::call('APIHandler::getEndpoints', array($this, 'addEndpoints'));
Hook::call('APIHandler::endpoints', array($this, 'addEndpoints'));

URL:

http://localhost:8081/index.php/test/api/v1/custom

CODE:

public function register($category, $path, $mainContextId = NULL)
	{
        // Register the plugin even when it is not enabled
        $success = parent::register($category, $path);

        if ($success && $this->getEnabled()) {
           
            //Hook::add('APIHandler::getEndpoints', array($this, 'addEndpoints'));
            Hook::call('APIHandler::endpoints', array($this, 'addEndpoints'));
            Hook::add('TemplateManager::display', array($this, 'callbackTemplateDisplay'));
            
        }

        return $success;
    }

    

    public function addEndpoints($hookName, $params) {
        $endpoints =& $params[0];
        $endpoints = array_merge_recursive($endpoints, [
            'GET' => [
                [
                    'pattern' => '/{contextPath}/api/{version}/'."custom",
                    'handler' => [$this, 'handlePing'],
                    
                ]
            ]
        ]);
    }

Hi @benrusza,

Does your plugin show up in the versions table, and are the product and product_class_name columns set appropriately (the plugin’s immediate pathname and PHP class name, respectively)?

Regards,
Alec Smecher
Public Knowledge Project Team

Hello @asmecher it is correctly set .

Regards.

Hi @benrusza,

The right syntax for registering for the APIHandler::endpoints hook is:

Hook::add('APIHandler::endpoints', [$this, 'addEndpoints']);

Regards,
Alec Smecher
Public Knowledge Project Team