Back

Step by Step Guide to Building an Odoo CRM API Integration in PHP

Aug 18, 20245 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Odoo CRM API integration? You're in for a treat. We'll be using the awesome ang3/php-odoo-api-client package to make our lives easier. Buckle up, and let's get started!

Prerequisites

Before we jump in, make sure you've got:

  • A PHP environment up and running
  • Composer installed (trust me, it's a lifesaver)
  • An Odoo CRM instance with API access (you've got this, right?)

Installation

First things first, let's get our hands on that ang3/php-odoo-api-client package. Fire up your terminal and run:

composer require ang3/php-odoo-api-client

Easy peasy, right?

Configuration

Now, let's set up those Odoo API credentials. Create a new PHP file and add this:

<?php use Ang3\Component\Odoo\Client; $client = new Client( 'https://your-odoo-instance.com', 'your_database', 'your_username', 'your_api_key' );

Replace those placeholders with your actual Odoo details, and you're good to go!

Basic Operations

Time to connect to the Odoo API. Don't worry, the client handles authentication for you. Here's a quick test:

try { $version = $client->version(); echo "Connected to Odoo version: " . $version; } catch (Exception $e) { echo "Oops! Something went wrong: " . $e->getMessage(); }

If you see your Odoo version, you're in business!

CRUD Operations

Let's get our hands dirty with some CRUD operations:

Creating Records

$newContact = $client->create('res.partner', [ 'name' => 'John Doe', 'email' => '[email protected]' ]); echo "New contact created with ID: " . $newContact;

Reading Records

$contact = $client->read('res.partner', $newContact, ['name', 'email']); print_r($contact);

Updating Records

$client->update('res.partner', $newContact, [ 'phone' => '123-456-7890' ]);

Deleting Records

$client->unlink('res.partner', $newContact);

Advanced Queries

Want to flex those API muscles? Try these:

Filtering and Searching

$contacts = $client->search('res.partner', [ ['is_company', '=', true], ['customer', '=', true] ]);

Pagination

$page = 1; $limit = 10; $offset = ($page - 1) * $limit; $contacts = $client->search('res.partner', [], $offset, $limit);

Sorting Results

$contacts = $client->search('res.partner', [], 0, 10, 'create_date DESC');

Error Handling

Always be prepared for the unexpected:

try { // Your Odoo API calls here } catch (\Ang3\Component\Odoo\Exception\OdooException $e) { echo "Odoo Error: " . $e->getMessage(); } catch (\Exception $e) { echo "General Error: " . $e->getMessage(); }

Best Practices

  • Cache frequently accessed data to reduce API calls
  • Use batch operations for bulk updates
  • Always validate and sanitize input data
  • Keep your API credentials secure (use environment variables!)

Conclusion

And there you have it! You're now equipped to integrate Odoo CRM into your PHP applications like a pro. Remember, practice makes perfect, so don't be afraid to experiment and push the boundaries of what you can do with this powerful API.

Happy coding, and may your integrations be ever smooth and bug-free!