PHP Requires Chilkat v11.0.0+
PHP
SendGrid: Add or update list contacts and check the import job
See more SendGrid Examples
Use a Marketing Campaigns API key with contact access and the ID of an existing list. A 202 response queues the upsert; it does not confirm that the contacts were saved. Save the job ID and use it to recheck a pending import without submitting the contacts again. The error report may contain contact data; handle it privately.
Add or update contacts ยท Import job status
Chilkat PHP Downloads
<?php
use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;
$success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = false;
$http = new Http();
$http->RequireSslCertVerify = true;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
$http->AuthToken = 'SENDGRID_API_KEY';
$resp = new HttpResponse();
$jobId = '';
if ($jobId === '') {
$contacts = new JsonObject();
$contacts->UpdateString('list_ids[0]', 'YOUR_LIST_UUID');
$contacts->UpdateString('contacts[0].email', 'avery@example.com');
$contacts->UpdateString('contacts[0].first_name', 'Avery');
$contacts->UpdateString('contacts[1].email', 'robin@example.com');
$contacts->UpdateString('contacts[1].first_name', 'Robin');
try {
$http->HttpJson('PUT', 'https://api.sendgrid.com/v3/marketing/contacts', $contacts, 'application/json', $resp);
} catch (\Chilkat2\Exception $e) {
if ($resp->StatusCode == 0) {
echo $e->getMessage(), "\n";
} else {
echo 'HTTP ', $resp->StatusCode, ': ', $resp->BodyStr, "\n";
}
exit;
}
if ($resp->StatusCode != 202) {
echo 'HTTP ', $resp->StatusCode, ': ', $resp->BodyStr, "\n";
exit;
}
$queued = new JsonObject();
try {
$queued->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
echo "Unexpected response: expected JSON.\n";
exit;
}
if (!$queued->HasMember('job_id')) {
echo "No job ID returned.\n";
exit;
}
$jobId = $queued->StringOf('job_id');
echo 'Save this import job ID: ', $jobId, "\n";
}
$http->SetUrlVar('job', $jobId);
try {
$http->HttpNoBody('GET', 'https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}', $resp);
} catch (\Chilkat2\Exception $e) {
if ($resp->StatusCode == 0) {
echo $e->getMessage(), "\n";
} else {
echo 'HTTP ', $resp->StatusCode, ': ', $resp->BodyStr, "\n";
}
exit;
}
if ($resp->StatusCode != 200) {
echo 'HTTP ', $resp->StatusCode, ': ', $resp->BodyStr, "\n";
exit;
}
$job = new JsonObject();
try {
$job->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
echo "Unexpected response: expected JSON.\n";
exit;
}
$state = $job->StringOf('status');
if ($state === 'pending') {
echo 'Still pending. Check again later with jobId = ', $jobId, "\n";
exit;
}
echo 'Job status: ', $state, "\n";
echo 'Created: ', $job->IntOf('results.created_count'), ', updated: ', $job->IntOf('results.updated_count'), ', errors: ', $job->IntOf('results.errored_count'), "\n";
if (($state === 'errored') || ($state === 'failed')) {
echo "Review the job before retrying. If present, results.errors_url identifies the private error report.\n";
}