2

I created a custom views query plugin following this article: Building Views Plugins for Drupal 8

In my case, I am pulling data from a CVR registers and not Fitbit.

My Goal:
I want to pull data from CVR Register through this plugin and save it as my information reference. This is a view search. If a user enters CVR number, I want to output all values of my defined fields in the views. When I say defined, these are fields I set in the hook_views_data.

Current status:
I'm already in Step 5(working with remote services and dependency injection section).

Problem:
I'm trying to figure out how to build the code that query the CVR api(refer to sample working code below) and shows results in the views.

I'm still learning PHP. I really need help how to integrate this code and use it in views query plugin.

Code below is sample php code to to make entries in the CVR Register.
I made a simple php file to run this code in the terminal which outputs all CVR variables and its values.

function cvrapi($vat, $country)
{

    // Strip all other characters than numbers
    $vat = preg_replace('/[^0-9]/', '', $vat);


    // Check whether VAT-number is invalid
    if( empty($vat) )
    {

        // Print error message
        return('Venligst angiv et CVR-nummer.');

    }else{

        // Start cURL
        $ch = curl_init();

        // Set cURL options
        curl_setopt($ch, CURLOPT_URL, 'http://cvrapi.dk/api?search=' . $vat . '&country=' . $country);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'mit projekt');

        // Parse result
        $result = curl_exec($ch);

        // Close connection when done
        curl_close($ch);

        // Return our decoded result
        return json_decode($result, 1);

    }

}

// Test CVRAPI
print_r( cvrapi('10150817', 'dk') );
print_r( cvrapi('988615625', 'no') )

Found some useful laravel code for more details to understand. This is a cvrapi.php

use LasseRafn\CvrApi\Models\Business;
use LasseRafn\CvrApi\Utils\Request;

class CvrApi
{
    protected $request;

    public function __construct()
    {
        $this->request = new Request(config( 'cvrapi.endpoint' ));
    }


    /**
     * @param string $query
     * @param string $country
     *
     * @return Business
     */
    public function get($query = '', $country = 'dk')
    {
        $response = $this->request->get($query, $country);

        // todo throw error..

        return new Business($response);
    }
}

And this is the Request.php that is called in the above constructor.

<?php
namespace LasseRafn\CvrApi\Utils;

use GuzzleHttp\Client;
use LasseRafn\CvrApi\Errors\CurlError;

class Request
{
    protected $api_token;
    public    $curl;

    public function __construct( $baseUri = '' )
    {
        $this->curl = new Client( [
            'base_uri' => $baseUri
        ] );
    }

    public function get( $query = '', $country = 'dk' )
    {
        try
        {
            $url      = config( 'cvrapi.endpoint' ) . "?search={$query}&country={$country}";
            $response = $this->curl->get( $url );

            return json_decode( $response->getBody() );
        } catch ( \Exception $exception )
        {
            throw new CurlError( $exception->getMessage(), $exception->getCode() );
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.