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!
Before we jump in, make sure you've got:
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?
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!
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!
Let's get our hands dirty with some CRUD operations:
$newContact = $client->create('res.partner', [ 'name' => 'John Doe', 'email' => '[email protected]' ]); echo "New contact created with ID: " . $newContact;
$contact = $client->read('res.partner', $newContact, ['name', 'email']); print_r($contact);
$client->update('res.partner', $newContact, [ 'phone' => '123-456-7890' ]);
$client->unlink('res.partner', $newContact);
Want to flex those API muscles? Try these:
$contacts = $client->search('res.partner', [ ['is_company', '=', true], ['customer', '=', true] ]);
$page = 1; $limit = 10; $offset = ($page - 1) * $limit; $contacts = $client->search('res.partner', [], $offset, $limit);
$contacts = $client->search('res.partner', [], 0, 10, 'create_date DESC');
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(); }
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!