---
title: "PHP"
description: "Official CarsXE SDK for PHP."
canonical_url: "https://docs.carsxe.com/docs/sdks/php"
markdown_url: "https://docs.carsxe.com/docs/sdks/php.md"
last_updated: "1980-01-01"
x_farming_labs_generated_preamble: true
agent:
  task: "Install and call the CarsXE PHP SDK for vehicle-data endpoints."
  outcome: "The PHP client is installed and its documented example returns a successful CarsXE response."
  appliesTo:
    version:
      - "carsxe/carsxe PHP SDK (PHP 8.1+); CarsXE API v1 and v2"
  prerequisites:
    - "A valid CarsXE API key."
    - "The runtime and package tooling required by the PHP installation steps."
  verification:
    - "Run the documented PHP example and confirm it authenticates and returns vehicle data without an SDK or API error."
  failureModes:
    - symptom: "The PHP client cannot install, authenticate, or complete the example request."
      resolution: "Verify the runtime version, package installation, API-key environment variable, and endpoint inputs before rerunning the example."
---

# PHP
URL: /docs/sdks/php
LLM index: /llms.txt
Description: Official CarsXE SDK for PHP.
Related: /docs/sdks, /docs/get-started

<!-- farming-labs:agent-contract:start -->
## Agent Contract

Task: Install and call the CarsXE PHP SDK for vehicle-data endpoints.
Outcome: The PHP client is installed and its documented example returns a successful CarsXE response.

### Applies To

- Version: `carsxe/carsxe PHP SDK (PHP 8.1+); CarsXE API v1 and v2`

### Prerequisites

- A valid CarsXE API key.
- The runtime and package tooling required by the PHP installation steps.

### Verification

- Run the documented PHP example and confirm it authenticates and returns vehicle data without an SDK or API error.

### Failure Modes

- The PHP client cannot install, authenticate, or complete the example request. — Recovery: Verify the runtime version, package installation, API-key environment variable, and endpoint inputs before rerunning the example.
<!-- farming-labs:agent-contract:end -->

Task: Install and call the CarsXE PHP SDK for vehicle-data endpoints.
Related: /docs/sdks/php, /docs/get-started, /docs/sdks

The `carsxe/carsxe` Packagist package ships typed PHP 8.1 model classes for all CarsXE API responses. It does not bundle an HTTP client — use it alongside Guzzle (or any PSR-18 client) to make requests and hydrate the typed models from the JSON response. Requires PHP 8.1 or later.

## Installation

```bash
composer require carsxe/carsxe guzzlehttp/guzzle
```

## Quick start

```php
<?php

require_once __DIR__ . '/vendor/autoload.php';

use CarsXE\Models\VehicleSpec;
use GuzzleHttp\Client;

$client = new Client(['base_uri' => 'https://api.carsxe.com']);

$response = $client->get('/specs', [
    'query' => [
        'key' => $_ENV['CARSXE_API_KEY'],
        'vin' => 'WBAFR7C57CC811956',
    ],
]);

$body = json_decode((string) $response->getBody(), true);
$spec = VehicleSpec::fromArray($body['data']);

echo "{$spec->year} {$spec->make} {$spec->model}\n";
```

## Available models

| Class | Description |
|---|---|
| `VehicleSpec` | Full vehicle specification (VIN, make, model, engine, etc.) |
| `BulkRecallBatchJob` | Completed bulk recall batch job |
| `BulkRecallBatchJobPending` | In-progress bulk recall batch job |
| `BulkRecallBatchResult` | Result row for a single VIN in a batch |
| `BulkRecallBatchRow` | Raw recall row within a batch result |

Each class exposes a `static fromArray(array $data): self` factory method that maps the API response directly to typed readonly properties.

## Additional endpoints

The same Guzzle pattern works for every CarsXE endpoint — just change the path and query parameters. Decode the body with `json_decode((string) $response->getBody(), true)` as in [Quick start](#quick-start).

```php
// Market value (v2 — accepts state, mileage, and condition)
$response = $client->get('/v2/marketvalue', [
    'query' => [
        'key'     => $_ENV['CARSXE_API_KEY'],
        'vin'     => 'WBAFR7C57CC811956',
        'mileage' => 45000,
        'state'   => 'CA',
    ],
]);

// Safety recalls
$response = $client->get('/v1/recalls', [
    'query' => [
        'key' => $_ENV['CARSXE_API_KEY'],
        'vin' => '1C4JJXR64PW696340',
    ],
]);

// Vehicle history
$response = $client->get('/history', [
    'query' => [
        'key' => $_ENV['CARSXE_API_KEY'],
        'vin' => 'WBAFR7C57CC811956',
    ],
]);

// International VIN decoder (non-US VINs)
$response = $client->get('/v1/international-vin-decoder', [
    'query' => [
        'key' => $_ENV['CARSXE_API_KEY'],
        'vin' => 'WF0MXXGBWM8R43240',
    ],
]);

// License plate decoder
$response = $client->get('/platedecoder', [
    'query' => [
        'key'     => $_ENV['CARSXE_API_KEY'],
        'plate'   => '7XER187',
        'state'   => 'CA',
        'country' => 'US',
    ],
]);

// Vehicle images
$response = $client->get('/images', [
    'query' => [
        'key'   => $_ENV['CARSXE_API_KEY'],
        'make'  => 'BMW',
        'model' => 'X5',
        'year'  => '2019',
    ],
]);

// Year, make and model search
$response = $client->get('/v1/ymm', [
    'query' => [
        'key'   => $_ENV['CARSXE_API_KEY'],
        'year'  => '2012',
        'make'  => 'BMW',
        'model' => '5 Series',
    ],
]);

// OBD code decoder
$response = $client->get('/obdcodesdecoder', [
    'query' => [
        'key'  => $_ENV['CARSXE_API_KEY'],
        'code' => 'P0115',
    ],
]);

// Lien and theft records
$response = $client->get('/v1/lien-theft', [
    'query' => [
        'key' => $_ENV['CARSXE_API_KEY'],
        'vin' => '2C3CDXFG1FH762860',
    ],
]);
```

The two recognition endpoints are `POST` requests that take a JSON body with an `image_url` field. The API key still travels as a query parameter:

```php
// Plate image recognition
$response = $client->post('/platerecognition', [
    'query' => ['key' => $_ENV['CARSXE_API_KEY']],
    'json'  => [
        'image_url' => 'https://imagedelivery.net/moyiiSImjJPI_EZVxNMBBw/f49aed53-d736-4370-f3f4-97418841c800/public',
    ],
]);

// VIN OCR from an image (URL or base64 string)
$response = $client->post('/v1/vinocr', [
    'query' => ['key' => $_ENV['CARSXE_API_KEY']],
    'json'  => [
        'image_url' => 'https://imagedelivery.net/moyiiSImjJPI_EZVxNMBBw/f49aed53-d736-4370-f3f4-97418841c800/public',
    ],
]);
```

## Error handling

Guzzle throws `GuzzleHttp\Exception\ClientException` on 4xx responses and `GuzzleHttp\Exception\ServerException` on 5xx:

```php
use GuzzleHttp\Exception\GuzzleException;

try {
    $response = $client->get('/specs', [
        'query' => ['key' => $_ENV['CARSXE_API_KEY'], 'vin' => 'WBAFR7C57CC811956'],
    ]);
    $body = json_decode((string) $response->getBody(), true);
    $spec = VehicleSpec::fromArray($body['data']);
} catch (GuzzleException $e) {
    // $e->getCode() → HTTP status
    // $e->getMessage() → error details
    error_log('CarsXE error: ' . $e->getMessage());
}
```

## Best practices

**Load your key from the environment.** Never hardcode it.

```php
// .env (loaded via vlucas/phpdotenv or similar)
CARSXE_API_KEY=your_key_here
```

```php
$apiKey = $_ENV['CARSXE_API_KEY'] ?? getenv('CARSXE_API_KEY');
```

**Reuse the Guzzle client.** Instantiate it once (e.g. in a service container) and inject it — Guzzle reuses the underlying cURL handle pool.

## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
Docs-scoped sitemap: [/docs/sitemap.md](/docs/sitemap.md).
Well-known sitemap: [/.well-known/sitemap.md](/.well-known/sitemap.md).
