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());
        }
    }
1
  • you can use search Criteria Commented Sep 20, 2018 at 19:04

1 Answer 1

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'
         ]
      ]
    ]
  ]
5
  • I am getting response in search controller. How can I return this response to the result page. Commented Sep 21, 2018 at 5:46
  • Call Interface and put your code in Model file. Commented Sep 22, 2018 at 6:04
  • Follow the service contact standard. Commented Sep 22, 2018 at 6:05
  • great, was my solution helped you in anyway ? Commented Sep 22, 2018 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 Commented Sep 22, 2018 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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