Skip to content

Using PHP

Many popular CMS systems are written using php which have support for calling RESTful services via curl libraries.

Elcome Limited does not directly support this API usage through php but can provide some limited help in providing the following examples.

Please bear in mind that the API conforms to the OpenAPi 3.0 specification so is accessible via a large range of technologies.

PHP Examples

PHP Example 1

This example is using getPrimaryEntityValuesByCascadingPrimaries to get the first two lists for cascading drop downs for vehicle selection.

<?php

// NB. If you get curl_error "SSL certificate problem: unable to get local issuer certificate"
// then you need to provide curl with the trusted CA certificates
// -> Download the Mozilla CA certificate bundle 'cacert.pem' from https://curl.haxx.se/docs/caextract.html
// -> In php.ini, add:
// [curl]
// curl.cainfo = C:\Path\To\cacert.pem
// -> And restart php

function get_oauth_token($client_id, $secret, $scope) {

    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://login.elcome.co.uk/connect/token',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => http_build_query(array(
            'client_id' => $client_id,
            'client_secret' => $secret,
            'grant_type' => 'client_credentials',
            'scope' => $scope,
        )),
        CURLOPT_FAILONERROR => true,
    ));
    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

    if ($err) {
        throw new Exception("CURL Error: $err");
    } else {
        $ret = json_decode($response);
        return $ret->access_token;
    }
}

function api_request($token, $url, $request) {

    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_HTTPHEADER => array(
            "Authorization: Bearer $token",
            'Accept: application/json',
            'Content-Type: application/json',
        ),
        CURLOPT_POSTFIELDS => json_encode($request),
        CURLOPT_FAILONERROR => true,
    ));
    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

    if ($err) {
        throw new Exception("CURL Error: $err");
    } else {
        return json_decode($response);
    }
}

function my_print($name, $value) {
    $value_pretty = htmlentities(json_encode($value, JSON_PRETTY_PRINT));
    print("<h2>$name</h2><pre>$value_pretty</pre>");
}

$client_id = ******/****;
$secret = '*********';
$scope = **********';

// Get an access token
// NB. the token can be reused and is valid for up to 24 hours
$token = get_oauth_token($client_id, $secret, $scope);

my_print('Token', $token);

$url = 'https://********-api.elcome.co.uk/v1/entity/getPrimaryEntityValuesByCascadingPrimaries';
$request = array(
    'baseRequest' => array(
        'requestReference' => 0,
        'requestPostedTime' => date('c'),
        'system' => 'ecat',
    ),
    'indexReference' => 4,
    'requestedPrimary' => 1,
    'primaryValues' => array(),
    'filterRequest' => array(
        'brands' => array(),
        'productTypeReferences' => array(),
        'categoryReferences' => array(),
    ),
);
$response = api_request($token, $url, $request);

my_print('Request', $request);
my_print('Response', $response);


$url = 'https://********-api.elcome.co.uk/v1/entity/getPrimaryEntityValuesByCascadingPrimaries';
$request = array(
    'baseRequest' => array(
        'requestReference' => 0,
        'requestPostedTime' => date('c'),
        'system' => 'ecat',
    ),
    'indexReference' => 4,
    'requestedPrimary' => 2,
    'primaryValues' => array('AUDI'),
    'filterRequest' => array(
        'brands' => array(),
        'productTypeReferences' => array(),
        'categoryReferences' => array(),
    ),
);
$response = api_request($token, $url, $request);

my_print('Request', $request);
my_print('Response', $response);

PHP Response 1

Token
"**********************************************"

Request
{
    "baseRequest": {
        "requestReference": 0,
        "requestPostedTime": "2018-10-10T15:10:16+00:00",
        "system": "ecat"
    },
    "indexReference": 4,
    "requestedPrimary": 1,
    "primaryValues": [],
    "filterRequest": {
        "brands": [],
        "productTypeReferences": [],
        "categoryReferences": []
    }
}
Response
{
    "baseResponse": {
        "baseRequest": {
            "requestReference": 0,
            "requestPostedTime": "2018-10-10T16:10:16+01:00",
            "system": "ecat"
        },
        "responseTime": "00:00:01.4981743",
        "resultCode": 0,
        "resultText": "success"
    },
    "response": [
        "ABARTH",
        "AC",
        "ACURA",
        "AIXAM",
        "ALFA ROMEO",
        "ALPINA",
        "ALPINE",
        "ALVIS",
        "AMC",
        "ANADOL",
        "ANDORIA",
        "ARO",
        "ARTEGA",
        "ASIA MOTORS",
        "ASKAM (FARGO\/DESOTO)",
        "ASTON MARTIN",
        "AUDI",
        "AUSTIN",
        "AUSTIN-HEALEY",
        "AUTO UNION",
        "AUTOBIANCHI",
        "AUVERLAND",
        "BARKAS",
        "BAW",
        "BEDFORD",
        "BENTLEY",
        "BERTONE",
        "BESTURN (FAW)",
        "BITTER",
        "BLUECAR",
        "BMW",
        "BMW (BRILLIANCE)",
        "BOGDAN",
        "BOND",
        "BORGWARD",
        "BRILLIANCE",
        "BRISTOL",
        "BUGATTI",
        "BUICK",
        "BYD",
        "CADILLAC",
        "CALLAWAY",
        "CARBODIES",
        "CASALINI",
        "CATERHAM",
        "CeA3 (SeAZ)",
        "CHANGAN",
        "CHANGFENG",
        "CHECKER",
        "CHERY",
        "CHEVROLET",
        "CHEVROLET (SGM)",
        "CHRYSLER",
        "CITRO\u00cbN",
        "CMC",
        "DACIA",
        "DAEWOO",
        "DAF",
        "DAIHATSU",
        "DAIMLER",
        "DALLAS",
        "DATSUN",
        "De La Chapelle",
        "DE LOREAN",
        "DE TOMASO",
        "DERWAYS",
        "DFSK",
        "DODGE",
        "DONGFENG (DFAC)",
        "DONKERVOORT",
        "DR",
        "DS",
        "DS (CAPSA)",
        "EAGLE",
        "EBRO",
        "EFFEDI",
        "EMGRAND",
        "EMGRAND (GEELY)",
        "ENGLON",
        "EUNOS",
        "FAW (TIANJIN)",
        "FERRARI",
        "FIAT",
        "FISKER",
        "FORD",
        "FORD ASIA \/ OCEANIA",
        "FORD AUSTRALIA",
        "FORD OTOSAN",
        "FORD USA",
        "FPV",
        "FSO",
        "GAZ",
        "GEELY",
        "GENESIS",
        "GEO",
        "GERMAN E-CARS",
        "GINETTA",
        "GLAS",
        "GMC",
        "GONOW",
        "GONOW (GAC)",
        "GOUPIL",
        "GREAT WALL",
        "GRECAV",
        "HAFEI",
        "HAIMA (FAW)",
        "HAIMA (ZHENGZHOU)",
        "HAVAL",
        "HAWTAI",
        "HILLMAN",
        "HINDUSTAN",
        "HOBBYCAR",
        "HOLDEN",
        "HONDA",
        "HONDA (GAC)",
        "HUANGHAI",
        "HUMMER",
        "HYUNDAI",
        "INDIGO",
        "INFINITI",
        "INNOCENTI",
        "IRAN KHODRO (IKCO)",
        "IRMSCHER",
        "ISDERA",
        "ISUZU",
        "IVECO",
        "IZH",
        "JAC",
        "JAGUAR",
        "JEEP",
        "JENSEN",
        "JMC",
        "KIA",
        "KOENIGSEGG",
        "KTM",
        "LADA",
        "LAMBORGHINI",
        "LANCIA",
        "LAND ROVER",
        "LANDWIND (JMC)",
        "LDV",
        "LEXUS",
        "LIFAN",
        "LIGIER",
        "LINCOLN",
        "LLOYD",
        "LOTUS",
        "LTI",
        "LUXGEN",
        "MAHINDRA",
        "MAN",
        "MARCOS",
        "MARUTI",
        "MARUTI SUZUKI",
        "MASERATI",
        "MAXUS",
        "MAXUS (SAIC MOTOR)",
        "MAYBACH",
        "MAZDA",
        "MCLAREN",
        "MEGA",
        "MERCEDES-BENZ",
        "MERCEDES-BENZ (BBDC)",
        "MERCURY",
        "METROCAB",
        "MG",
        "MIA",
        "MICROCAR",
        "MIDDLEBRIDGE",
        "MINELLI",
        "MINI",
        "MITSUBISHI",
        "MITSUOKA",
        "MORGAN",
        "MORRIS",
        "MOSKVICH",
        "NISSAN",
        "NISSAN (DFAC)",
        "NISSAN (DONGFENG)",
        "NOBLE",
        "NSU",
        "OLDSMOBILE",
        "OLTCIT",
        "OPEL",
        "OSCA",
        "PAGANI",
        "PANOZ",
        "PANTHER",
        "PAYKAN",
        "PERODUA",
        "PEUGEOT",
        "PGO",
        "PIAGGIO",
        "PININFARINA",
        "PLYMOUTH",
        "POLESTAR",
        "PONTIAC",
        "PORSCHE",
        "PREMIER",
        "PROTON",
        "PUCH",
        "RAM",
        "RANGER",
        "RAVON",
        "RAYTON FISSORE",
        "RELIANT",
        "RENAULT",
        "RENAULT TRUCKS",
        "REVA (MAHINDRA)",
        "RILEY",
        "ROLLS-ROYCE",
        "ROMANITAL",
        "ROVER",
        "RUF",
        "SAAB",
        "SAIPA",
        "SAMSUNG",
        "SANTANA",
        "SAO",
        "SATURN",
        "SEAT",
        "SHELBY",
        "SHUANGHUAN",
        "SIPANI",
        "SKODA",
        "SMART",
        "SPECTRE",
        "SPYKER",
        "SSANGYONG",
        "STANDARD",
        "STANGUELLINI",
        "STEYR",
        "STREETSCOOTER",
        "SUBARU",
        "SUNBEAM",
        "SUZUKI",
        "TAGAZ",
        "TALBOT",
        "TATA",
        "TATA (TELCO)",
        "TAZZARI",
        "TESLA",
        "THINK",
        "TOFAS",
        "TOYOTA",
        "TOYOTA (FAW)",
        "TOYOTA (GAC)",
        "TRABANT",
        "TRIUMPH",
        "TVR",
        "UAZ",
        "UMM",
        "UZ-DAEWOO",
        "VAUXHALL",
        "VECTOR",
        "VEM",
        "VENTURI",
        "VICTORY",
        "VM 77",
        "VOLVO",
        "VORTEX",
        "VUHL",
        "VW",
        "WARTBURG",
        "WESTFIELD",
        "WIESMANN",
        "WOLSELEY",
        "YUEJIN",
        "YUGO",
        "YULON",
        "ZASTAVA",
        "ZAZ",
        "ZENOS CARS",
        "ZHONGXING (ZTE)",
        "ZIL",
        "ZUENDAPP"
    ]
}
Request
{
    "baseRequest": {
        "requestReference": 0,
        "requestPostedTime": "2018-10-10T15:10:19+00:00",
        "system": "ecat"
    },
    "indexReference": 4,
    "requestedPrimary": 2,
    "primaryValues": [
        "AUDI"
    ],
    "filterRequest": {
        "brands": [],
        "productTypeReferences": [],
        "categoryReferences": []
    }
}
Response
{
    "baseResponse": {
        "baseRequest": {
            "requestReference": 0,
            "requestPostedTime": "2018-10-10T16:10:19+01:00",
            "system": "ecat"
        },
        "responseTime": "00:00:00.0541277",
        "resultCode": 0,
        "resultText": "success"
    },
    "response": [
        "100 (43, C2)",
        "100 (44, 44Q, C3)",
        "100 (4A, C4)",
        "100 (4A2, C4)",
        "100 (C1)",
        "100 Avant (43, C2)",
        "100 Avant (44, 44Q, C3)",
        "100 Avant (4A, C4)",
        "100 Avant (4A5, C4)",
        "100 Coupe (C1)",
        "200 (43)",
        "200 (44, 44Q)",
        "200 Avant (44, 44Q)",
        "50 (86)",
        "60",
        "60 Variant",
        "75",
        "75 Variant",
        "80",
        "80 (80, 82, B1)",
        "80 (81, 85, B2)",
        "80 (89, 89Q, 8A, B3)",
        "80 (8C2, B4)",
        "80 Avant (8C5, B4)",
        "80 Variant",
        "90 (81, 85, B2)",
        "90 (89, 89Q, 8A, B3)",
        "A1 (8X1, 8XK)",
        "A1 Sportback (8XA, 8XF)",
        "A2 (8Z0)",
        "A3 (8L1)",
        "A3 (8P1)",
        "A3 (8V1, 8VK)",
        "A3 Convertible (8P7)",
        "A3 Convertible (8V7, 8VE)",
        "A3 Limousine (8VS, 8VM)",
        "A3 Sportback (8PA)",
        "A3 Sportback (8VA, 8VF)",
        "A4 (8D2, B5)",
        "A4 (8E2, B6)",
        "A4 (8EC, B7)",
        "A4 (8K2, B8)",
        "A4 (8W2, B9)",
        "A4 Allroad (8KH, B8)",
        "A4 Allroad (8WH, B9)",
        "A4 Avant (8D5, B5)",
        "A4 Avant (8E5, B6)",
        "A4 Avant (8ED, B7)",
        "A4 Avant (8K5, B8)",
        "A4 Avant (8W5, B9)",
        "A4 Convertible (8H7, B6, 8HE, B7)",
        "A5 (8T3)",
        "A5 (F53)",
        "A5 Convertible (8F7)",
        "A5 Convertible (F57)",
        "A5 Sportback (8TA)",
        "A5 Sportback (F5A)",
       "A6 (4A, C4)",
        "A6 (4A2, C4)",
        "A6 (4B2, C5)",
        "A6 (4F2, C6)",
        "A6 (4G2, 4GC, C7)",
        "A6 (C8, 4A2)",
        "A6 Allroad (4FH, C6)",
        "A6 Allroad (4GH, 4GJ)",
        "A6 Avant (4A, C4)",
        "A6 Avant (4A5, C4)",
        "A6 Avant (4B5, C5)",
        "A6 Avant (4F5, C6)",
        "A6 Avant (4G5, 4GD, C7)",
        "A6 Avant (C8, 4A5)",
        "A7 Sportback (4GA, 4GF)",
        "A7 Sportback (4KA)",
        "A8 (4D2, 4D8)",
        "A8 (4E2, 4E8)",
        "A8 (4H2, 4H8, 4HC, 4HL)",
        "A8 (4N2, 4N8)",
        "ALLROAD (4BH, C5)",
        "CABRIOLET (8G7, B4)",
        "COUPE (81, 85)",
        "COUPE (89, 8B)",
        "F103",
        "F103 Variant",
        "Q2 (GAB)",
        "Q3 (8UB, 8UG)",
        "Q5 (8RB)",
        "Q5 (FYB)",
        "Q7 (4LB)",
        "Q7 (4MB)",
        "Q8 (4MN)",
        "QUATTRO (85)",
        "R8 (422, 423)",
        "R8 (4S3)",
        "R8 Spyder (427, 429)",
        "R8 Spyder (4S9)",
        "SUPER 90",
       "TT (8J3)",
        "TT (8N3)",
        "TT (FV3)",
        "TT Roadster (8J9)",
        "TT Roadster (8N9)",
        "TT Roadster (FV9)",
        "V8 (441, 442, 4C2, 4C8)"
    ]
}

PHP Example 2

This example uses getEntitiesByCascadingPrimaries to show how to retrieve full vehicle details given a set of selected primary values.

<?php

// NB. If you get curl_error "SSL certificate problem: unable to get local issuer certificate"
// then you need to provide curl with the trusted CA certificates
// -> Download the Mozilla CA certificate bundle 'cacert.pem' from https://curl.haxx.se/docs/caextract.html
// -> In php.ini, add:
// [curl]
// curl.cainfo = C:\Path\To\cacert.pem
// -> And restart php

function get_oauth_token($client_id, $secret, $scope) {

    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://login.elcome.co.uk/connect/token',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => http_build_query(array(
            'client_id' => $client_id,
            'client_secret' => $secret,
            'grant_type' => 'client_credentials',
            'scope' => $scope,
        )),
        CURLOPT_FAILONERROR => true,
    ));
    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

    if ($err) {
        throw new Exception("CURL Error: $err");
    } else {
        $ret = json_decode($response);
        return $ret->access_token;
    }
}

function api_request($token, $url, $request) {

    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_HTTPHEADER => array(
            "Authorization: Bearer $token",
            'Accept: application/json',
            'Content-Type: application/json',
        ),
        CURLOPT_POSTFIELDS => json_encode($request),
        CURLOPT_FAILONERROR => true,
    ));
    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

    if ($err) {
        throw new Exception("CURL Error: $err");
    } else {
        return json_decode($response);
    }
}

function my_print($name, $value) {
    $value_pretty = htmlentities(json_encode($value, JSON_PRETTY_PRINT));
    print("<h2>$name</h2><pre>$value_pretty</pre>");
}

$client_id = '******/ecat';
$secret = '******';
$scope = '******';

// Get an access token
// NB. the token can be reused and is valid for up to 24 hours
$token = get_oauth_token($client_id, $secret, $scope);

my_print('Token', $token);

$url = 'https://************/v1/entity/getEntitiesByCascadingPrimaries';
$request = array(
    'baseRequest' => array(
        'requestReference' => 0,
        'requestPostedTime' => date('c'),
        'system' => 'ecat',
    ),
    'indexReference' => 7,
    'requestedPrimary' => 6,
    'primaryValues' => array('Audi','A4','2.0','12/2000 -> 12/2004','Petrol','ALT'),
    'filterRequest' => array(
        'brands' => array(),
        'productTypeReferences' => array(),
        'categoryReferences' => array(),
    ),
);
$response = api_request($token, $url, $request);

my_print('Request', $request);
my_print('Response', $response);

PHP Response 2

Token
"********************************************************"

Request
{
    "baseRequest": {
        "requestReference": 0,
        "requestPostedTime": "2018-11-12T12:52:40+03:00",
        "system": "ecat"
    },
    "indexReference": 7,
    "requestedPrimary": 6,
    "primaryValues": [
        "Audi",
        "A4",
        "2.0",
        "12\/2000 -> 12\/2004",
        "Petrol",
        "ALT"
    ],
    "filterRequest": {
        "brands": [],
        "productTypeReferences": [],
        "categoryReferences": []
    }
}

Response
{
    "baseResponse": {
        "baseRequest": {
            "requestReference": 0,
            "requestPostedTime": "2018-11-12T09:52:40+00:00",
            "system": "ecat"
        },
        "responseTime": "00:00:22.1500180",
        "resultCode": 0,
        "resultText": "success"
    },
    "entities": [
        {
            "indexReference": 7,
            "provider": "***",
            "internalReference": 122028,
            "externalReference": "******",
            "attributes": {
                "make": "Audi",
                "model": "A4",
                "enginesize": "2.0",
                "datefrom": "12\/2000 -> 12\/2004",
                "fueltype": "Petrol",
                "enginecode": "ALT",
                "vehicletype": "********,
                "submodel": null,
                "mark": "B6 (8E) (2001-2004)",
                "trim": "A4 2.0 Avant B6 (8E) (2001-2004)",
                "enginenumberofcylinders": "4",
                "enginecylinderlayout": "I",
                "enginecamshafttype": "DOHC",
                "enginenumberofvalves": "20",
                "exactenginesize": "1984",
                "dateto": "12\/2004",
                "bodytype": "Estate",
                "bhp": "130",
                "drivetype": "FWD",
                "import": null,
                "enginesizeknownas": null,
                "v8_doors": "5",
                "v8_transmission_gears": "CVT, Manual\/5"
            }
        },
        {
            "indexReference": 7,
            "provider": "***",
            "internalReference": 122053,
            "externalReference": "******",
            "attributes": {
                "make": "Audi",
                "model": "A4",
                "enginesize": "2.0",
                "datefrom": "12\/2000 -> 12\/2004",
                "fueltype": "Petrol",
                "enginecode": "ALT",
                "vehicletype": "******",
                "submodel": null,
                "mark": "B6 (8E) (2001-2004)",
                "trim": "A4 2.0 B6 (8E) (2001-2004)",
                "enginenumberofcylinders": "4",
                "enginecylinderlayout": "I",
                "enginecamshafttype": "DOHC",
                "enginenumberofvalves": "20",
                "exactenginesize": "1984",
                "dateto": "12\/2004",
                "bodytype": "Saloon",
                "bhp": "130",
                "drivetype": "FWD",
                "import": null,
                "enginesizeknownas": null,
                "v8_doors": "4",
                "v8_transmission_gears": "CVT, Manual\/5"
            }
        }
    ]
}