HubSpot API: Master How to Add New Compaines (PHP) 2024!

HubSpot API: Master How to Add New Compaines (PHP) 2024!

Share

In case you’re looking for the first article about creating a contact in HubSpot API, click here.

While HubSpot is renowned for its powerful automations and seamless contact management, there might be times when its standard features don’t meet your specific needs. One such scenario is adding multiple companies to a contact, a task that HubSpot’s forms do not natively support. Fear not, as HubSpot’s API comes to the rescue, allowing for such customizations.

This guide will walk you through setting up your PHP environment and writing code to add multiple companies in HubSpot using a private app. Before diving in, ensure you’re comfortable with PHP fundamentals and familiar with Composer, PHP’s dependency manager.

use HubSpot\Client\Crm\Companies\Model\BatchInputSimplePublicObjectInputForCreate;
use HubSpot\Client\Crm\Companies\Model\SimplePublicObjectInputForCreate;


class Company
{
    private $client;
    private array $ids;
    private $company;
    private $properties;



    public function __construct($client)
    {
        $this->client = $client;
    }


    public function create($companiesProperties)
    {
        foreach ($companiesProperties as $property) {
            $simplePublicObjectInput = new SimplePublicObjectInputForCreate([
                'properties' => $property
            ]);
            $simplePublicObjectInputs[] = $simplePublicObjectInput;
        }
        $batchInputSimplePublicObjectInputForCreate = new BatchInputSimplePublicObjectInputForCreate(['inputs' => $simplePublicObjectInputs]);

        try {
            $batchResponse = $this->client->crm()->companies()->batchApi()->create($batchInputSimplePublicObjectInputForCreate);

            foreach ($batchResponse->getResults() as $company) {
                $this->ids[] = $company->getId(); 
            }
            return $batchResponse; 
        } catch (Exception $e) {
            new HSErrorHandler($e->getMessage(), __LINE__ . __FILE__, $e->getCode());
            return null;
        }
    }

    public function getCompanyIds()
    {
        return $this->ids;
    }
}

Setting Up Your Environment

  1. Install Composer: Composer is crucial for managing dependencies in PHP projects. If it’s not already installed, you can download it from the Composer website.
  2. Initialize Your PHP Project: In your project directory, run composer init to start a new PHP project, following the prompts to configure it.
  3. Add HubSpot API Client: Include the HubSpot API client in your project with the command composer require hubspot/api-client.

Writing the Code Using HubSpot API

Your primary script will be Company.php, handling the creation of companies in HubSpot. Similar to the contact management script, we’ll include error handling to catch and log any issues.

HubSpot API Guide

Company.php Overview

The Company.php file is structured to handle the complexities of creating multiple companies in HubSpot. Here’s a breakdown of its components:

Initial Setup

Security is paramount, so we start by ensuring our script isn’t directly accessed:

if (!defined('ABSPATH')) { exit; }

Set your HubSpot access token, which authenticates your API requests. Replace "YOUR_ACCESS_TOKEN" with your actual token:

define("HUBSPOT_ACCESS_TOKEN", "YOUR_ACCESS_TOKEN");

Importing Required Classes

We’ll use several classes from the HubSpot API client library to interact with the companies section:

use HubSpot\Client\Crm\Companies\Model\BatchInputSimplePublicObjectInputForCreate; 
use HubSpot\Client\Crm\Companies\Model\SimplePublicObjectInputForCreate;

The Company Class

The Company class encapsulates everything related to company management. It includes methods for creating companies and storing their IDs for further reference.

Here’s an outline of the Company class:

  • Properties: Includes the HubSpot client instance, an array for company IDs, and the company object and properties.
  • __construct($client): Initializes the Company class with a HubSpot client instance.
  • create($companiesProperties): Accepts an array of properties for multiple companies. It uses a loop to create a SimplePublicObjectInputForCreate instance for each company, then batches these instances for creation. On success, it stores each company’s ID.
  • getCompanyIds(): Returns the stored company IDs, useful for linking companies to contacts or other purposes.

Error Handling

As with contact management, error handling is crucial for debugging and ensuring the smooth execution of your script. The HSErrorHandler class can be reused or adapted from the contact management script to log any errors that occur.

Usage

To use the Company class, instantiate it with your HubSpot client, define the properties for each company, and call the create method. Here’s a simplified example:

$client = Factory::createWithAccessToken(HUBSPOT_ACCESS_TOKEN); 
$company = new Company($client); 
$companiesProperties = [ // Array of properties for each company ]; 
$company->create($companiesProperties); 
$companyIds = $company->getCompanyIds();

This process initializes the HubSpot client, creates a Company object, sets properties for multiple companies, and attempts to create these companies in HubSpot. The returned company IDs can be used for further actions or integrations.

By following this guide, you can extend HubSpot’s capabilities to fit your unique business needs, particularly when dealing with multiple companies for a single contact. For more advanced features or assistance, the HubSpot developer documentation and community forums are invaluable resources.

Learn More About APIs in HubSpot: https://developers.hubspot.com/docs/api/crm/companies

HubSpot Packages: https://packagist.org/packages/hubspot/api-client