Here’s a very simple PHP sample to execute multiple actions to one specific lead.
First, let’s study the flow in the code:
- First, we ping Kartra API’s secure URL
- Then, we connect via to our Kartra App ID (AIm863DwsOW), injecting our customer's unique Kartra API key (QG9GPLW8G) and Kartra API password (kdwFAfwrfVS).
- You will have a different key and password for your actual app and customer credentials.
- Then, we identify the lead the API call is for ( [email protected] ), whose full name is “Joe Smith”.
- If the system finds a lead with this email in our contacts database, it will overwrite the lead’s details in the lead’s profile with the new data we're passing over.
- If the lead is not found, it will be created as a brand new contact.
- Then, we pass 3 actions for that lead:
- subscribe him or her to list “My customers newsletter”
- assign him or her the tag “My customer”, and
- add +12 points to his or her score. Note how each action is wrapped around its own array!
- After all that, we request a confirmation response from the API
- Finally, we apply an “IF” conditional for further instructions depending on whether the command was successful or not.
- Note how we use the “Type” parameter to identify the message type.
<?php $ch = curl_init(); // CONNECT TO THE API SYSTEM VIA THE APP ID, AND VERIFY MY API KEY AND PASSWORD, IDENTIFY THE LEAD, AND SEND THE ACTIONS… curl_setopt($ch, CURLOPT_URL,"https://app.kartra.com/api"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( array( 'app_id' => 'AIm863DwsOW', 'api_key' => 'QG9GPLW8G', 'api_password' => 'kdwFAfwrfVS', 'lead' => array( 'email' => '[email protected]', ), 'actions' => array( '0' =>array( 'cmd' => 'subscribe_lead_to_list', 'list_name' => 'My customers newsletter' ), '1' =>array( 'cmd' => 'assign_tag', 'tag_name' => 'My customer' ), '2' =>array( 'cmd' => 'give_points_to_lead', 'points' => '12' ), ) ) ) ); // REQUEST CONFIRMATION MESSAGE FROM API… curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $server_output = curl_exec ($ch); curl_close ($ch); $server_json = json_decode($server_output); /* JSON SAMPLE ERROR MESSAGES: { "status":"Error", ,"message":"API key cannot be empty. Please get an API key first", ,"type":"202" } JSON SAMPLE SUCCESS MESSAGES: { "status":"Success" ,"lead":{"Status":"Found"} ,"actions":[ { "subscribe_lead_to_list": { "status":"Success" ,"message":"Lead subscribed in list My customers list" ,"type":"101" } } ,{ "assign_tag": { "status":"Error" ,"message":"tag does not exist. Nothing done" ,"type":"212" } } ,{ "give_points_to_lead": { "status":"Error" ,"message":"points is not a number. Nothing done" ,"type":"227" } } ] } */ // CONDITIONAL FOR FURTHER INSTRUCTIONS… if ($server_json->status == "Error") { echo "Sorry, the following error has occurred: ".$server_json->message.", type:".$server_json->type; } elseif ($server_json->status == "Success") { echo "lead status: ".$server_json->status."<br>"; echo "Status: ".$server_json->actions[0]->subscribe_lead_to_list->status.", Message: ".$server_json->actions[0]->subscribe_lead_to_list->message.", Type:".$server_json->actions[0]->subscribe_lead_to_list->type."<br>"; echo "Status: ".$server_json->actions[1]->assign_tag->status.", Message: ".$server_json->actions[1]->assign_tag->message.", Type:".$server_json->actions[1]->assign_tag->type."<br>"; echo "Status: ".$server_json->actions[2]->give_points_to_lead->status.", Message: ".$server_json->actions[2]->give_points_to_lead->message.", Type:".$server_json->actions[2]->give_points_to_lead->type."<br>"; } ?>