3

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());
        }
    }
  • you can use search Criteria – Aditya Shah Sep 20 '18 at 19:04
0

I have an API(ukvehicledata) for search using registration id.

API(ukvehicledata)

you can use search Criteria using registration id

    http://<magento_host>/rest/V1/YOUR_API/?
    searchCriteria[filter_groups][0][filters][0][field]=registration_id&
    searchCriteria[filter_groups][0][filters][0][value]={VALUE}&
    searchCriteria[filter_groups][0][filters][0][condition_type]‌​=eq

method type : GET

The system creates an array, as shown in the following pseudo-code.

searchCriteria => [
  'filterGroups' => [
    0 => [
      'filters' => [
         0 => [
           'field' => 'registration_id',
           'value' => 'YOUR VALUE',
           'condition_type' => 'eq'
         ]
      ]
    ]
  ]
  • I am getting response in search controller. How can I return this response to the result page. – Vinod Kumar Sep 21 '18 at 5:46
  • Call Interface and put your code in Model file. – Aditya Shah Sep 22 '18 at 6:04
  • Follow the service contact standard. – Aditya Shah Sep 22 '18 at 6:05
  • great, was my solution helped you in anyway ? – Aditya Shah Sep 22 '18 at 6:21
  • 1
    No, I have done via different way.. I have followed this magento.stackexchange.com/a/100115/44608 and get response from block – Vinod Kumar Sep 22 '18 at 7: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.