2

In my plugin I created a new table 'myapp_staging' made of three columns: 'id', 'id_api_call' and 'f24_options_value'. In the last field I would like to save a response to my Api call in json format.

I created a new field, the path of this is Vendor\MyApp\Block\System\Config\FieldModelli.php

here below the code:

<?php

namespace Vendor\MyApp\Block\System\Config;

use Magento\Framework\Data\Form\Element\AbstractElement;

class F24Modelli extends \Magento\Config\Block\System\Config\Form\Field
{
    protected $objectManager;
    
    public function __construct
    (
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Fattura24\AppFatturazione\Helper\Data $helper,
        array $data = []
    )
    {
        $this->objectManager = $objectManager;
        $this->helper = $helper;
        parent::__construct($context, $data);
    }

    public function render(AbstractElement $element)
    {
        $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
        return parent::render($element);
    }

    protected function _getElementHtml(AbstractElement $element)
    {
       return $this->F24Modelli();
    }

   
    public function F24Modelli()
    {
        $storeId = (int) $this->getRequest()->getParam('store', 0);
        $apiKey = $this->helper->getConfig('myApp/generali/api_key', $storeId);
        $templates_res = $this->helper->apiCall('https://www.app.myapp.com/api/v0.3/GetTemplate', $apiKey);
        //$templates_xml = simplexml_load_string($templates_res);
        $templates_xml = simplexml_load_string($templates_res, 'SimpleXMLElement', LIBXML_NOCDATA);
        $json_templates = json_encode($templates_xml);
     
        $resource = $this->objectManager->get('Magento\Framework\App\ResourceConnection');
        $connection = $resource->getConnection();
        $tableName = $resource->getTableName('myapp_staging');
        $select = "select * from $tableName where id_api_call='1'";
        $tem_result = $connection->fetchAll($select);
        if (count($tem_result) == 0) {
            $insert = "insert into $tableName set id_api_call='1',f24_options_value='$json_templates'";
            $connection->query($insert);
        } else {
            $update = "update $tableName set id_api_call='1',f24_options_value='$json_templates'";
            $connection->query($update);
        }
        return htmlspecialchars($json_templates);
    }
}

first I tried to read xml in this way:

$templates_xml = simplexml_load_string($templates_res);

then I encode the response in json and succeed in saving data in my table.

In my original xml object I have got some tags inside <[!CData[]]> nodes and I need the content of these nodes, so I tried this:

 $templates_xml = simplexml_load_string($templates_res, 'SimpleXMLElement', LIBXML_NOCDATA);

And then I encode the result in json format. Now after thata I'm not more able to save my 'complete' json result in the table.

Anyone could help me?

Thanks

0

At last I found my json string was not valid, I succeeded in db saving after doing some lint programmatically (by str_replace method and similar)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.