I have an API(ukvehicledata) for search using registration id.
I want to integrate with magento 2.2
I am getting response in controller as JSON format. I have converted it into an array.
Now I want to return the response to the search result page.
I am using below code:
<?php
public function execute()
{
$this->layerResolver->create(Resolver::CATALOG_LAYER_SEARCH);
/* @var $query \Magento\Search\Model\Query */
$query = $this->_queryFactory->get();
$storeId = $this->_storeManager->getStore()->getId();
$query->setStoreId($storeId);
$queryText = $query->getQueryText();
if ($queryText != '') {
// Init cURL session
$curl = curl_init();
// Set API Key
$ApiKey = "675D83AAC4C8-E9C9-E9C9-E9C9-675D83AAC4C8";
// Construct URL String
$url = "https://uk1.domainname.co.uk/api/datapackage/%s?v=2&api_nullitems=1&key_vrm=%s&auth_apikey=%s";
$url = sprintf($url, "VehicleData", $queryText, $ApiKey); // Syntax: sprintf($url, "PackageName", "VRM", ApiKey);
// Note your package name here. There are 5 standard packagenames. Please see your control panel > weblookup or contact your account manager
// Create array of options for the cURL session
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET"
));
// Execute cURL session and store the response in $response
$response = curl_exec($curl);
// If the operation failed, store the error message in $error
$error = curl_error($curl);
// Close cURL session
curl_close($curl);
// If there was an error, print it to screen. Otherwise, unserialize response and print to screen.
if ($error) {
echo "cURL Error: " . $error;
} else {
//$respons = json_decode($response, true); // For demonstration purposes - Unserialize response & dump array contents to screen
$results = json_decode($response, true);
$responsee = $results->Response->DataItems->VehicleRegistration;
/*echo '<ul>';
foreach ($responsee as $key => $value) {
echo '<li>'.$key.' => '.$value.'</li><br/>';
}
echo '</ul>';*/
} else {
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
}
}