Informations about HikAPI and minor fixes

  • Posts: 9
  • Thank you received: 2
  • Hikashop Business
6 years 6 months ago #280423

-- HikaShop version -- : 3.2.0
-- Joomla version -- : 3.8.1
-- PHP version -- : 7.0.21
-- Browser(s) name and version -- : Firefox 7.0.21

Hi!

I am currently on the way of creating a plugin for handling the interaction between Hikashop on Joomla! and another service located on a different site. For this, I thought it would be cleaner to create a kind of Rest API by a plugin, rather than by an external script like i previously used to do for other plugins in Joomla!.

After a little search, i found your pask work: HikAPI , and because it looked quite simple, i choosed to give it a try. It was not fully functional as it was, but actually with 2 minor fixes, it works not bad (see end of message for the fixes).

But there are 2 things that puzzle me a little: when i displayed

$this->routes
in "hikapihelper.php" for a check, it came with almost 50 already declared urls, that really look like there was already a full API declared. To begin with, i have no idea where these values come from, they are already present when i do the display in the first line of the class __construct. Here is a small example of these values:
["/products/:id"]=>
  array(2) {
    ["ctrl"]=>
    string(18) "helper.api-product"
    ["options"]=>
    array(1) {
      ["pagination"]=>
      bool(true)
    }
  }
  ["/products"]=>
  string(11) "/products/0"
  ...

Now there are 2 problems with this, the first is that these urls are not working currently. I tried to mess a little with what's inside the "ctrl" field to try to load something, but I couldn't make it work. The second problem is less important, just that the system doesn't allow me to redeclare these urls. If i want to implement it, i have to use another url.

I know that this project is a bit old, probably left to die, but with very few advices to solve these problems, it could be useful and i would probably gain lots of time.. what i would like to know:
_where do all these already declared urls come from, and were they intended to be functional?
_is there a quick fix i could do to make them work, if they were intended to work? It might be coming from a little change in HikaShop, i can do the fixes if I have little advices on this.

Thanks!

FOR THE FIXES:
_hikapihelper.php line 253: removed the reference here, as it caused all newly declared url to take the "ctrl" value from the last url declaration, caused by a reference chain that makes all "ctrl" fields point to the same value:
'ctrl' => &$ctrl,
becomes
'ctrl' => $ctrl,
_hikapi.php line 82: "$base" comes empty when the joomla site is located in the root of the server, causing "strpos" to fail:
if(strpos($url, $base) !== 0)
becomes
if(!empty($base) && strpos($url, $base) !== 0)
_test.php: for the function "processRequest", the $url comes as declared in the first array (params are not removed). Instead of doing
case '/param':
you have to do
case '/param/:id':
like declared in the array

Please Log in or Create an account to join the conversation.

  • Posts: 26000
  • Thank you received: 4004
  • MODERATOR
6 years 6 months ago #280428

Hello,

HikAPI is a bit old and not really left for die.
I created that plugin for a big development related to the creation of an API system within HikaShop.
It was the part of a big project but unfortunately, the client only paid a small part of the development (first part to start the project) and several months of development are still waiting to be honored.
I can't explain all details like that but we had to stop the project since the client was not paying anymore.
So, in a way, the web service has been postponed even if we still have some work to do on it and a lot of ideas for improvements.

Please delete the file "administrator/components/com_hikashop/helpers/api.php" which is the reason why you have the HikaShop web services rules.
It looks like we forgot to remove that file from the production repository.
You can also change the line 105-112 for the "hikapi.php" file to be sure to load the helper file of the plugin and not the HikaShop file (which have its hard coded rules).

Regarding your fixes.

1 - I'm sorry but I don't understand what you're talking about without a proper use case.
The variable $ctrl can be an object and that's why we use a reference.
So without a code explanation ; I just can't push such patch knowing that it will affect the function while using an object for the $ctrl parameter.

2 - Okay

3 - You're right ; It should be something we forgot when we changed the URL for that sample plugin.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.
The following user(s) said Thank You: marc@ma74.net

Please Log in or Create an account to join the conversation.

  • Posts: 9
  • Thank you received: 2
  • Hikashop Business
6 years 6 months ago #280705

Thanks a lot, that helped much!

I first wanted to settle everything before answering, but i am kind of progressing on different fronts at the same time on this project and a little more time will be needed before all is set, more than that, it would be a bit long for a post here. Maybe it would be simpler if i send you the files at the end rather than explaining all changes (if you are interested, of course), or a commit maybe (not a specialist on git)?

I have already added several things, such as support for Access-Control-Allow-Origin and cache, modified the "auth" function, some things that could interest you, some maybe not.

Concerning the point 1, i am aware that removing the "&" here may not be the ideal solution, i still have to look closer to find why there is this "reference chain" to properly fix it. Note that removing the reference works properly for the tests (probably because i never tried to use an explicit object as 'ctrl'. But the fallback to $this - the plugin class - when 'ctrl' is not set works OK).
Here is a more precise exemple of what i meant by "reference chain". It comes straight from your example file, where you define these urls:

		'/simple' => null, // Simple entry point
		'/param/:id' => null, // Entry point with a specified parameter
		'/test-pagination' => array( // Entry point with pagination
			'options' => array(
				'pagination' => true
			)
		),
		'/extclass' => array( // Entry point which specify an external class to call
			'ctrl' => 'plugin.test.extclass'
		)
As you see, the field 'ctrl' is only set for the last url, to demonstrate how to use an external class. It means that for all the others urls, the 'ctrl' field should be set to $this (the plugin class again) because there is no explicit value.
However, when i did the tests, all urls came with 'plugin.test.extclass' for 'ctrl' value. One way or another, when the last url sets the value as 'plugin.test.extclass', all urls 'ctrl' fields take also this value, probably because they all point to the same reference. So when i let the '&' in 'registerAPI' function, all urls end up having the same 'ctrl' value (actually the last one set). I hope i am clearer..

Thanks again for the quick answer!

Please Log in or Create an account to join the conversation.

  • Posts: 26000
  • Thank you received: 4004
  • MODERATOR
6 years 6 months ago #280720

Hello,

Since the variable "ctrl" is translated ; there is not more reference on the plugin object when you give the parameter ctrl.
CF : github.com/HikaShop/hikapi/blob/master/p...ikapihelper.php#L606
Afterwards, maybe it's a PHP 7 side effect and we might want to unset l_ctrl each time in the loop to be sure that it will not affect something else, but since there is no reference for that l_ctrl variable, it should not affect the ctrl one.

Yes you can see to perform a fork on HikAPI on github and propose a "pull request".
The GitHub interface allow you to modify the files online, you do not need to install a special software in your computer for simple tasks.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

Time to create page: 0.076 seconds
Powered by Kunena Forum