Last active
March 31, 2023 06:28
-
-
Save LucWollants/3e73f974284431a651486be20438325c to your computer and use it in GitHub Desktop.
Insightly Tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$apiEndpoint = 'https://api.na1.insightly.com/v3.1/Contacts'; | |
$apiKey = 'your-api-key'; | |
// | |
// Create a new contact | |
// | |
$data = [ | |
'FIRST_NAME' => 'Jane', | |
'LAST_NAME' => 'Doe', | |
'EMAIL_ADDRESS' => '[email protected]' | |
]; | |
$options = [ | |
CURLOPT_URL => $apiEndpoint, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_HTTPHEADER => [ | |
'Authorization: Basic ' . base64_encode($apiKey . ':'), | |
'Content-Type: application/json' | |
], | |
CURLOPT_POSTFIELDS => json_encode($data), | |
]; | |
$curl = curl_init(); | |
curl_setopt_array($curl, $options); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
$data = json_decode($response, true); | |
$contactId = $data['CONTACT_ID']; | |
echo 'Contact Id: ' . $contactId . PHP_EOL . PHP_EOL; | |
// | |
// Get the contact by ID | |
// | |
$getOptions = array( | |
CURLOPT_URL => $apiEndpoint . '/' . $contactId, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_HTTPHEADER => array( | |
'Authorization: Basic ' . base64_encode($apiKey . ':'), | |
'Content-Type: application/json' | |
) | |
); | |
$getCurl = curl_init(); | |
curl_setopt_array($getCurl, $getOptions); | |
$getResponse = curl_exec($getCurl); | |
curl_close($getCurl); | |
echo 'Contact GET by id' . $getResponse . PHP_EOL . PHP_EOL; | |
// | |
// Search the contact by email address | |
// | |
$getOptions = array( | |
CURLOPT_URL => $apiEndpoint . '/' . 'Search?field_name=EMAIL_ADDRESS&field_value=jane.doe%40anonymous&top=1', | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_HTTPHEADER => array( | |
'Authorization: Basic ' . base64_encode($apiKey . ':'), | |
'Content-Type: application/json' | |
) | |
); | |
$getCurl = curl_init(); | |
curl_setopt_array($getCurl, $getOptions); | |
$getResponse = curl_exec($getCurl); | |
curl_close($getCurl); | |
if ($getResponse = '[]') { | |
echo '!!! NOTHING FOUND BY EMAIL BUT FOUND BY ID !!!' . PHP_EOL . PHP_EOL; | |
} else { | |
echo 'Contact found by email: ' . $getResponse . PHP_EOL . PHP_EOL; | |
} | |
// | |
// Delete the contact | |
// | |
$deleteOptions = [ | |
CURLOPT_URL => $apiEndpoint . '/' . $contactId, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_CUSTOMREQUEST => 'DELETE', | |
CURLOPT_HTTPHEADER => [ | |
'Authorization: Basic ' . base64_encode($apiKey . ':'), | |
'Content-Type: application/json' | |
] | |
]; | |
$deleteCurl = curl_init(); | |
curl_setopt_array($deleteCurl, $deleteOptions); | |
$deleteResponse = curl_exec($deleteCurl); | |
curl_close($deleteCurl); | |
echo $deleteResponse; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$contactsEndpoint = 'https://api.na1.insightly.com/v3.1/Contacts/'; | |
$opportunitiesEndpoint = 'https://api.na1.insightly.com/v3.1/Opportunities/'; | |
$apiKey = 'your-api-key'; | |
// | |
// Create a new opportunity | |
// | |
$data = [ | |
'OPPORTUNITY_NAME' => 'Test opportunity', | |
'OPPORTUNITY_STATE' => 'Open', | |
'OPPORTUNITY_DETAILS' => 'Test oppportunity details', | |
]; | |
$options = [ | |
CURLOPT_URL => $opportunitiesEndpoint, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_HTTPHEADER => [ | |
'Authorization: Basic ' . base64_encode($apiKey . ':'), | |
'Content-Type: application/json' | |
], | |
CURLOPT_POSTFIELDS => json_encode($data), | |
]; | |
$curl = curl_init(); | |
curl_setopt_array($curl, $options); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
$data = json_decode($response, true); | |
$opportunityId = $data['OPPORTUNITY_ID']; | |
echo 'Opportunity Id: ' . $opportunityId . PHP_EOL . PHP_EOL; | |
// | |
// Create a new contact | |
// | |
$data = [ | |
'FIRST_NAME' => 'Jane', | |
'LAST_NAME' => 'Doe', | |
'EMAIL_ADDRESS' => '[email protected]' | |
]; | |
$options = [ | |
CURLOPT_URL => $contactsEndpoint, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_HTTPHEADER => [ | |
'Authorization: Basic ' . base64_encode($apiKey . ':'), | |
'Content-Type: application/json' | |
], | |
CURLOPT_POSTFIELDS => json_encode($data), | |
]; | |
$curl = curl_init(); | |
curl_setopt_array($curl, $options); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
$data = json_decode($response, true); | |
$contactId = $data['CONTACT_ID']; | |
echo 'Contact Id: ' . $contactId . PHP_EOL . PHP_EOL; | |
// | |
// Link the contact to the opportunity | |
// | |
$data = [ | |
'LINK_OBJECT_ID' => $contactId, | |
'LINK_OBJECT_NAME' => 'contact', | |
]; | |
$options = [ | |
CURLOPT_URL => $opportunitiesEndpoint . $opportunityId . '/Links', | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_HTTPHEADER => [ | |
'Authorization: Basic ' . base64_encode($apiKey . ':'), | |
'Content-Type: application/json' | |
], | |
CURLOPT_POSTFIELDS => json_encode($data), | |
]; | |
$curl = curl_init(); | |
curl_setopt_array($curl, $options); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
echo 'Link response: ' . $response . PHP_EOL . PHP_EOL; | |
// | |
// Get the opportunity to verify link | |
// | |
$getOptions = array( | |
CURLOPT_URL => $opportunitiesEndpoint . '/' . $opportunityId, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_HTTPHEADER => array( | |
'Authorization: Basic ' . base64_encode($apiKey . ':'), | |
'Content-Type: application/json' | |
) | |
); | |
$getCurl = curl_init(); | |
curl_setopt_array($getCurl, $getOptions); | |
$getResponse = curl_exec($getCurl); | |
$data = json_decode($getResponse, true); | |
curl_close($getCurl); | |
if (!isset($data['LINKS'][0])) { | |
echo '!!! No links found !!!' . PHP_EOL . PHP_EOL; | |
} else { | |
echo 'Link found: ' . $data['LINKS'][0]['LINK_OBJECT_ID'] . PHP_EOL . PHP_EOL; | |
} | |
// | |
// Delete the contact | |
// | |
$deleteOptions = [ | |
CURLOPT_URL => $contactsEndpoint . '/' . $contactId, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_CUSTOMREQUEST => 'DELETE', | |
CURLOPT_HTTPHEADER => [ | |
'Authorization: Basic ' . base64_encode($apiKey . ':'), | |
'Content-Type: application/json' | |
] | |
]; | |
$deleteCurl = curl_init(); | |
curl_setopt_array($deleteCurl, $deleteOptions); | |
$deleteResponse = curl_exec($deleteCurl); | |
curl_close($deleteCurl); | |
echo $deleteResponse; | |
// | |
// Delete the opportunity | |
// | |
$deleteOptions = [ | |
CURLOPT_URL => $opportunitiesEndpoint . '/' . $opportunityId, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_CUSTOMREQUEST => 'DELETE', | |
CURLOPT_HTTPHEADER => [ | |
'Authorization: Basic ' . base64_encode($apiKey . ':'), | |
'Content-Type: application/json' | |
] | |
]; | |
$deleteCurl = curl_init(); | |
curl_setopt_array($deleteCurl, $deleteOptions); | |
$deleteResponse = curl_exec($deleteCurl); | |
curl_close($deleteCurl); | |
echo $deleteResponse; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replace
your-api-key
Scenario 1: searching on e-mail
Steps:
Scenario 2: linking a contact to an opportunity
Steps:
Problem
How can we build a polling mechanism to cover these failures?