Last active
April 21, 2017 22:16
-
-
Save crazytonyi/d31ae1b576e277d704d2f2a6261ca476 to your computer and use it in GitHub Desktop.
Basic Example of API for MassUpdate
This file contains 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 | |
function getAccessToken($instance_url, $username, $password) | |
{ | |
echo "Authenticating..." . PHP_EOL; | |
$auth_url = $instance_url . "/oauth2/token"; | |
$oauth2_token_arguments = array( | |
"grant_type" => "password", | |
"client_id" => "sugar", | |
"client_secret" => "", | |
"username" => $username, | |
"password" => $password, | |
"platform" => "custom_api" | |
); | |
$auth_request = curl_init($auth_url); | |
curl_setopt($auth_request, CURLOPT_HEADER, false); | |
curl_setopt($auth_request, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($auth_request, CURLOPT_HTTPHEADER, array( | |
"Content-Type: application/json" | |
)); | |
$json_arguments = json_encode($oauth2_token_arguments); | |
curl_setopt($auth_request, CURLOPT_POSTFIELDS, $json_arguments); | |
$oauth2_token_response = curl_exec($auth_request); | |
$oauth2_token_response_obj = json_decode($oauth2_token_response); | |
$oauth_token = $oauth2_token_response_obj->access_token; | |
return $oauth_token; | |
} | |
function getLeads($instance_url, $oauth_token, $max_num, $offset) | |
{ | |
echo "Getting Leads from offset: $offset..." . PHP_EOL; | |
$leads_list_url = $instance_url . "/Leads"; | |
$list_arguments = array( | |
"max_num" => $max_num, | |
"offset" => $offset, | |
"fields" => "id", | |
"order_by" => "date_entered:ASC", | |
"favorites" => false, | |
"my_items" => false | |
); | |
$leads_list_url = $leads_list_url . "?" . http_build_query($list_arguments); | |
$leads_request = curl_init($leads_list_url); | |
curl_setopt($leads_request, CURLOPT_HEADER, false); | |
curl_setopt($leads_request, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($leads_request, CURLOPT_HTTPHEADER, array( | |
"Content-Type: application/json", | |
"oauth-token: {$oauth_token}" | |
)); | |
$leads_response = curl_exec($leads_request); | |
$leads_response_obj = json_decode($leads_response); | |
return $leads_response_obj; | |
} | |
function massUpdateLeads($instance_url, $oauth_token, $ids) | |
{ | |
echo "Mass Updating Leads..." . PHP_EOL; | |
$leads_update_url = $instance_url . "/Leads/MassUpdate"; | |
$update_arguments['massupdate_params'] = array( | |
"uid" => $ids, | |
"mkto_sync" => true | |
); | |
$leads_update_request = curl_init($leads_update_url); | |
curl_setopt($leads_update_request, CURLOPT_CUSTOMREQUEST, "PUT"); | |
curl_setopt($leads_update_request, CURLOPT_HEADER, false); | |
curl_setopt($leads_update_request, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($leads_update_request, CURLOPT_HTTPHEADER, array( | |
"Content-Type: application/json", | |
"oauth-token: {$oauth_token}" | |
)); | |
$json_arguments = json_encode($update_arguments); | |
curl_setopt($leads_update_request, CURLOPT_POSTFIELDS, $json_arguments); | |
$leads_update_response = curl_exec($leads_update_request); | |
echo "Mass Updating Leads Batch Complete." . PHP_EOL; | |
$leads_update_response_obj = json_decode($leads_update_response); | |
return $leads_update_response_obj; | |
} | |
$instance_url = "<some_sugar_instance>/rest/v10"; | |
$username = "<username>"; | |
$password = "<password>"; | |
$access_token = getAccessToken($instance_url, $username, $password); | |
$current_offset = 0; | |
$max_num = 500; | |
while ($current_offset != -1) { | |
$lead_list = getLeads($instance_url, $access_token, $max_num, $current_offset); | |
$current_offset = $lead_list->next_offset; | |
$lead_ids = array(); | |
foreach ($lead_list->records as $record) { | |
$lead_ids[] = $record->id; | |
} | |
$update_result = massUpdateLeads($instance_url, $access_token, $lead_ids); | |
} | |
echo "Mass Updating Leads Complete." . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment