3

Upon Login, I am sending an API call(post request) to an external site in observer which return a JSON response. I need to populate a table in the phtml with the response received.

Hence I am trying to pass this JSON data from the observer into phtml, Could anyone tell me how to do it, any other method of doing this is also welcome.

Thanking you in advance.

observer code

<?php

namespace Vendor\Module\Observer\Customer;

use \Psr\Log\LoggerInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
class Authenticated implements ObserverInterface
{
  protected $_responseFactory;
protected $_url;
protected $email;

protected $_coreSession;

public function __construct(

    \Magento\Framework\App\ResponseFactory $responseFactory,
    \Magento\Framework\UrlInterface $url,
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Framework\Session\SessionManagerInterface $coreSession

) {
_responseFactory = $responseFactory;
$this->_url = $url;
// $this->customerSession = $customerSession;
$this->_coreSession = $coreSession;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
//get object method

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customer = $observer->getEvent()->getCustomer();

$event = $observer->getEvent();
$customer = $event->getCustomer();

$email=$customer->getEmail();

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "53252",
CURLOPT_URL => "http://localhost:53252/api/Account/Login",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\n\t\"Email\":\"$email\",\n\t\"Password\":\"$email\"\n}",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
"postman-token: f2d5833b-07f0-9ecc-ca24-94dd9fc58a66"
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);
$json = json_decode($response, true);
//$this->customerSession->setMyValue($json);
//$this->customerSession->getMyValue();
$this->_coreSession->start();
$this->_coreSession->setMessage('The Core session');
if ($err) {
echo "cURL Error #:" . $err;
} else {

//echo $response;
if($json['Response']!='FOUND'){
$customerBeforeAuthUrl = $this->_url->getUrl('test/page/view');
$this->_responseFactory->create()->setRedirect($customerBeforeAuthUrl)->sendResponse();
}
else {
return $this;
}
}
exit;
}
//$redirectionUrl = $this->url->getUrl('[test]/[page]/[[view]');
//$this->responseFactory->create()->setRedirect($redirectionUrl)->sendResponse();

//return $this;

}

controller code

_pageFactory = $pageFactory;
        return parent::__construct($context);
    }

    public function execute()
    {
        return $this->_pageFactory->create();
    //$hello=$block->getValue();
    //echo $hello;
    }
}
  • Please share the code of controller & observer & phtml? – Amit Bera Nov 26 '18 at 9:43
  • You can register the json data variable using registry and use it in your block class file. after doing all the operation in block class file call your html renderrer function in phtml file. – Himanshu Nov 26 '18 at 10:25
0

You can use session on your observer to store JSON response that you are getting from API.

Later on the phtml, you get this session data from a Block file.

you can use below code in your observer to store data in the customer session

observer code

 <?php
namespace Vendor\Module\Observer\Customer;    

class Authenticated implements ObserverInterface {
    ............
    protected $_customerSession;
    ............

    public function __construct(
            ............
            \Magento\Customer\Model\Session $customerSession
            ............
    ) {
        ............
         $this->_customerSession = $customerSession;
         ............
    }

    public function execute(\Magento\Framework\Event\Observer $observer) 
    {
        ............
        $this->_customerSession->setMyVar1('my var 1 data');
        $this->_customerSession->setMyVar2('my var 2 data');
        ............
        return $this;
    }
}

Block class code (assumed your Block class file is Vendor\Module\Block\Myblock.php)

<?php

namespace Vendor\Module\Block;

class Myblock extends \Magento\Framework\View\Element\Template
{

    private $customerSession;
    private $storeManager;    

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,    
        \Magento\Customer\Model\Session $customerSession,     
        \Magento\Store\Model\StoreManagerInterface $storeManager

    ) {        
        $this->customerSession = $customerSession;        
        $this->_storeManager = $storeManager;                
        parent::__construct($context);
    }


    public function getMyVarOne() 
    {
        return $this->getCustomerSession()->getMyVar1();
    }

    public function getCustomerSession() 
    {
       return $this->customerSession;
    }    
}

Phtml File Code (assumed your phtml file is Vendor/Module/view/frontend/templates/myblock.phtml)

<?php echo $block->getMyVarOne(); ?>

Please Note: since you will store JSON data, you may need to use json_encode and json_decode to store and parse data.

  • Since I am calling these api calls from an observer,how do I return the values ? – Jerin Cherian Nov 27 '18 at 5:02

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.