Sendinblue API Integration in php: Method curl and vendor autoload (both)

Picture of Woocoders
Woocoders
25th March 2023

Sending out email campaigns is an essential part of any successful marketing strategy. It can help you reach out to a wider audience and keep them engaged with your brand. Sendinblue is a popular email marketing platform that allows you to send and track email campaigns.

In this blog post, we will discuss how to use PHP code to add a contact to Sendinblue via API. This can be useful if you have a website or an application and want to add contacts to Sendinblue automatically.

Method 1: Composer (vendor/autoload.php)

Step 1: Obtain Sendinblue API Key

Before you can start using Sendinblue API, you need to obtain an API key. You can obtain the API key by logging in to your Sendinblue account, navigating to the Account Overview page, and clicking on the SMTP & API tab. From there, you can generate a new API key or use an existing one.

Step 2: Install Sendinblue API Library for PHP

To use Sendinblue API with PHP, you will need to install the Sendinblue API library for PHP. You can download the library from the official Sendinblue GitHub repository or install it using Composer.

If you’re using Composer, you can simply run the following command in your project directory:

				
					composer require sendinblue/api-v3-sdk
				
			

Step 3: Write PHP code to add a contact to Sendinblue

Now that you have obtained the API key and installed the Sendinblue API library for PHP, you can write PHP code to add a contact to Sendinblue.

Here is an example PHP code that adds a contact to Sendinblue:

				
					<?php

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

$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', 'YOUR_API_KEY');

$apiInstance = new SendinBlue\Client\Api\ContactsApi(
    new GuzzleHttp\Client(),
    $config
);

$createContact = new \SendinBlue\Client\Model\CreateContact(); // \SendinBlue\Client\Model\CreateContact | Values to create a contact
$createContact['email'] = 'johndoe@example.com'; // Email address of the user
$createContact['listIds'] = array(2,3,4); // Ids of the lists to add the contact to

try {
    $result = $apiInstance->createContact($createContact);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling ContactsApi->createContact: ', $e->getMessage(), PHP_EOL;
}
?>
				
			

In this example, we first set the API key using the setDefaultConfiguration() method. We then create an instance of the ContactsApi class, which is responsible for adding contacts to Sendinblue.

We create a new contact object using the CreateContact() method and set the email address of the contact using the email property. We also set the IDs of the lists to which we want to add the contact using the listIds property.

Finally, we call the createContact() method of the ContactsApi instance and pass the CreateContact object as a parameter. If the contact is added successfully, the result will be returned in the $result variable.

Method 2: Php curl

If you want to use curl simple method then follow this code:

				
					<?php 
$api_key = 'YOUR_API_KEY';
$list_id = 123; // Replace with the ID of the list you want to add the contact to
$email = 'johndoe@example.com'; // Replace with the email address of the contact

$data = array(
'email' => $email,
'listIds' => array($list_id)
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.sendinblue.com/v3/contacts');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'api-key: ' . $api_key
));
$response = curl_exec($ch);
curl_close($ch);

if ($response === false) {
echo 'Error adding contact: ' . curl_error($ch);
} else {
echo 'Contact added successfully.';
}
?>
				
			

In this example, we first set the API key, list ID, and email address of the contact. We then create an array with the email and list IDs, and use json_encode() to convert it to a JSON string.

We then initialize a cURL session with curl_init(), and set the URL, return transfer option, HTTP method (POST), and request data with curl_setopt(). We also set the content type and API key headers with CURLOPT_HTTPHEADER.

We then execute the request with curl_exec(), and close the cURL session with curl_close().

Finally, we check if the response is false (which indicates an error), and if not, we display a success message.

Note that this is just a basic example, and you may need to modify the code to suit your specific needs (e.g. adding more fields to the request data, handling errors differently, etc.).

Conclusion:

In this blog post, we have discussed how to use PHP code to add a contact to Sendinblue via API. By following these steps, you can automate the process of adding contacts to Sendinblue and streamline your email marketing efforts.

Share this post: