Select Page

Prevent webservice when asking for an authentication token (webservice key) on every request using PrestaShop

Sunny Kumar
Published: September 2, 2022

Using webservice, PrestaShop allows merchants to give third-party tools access to their shop’s database through a CRUD API. When we visit webservice page in backoffie then we can see that there is two configurations on webservice page in backoffice.

1) Enable PrestaShop’s webservice

2) Enable CGI mode for PHP

The first option is simply used to enable & disable the webservice of PrestaShop.

In this blog, we will understand the concept of “Enable CGI mode for PHP” configuration.

CGI

Let’s Start..!

When we work with PrestaShop then some host support API requests directly without enabling this configuration. But some hosts create some authentication related issues with every API request.

Example: When we hit any API with ws_key in URL but still it asks for the authentication same as below.

Selection_017-1

Reason for issue: Some hosting providers configure Apache to run the PHP interpreter as a CGI binary.

By enabling the “Enable CGI mode for PHP” option we can prevent this authentication token related error.

Let’s understand the role of this configuration:

When we enable this configuration then a rewrite rule is added to the .htaccess file to manage HTTP Authorization. Check the below code.

if (null === $path) {
    $path = _PS_ROOT_DIR_ . '/.htaccess';
}
// Write .htaccess data
if (!$write_fd = @fopen($path, 'wb')) {
    return false;
}
if (Configuration::get('PS_WEBSERVICE_CGI_HOST')) {
    fwrite($write_fd, "RewriteCond %{HTTP:Authorization} ^(.*)\nRewriteRule . - [E=HTTP_AUTHORIZATION:%1]\n\n");
}

You can directly add the below code in the .htaccess file on your hosting server.

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

After making changes in the .htaccess file you can see the error has been resolved.

Selection_018

That’s all about this blog. Hope it will help you.

If you are facing any issues or have any doubts about the above process, please feel free to contact us through the comment section.

Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.

For any doubt contact us at [email protected]

Source: webkul.com