PHP code example of car-api-team / carapi-php-sdk
1. Go to this page and download the library: Download car-api-team/carapi-php-sdk library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
car-api-team / carapi-php-sdk example snippets
$sdk = \CarApiSdk\CarApi::build([
'token' => getenv('CARAPI_TOKEN'),
'secret' => getenv('CARAPI_SECRET'),
]);
$sdk = \CarApiSdk\CarApi::build([
'token' => getenv('CARAPI_TOKEN'),
'secret' => getenv('CARAPI_SECRET'),
'httpVersion' => '2.0', // we recommend keeping the default 1.1
'encoding' => ['gzip'],
]);
$filePath = '/some/path/not/readable/by/browsers/carapi_jwt.txt';
$jwt = file_get_contents($filePath);
if (empty($jwt) || $sdk->loadJwt($jwt)->isJwtExpired() !== false) {
try {
$jwt = $sdk->authenticate();
file_put_contents($filePath, $jwt);
} catch (\CarApiSdk\CarApiException $e) {
// handle errors here
}
}
// make your api calls here...
$sdk->years(['query' => ['make' => 'Tesla']]);
$json = (new \CarApiSdk\JsonSearch())
->addItem(new \CarApiSdk\JsonSearchItem('make', 'in', ['Tesla']));
$sdk->years(['query' => ['json' => $json]])
$page = 1;
do {
$result = $sdk->makes(['query' => ['limit' => 1, 'page' => $page]]);
$lastPage = $result->collection->pages;
$page++;
print_r($result->data);
} while ($page <= $lastPage);
$years = $sdk->years();
foreach ($years as $year) {
echo $year;
}
$sdk->years(['query' => ['make' => 'Tesla']]);
foreach ($sdk->makes()->data as $make) {
echo $make->name;
}
$sdk->makes(['query' => ['year' => 2020]]);
foreach ($sdk->models()->data as $model) {
echo $model->name;
}
$sdk->models(['query' => ['year' => 2020, 'make' => 'Toyota']]);
foreach ($sdk->trims()->data as $trim) {
echo $trim->name;
}
$sdk->trims(['query' => ['year' => 2020, 'make' => 'Ford', 'model' => 'F-150']]);
$json = (new \CarApiSdk\JsonSearch())
->addItem(new \CarApiSdk\JsonSearchItem('model', 'in', ['F-150', 'F-250']));
$sdk->trims(['query' => ['year' => 2020, 'make' => 'Ford', 'json' => $json]]);
$json = (new \CarApiSdk\JsonSearch())
->addItem(new \CarApiSdk\JsonSearchItem('make', 'in', ['Toyota', 'Ford']));
->addItem(new \CarApiSdk\JsonSearchItem('bodies.type', '=', 'Sedan'));
$result = $sdk->trims(['query' => ['year' => 2020, 'json' => $json]]);
foreach ($result->data as $trim) {
echo $trim->name;
}
echo $sdk->trimItem($id)->name;
$sdk->vin('1GTG6CEN0L1139305');
foreach ($sdk->vin('1GTG6CEN0L1139305')->trims as $trim) {
echo $trim->name;
}
foreach ($sdk->bodies()->data as $body) {
echo $body->type;
}
foreach ($sdk->engines()->data as $engine) {
echo $engine->engine_type;
}
$sdk->mileages();
$sdk->interiorColors();
$sdk->exteriorColors();
$sdk->csvDataFeed();
$sdk->csvDataFeedLastUpdated();
$sdk->vehicleAttributes('bodies.type');
$sdk->accountRequests();
$sdk->accountRequestsToday();