Retrive Configuration data via API
-
02-03-2021 - |
Frage
Is Magento giving any default REST API for retrieving configuration (system.xml
) data ?.
If yes then share URL.
Else share the best approach for retrieving configuration data via REST API.
Lösung
Magento has a single Rest API which provides some system.xml field value
<route url="/V1/store/storeConfigs" method="GET">
This only provides below system config fields value:
[
{
"id": 1,
"code": "default",
"website_id": 1,
"locale": "en_US",
"base_currency_code": "USD",
"default_display_currency_code": "USD",
"timezone": "America/Chicago",
"weight_unit": "lbs",
"base_url": "http://127.0.0.1/magento23dev/",
"base_link_url": "http://127.0.0.1/magento23dev/",
"base_static_url": "http://127.0.0.1/magento23dev/pub/static/version1554979401/",
"base_media_url": "http://127.0.0.1/magento23dev/pub/media/",
"secure_base_url": "http://127.0.0.1/magento23dev/",
"secure_base_link_url": "http://127.0.0.1/magento23dev/",
"secure_base_static_url": "http://127.0.0.1/magento23dev/pub/static/version1554979401/",
"secure_base_media_url": "http://127.0.0.1/magento23dev/pub/media/"
}
]
If you want to all field system config value or specific system field value then you need to build a Rest API yourself.
API point like:
webapi.xml Code
<?xml version="1.0" ?> <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd"> <route method="POST" url="/V1/devamitbera-systemconfig/systemconfig/"> <service class="Devamitbera\Systemconfig\Api\SystemconfigManagementInterface" method="getSystemconfig"/> <resources> <resource ref="anonymous"/> </resources> </route> </routes>
Interface Class: Devamitbera\Systemconfig\Api\SystemconfigManagementInterface
<?php
namespace Devamitbera\Systemconfig\Api;
interface SystemconfigManagementInterface
{
/**
* GET for getSystemconfig api
* @param string $storeCode
* @param string $path
* @return string[]
*/
public function getSystemconfig($storeCode =null ,$path = null);
}
Model Class for the interface:
Devamitbera\Systemconfig\Model\SystemconfigManagement
Code:
<?php
namespace Devamitbera\Systemconfig\Model;
class SystemconfigManagement implements \Devamitbera\Systemconfig\Api\SystemconfigManagementInterface
{
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
private $scopeConfig;
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Store\Model\StoreManagerInterface $storeManager
)
{
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
}
/**
* {@inheritdoc}
*/
public function getSystemconfig($storeCode = null ,$path = null)
{
if($storeCode === null){
$storeCode = $this->storeManager->getStore()->getCode();
}
$storeConfigFinaData = [];
$storeConfigFinaData[][$path] = $this->scopeConfig
->getValue(
$path,
\Magento\Store\Model\ScopeInterface::SCOPE_STORES,
$storeCode
);
return $storeConfigFinaData;
}
}
di.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Devamitbera\Systemconfig\Api\SystemconfigManagementInterface" type="Devamitbera\Systemconfig\Model\SystemconfigManagement"/>
</config>
TEST SAMPLE:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://127.0.0.1/magento23dev/rest/V1/devamitbera-systemconfig/systemconfig/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"storeCode\": \"default\",\"path\":\"contact/email/recipient_email\"}",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer gv5dgldyscnzpa8sveg1xfct9frnwy0q",
"Content-Type: application/json",
"Postman-Token: ff714339-e830-4347-8266-e458c58102d9",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
This method support two paramters:
1.$storeCode : Store code
2.$path : Xpath of system.xml