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;
}
}