Installation Add the package via Composer:
composer require ang3/php-odoo-api-client:^7.0
Register the service provider in config/app.php (if not auto-discovered):
'providers' => [
Ang3\Odoo\OdooServiceProvider::class,
],
Basic Configuration Publish the config file:
php artisan vendor:publish --provider="Ang3\Odoo\OdooServiceProvider"
Update .env with Odoo API credentials:
ODOO_URL=https://your-odoo-instance.com
ODOO_DB=your_database
ODOO_USERNAME=your_username
ODOO_PASSWORD=your_password
First API Call Inject the client into a Laravel service or controller:
use Ang3\Odoo\OdooClient;
use Psr\Log\LoggerInterface; // New PSR-14 Logger support
public function __construct(OdooClient $client, LoggerInterface $logger) {
$this->client = $client;
$this->logger = $logger;
}
public function fetchContacts() {
$contacts = $this->client->get('/res.partner');
$this->logger->info('Fetched contacts', ['count' => count($contacts)]);
return response()->json($contacts);
}
CRUD Operations
$newContact = $this->client->post('/res.partner', [
'name' => 'John Doe',
'email' => 'john@example.com',
]);
$contact = $this->client->get('/res.partner/1');
$this->client->put('/res.partner/1', ['name' => 'Updated Name']);
$this->client->delete('/res.partner/1');
Searching Records Use domain filters:
$contacts = $this->client->search('/res.partner', [
'filters' => [['email', '=', 'john@example.com']],
]);
Batch Operations
Use execute_kw for complex actions (e.g., bulk updates):
$this->client->executeKw('res.partner', 'write', [
[1, 2, 3], // IDs
[['name' => 'Updated']] // Values
]);
Logging with PSR-14 Leverage the new PSR-14 logger integration for structured logging:
// Configure logger in OdooServiceProvider
$this->app->bind(LoggerInterface::class, function ($app) {
return $app->make(\Illuminate\Log\Logger::class);
});
// Use in your code
$this->logger->debug('Odoo API request', [
'method' => 'GET',
'endpoint' => '/res.partner',
'params' => $this->client->getLastRequest()->getData()
]);
Authentication & Rate Limiting
$this->client->authenticate(); // Force re-auth
Event-Driven Integrations Use Laravel queues to process Odoo webhook payloads:
// In a controller
public function handleWebhook(Request $request) {
dispatch(new ProcessOdooWebhook($request->json()));
}
Authentication Issues
.env credentials and call $client->authenticate() explicitly.OdooClient::setDebug(true) to log raw requests/responses.Rate Limiting
$attempts = 0;
while ($attempts < 3) {
try {
$response = $this->client->get('/endpoint');
break;
} catch (\Ang3\Odoo\Exception\RateLimitException $e) {
$this->logger->warning('Rate limited, retrying...', ['attempt' => $attempts]);
sleep(2 ** $attempts); // Exponential delay
$attempts++;
}
}
XML-RPC vs. JSON-RPC
Field Access Restrictions
Access Rights in Odoo settings).Timeouts
'timeout' => 60, // seconds
PSR-14 Logger Compatibility
$this->app->bind(LoggerInterface::class, function ($app) {
return new MonologLogger($app->make(\Illuminate\Log\Logger::class));
});
Custom Middleware Add request/response filters:
$client->getMiddleware()->push(function ($request) {
$request->headers->set('X-Custom-Header', 'value');
});
Model Bindings Create Laravel models that map to Odoo records:
class OdooContact extends Model {
public function fetch($id) {
return $this->client->get("/res.partner/{$id}");
}
}
Webhook Validation Validate Odoo webhook signatures (if enabled):
use Ang3\Odoo\WebhookValidator;
$validator = new WebhookValidator($request->header('X-Odoo-Signature'));
if (!$validator->isValid($request->getContent())) {
$this->logger->warning('Invalid webhook signature');
abort(403);
}
Caching Responses Cache frequent API calls (e.g., product lists):
$products = Cache::remember('odoo_products', now()->addHours(1), function () {
return $this->client->get('/product.product');
});
Structured Logging Use the new PSR-14 logger for better observability:
$this->logger->info('Odoo API operation', [
'action' => 'create',
'model' => 'res.partner',
'data' => ['name' => 'John Doe']
]);
ODOO_URL includes the database name (e.g., https://odoo.com/db_name).config('odoo.url') for dynamic environments.config/odoo.php:
'debug' => env('ODOO_DEBUG', false),
'logger' => [
'enabled' => true,
'channel' => env('ODOO_LOG_CHANNEL', 'stack'),
],
config/logging.php is set up for PSR-14 compatibility:
'default' => env('LOG_CHANNEL', 'stack'),
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'daily'],
],
// ...
],
How can I help you explore Laravel packages today?