qwidjib0

whmcs to marekto

May 30th, 2017
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?
  2. /*
  3.  * THIS CRON:
  4.  *
  5.  * 1. Syncs WHMCS/BuzzStream subscribers (non-clients) with these relevant Marketo lists:
  6.  *
  7.  * 1024:  Subscribers | Free (New)
  8.  * 1040:  Subscribers | Paying
  9.  * 1041:  Subscribers | Former Paying
  10.  *
  11.  * The forms will aready put people on the first two lists, so this should be pretty good to start.
  12.  *    
  13.  */
  14.  
  15. // Include libraries
  16. require_once("header.for.crons.php");   // For errors and printing
  17. require_once($models.'class.core.php');
  18. require_once($models.'class.whmcs.php');
  19. require_once($models.'class.marketo.php'); // Kind of a hack, $marketo object defined within
  20.  
  21. // Declare objects
  22. $whmcs = new NCWHMCS;
  23. $core = new NCCore;
  24.  
  25. /*
  26.  * Phase 1: Get contacts
  27.  */
  28. echo "<pre><H2>GATHERING CONTACTS TO SYNC</H2>";
  29. echo "<pre><H3>GATHERING CLIENTS FROM WHMCS</H3>";
  30.  
  31. // Mega array of contacts.
  32. $contactArr = array();
  33.  
  34. // Add paying and former to the mega array.
  35. $allSubscriberServices = $whmcs->getChecklistSubscribers();
  36. $globalContactIncr = 0;
  37. foreach($allSubscriberServices AS $incr => $subscriber) {
  38.     $globalContactIncr++;
  39.     $contactArr[$globalContactIncr]["firstname"] = $subscriber["firstname"];
  40.     $contactArr[$globalContactIncr]["lastname"] = $subscriber["lastname"];
  41.     $contactArr[$globalContactIncr]["email"] = strtolower($subscriber["email"]);   
  42.     if($subscriber["domainstatus"] == "Active")
  43.         $contactArr[$globalContactIncr]["status"] = "Paying";  
  44.     else
  45.         $contactArr[$globalContactIncr]["status"] = "Former";      
  46. }
  47.  
  48. // Add free access to the mega array.
  49. $allFreeSubscribers = $whmcs->getChecklistFreeUsers();
  50. foreach($allFreeSubscribers AS $incr => $subscriber) {
  51.     if($core->search2dArrayForValue($contactArr,$subscriber["email"]) < 1) {
  52.         $globalContactIncr++;
  53.         $contactArr[$globalContactIncr]["firstname"] = $subscriber["firstname"];
  54.         $contactArr[$globalContactIncr]["lastname"] = $subscriber["lastname"];
  55.         $contactArr[$globalContactIncr]["email"] = strtolower($subscriber["email"]);   
  56.         $contactArr[$globalContactIncr]["status"] = "Free";
  57.     }
  58. }
  59.  
  60.  /*
  61.   * Phase 2: Sync to Marketo
  62.  */
  63. echo "<h2>SYNCING IT ALL TO MARKETO</H2>";
  64. echo "<h3>SCRUBING CLIENT DATA</H3>";
  65.  
  66. // Step 1:  Get existing Marketo Clients Lists to avoid unnecessary requests
  67. $freeMarketo = accessProtected($marketo->getLeadsByList("1024"),"data"); // Get free members.  Paginates to 10 without extra vars.
  68. $payingMarketo = accessProtected($marketo->getLeadsByList("1040"),"data"); // Get paying members.  Paginates to 10 without extra vars.
  69. $formerMarketo = accessProtected($marketo->getLeadsByList("1041"),"data"); // Get former members.  Paginates to 10 without extra vars.
  70.    
  71. // Step 2:  Scrub our data
  72. // 2a: Free
  73. if(isset($freeMarketo) && isset($freeMarketo["result"])) {
  74.     foreach($freeMarketo["result"] AS $incr => $alreadyMarkedFree) {
  75.         $matchId = $core->search2dArrayForValue($contactArr,$alreadyMarkedFree["email"]);
  76.         if($matchId > 0 && $contactArr[$matchId]["status"] == "Free") {
  77.             echo $contactArr[$matchId]["email"] . " is already on the free list (".$contactArr[$matchId]["status"].").<br>";   
  78.             unset($contactArr[$matchId]);  
  79.         } elseif($matchId > 0) {
  80.             $removal = $marketo->removeLeadsFromList("1024",array($alreadyMarkedFree["id"]));  // Remove lead from wrong list          
  81.         }
  82.     }
  83. }
  84.  
  85. // 2b: Paying
  86. if(isset($payingMarketo) && isset($payingMarketo["result"])) {
  87.     foreach($payingMarketo["result"] AS $incr => $alreadyMarkedPaying) {
  88.         $matchId = $core->search2dArrayForValue($contactArr,$alreadyMarkedPaying["email"]);
  89.         if($matchId > 0 && $contactArr[$matchId]["status"] == "Paying") {
  90.             echo $contactArr[$matchId]["email"] . " is already on the paying list (".$contactArr[$matchId]["status"].").<br>"; 
  91.             unset($contactArr[$matchId]);  
  92.         } elseif($matchId > 0) {
  93.             $removal = $marketo->removeLeadsFromList("1040",array($alreadyMarkedPaying["id"]));  // Remove lead from wrong list        
  94.         }
  95.     }
  96. }
  97.  
  98. // 2c: Former
  99. if(isset($formerMarketo) && isset($formerMarketo["result"])) {
  100.     foreach($formerMarketo["result"] AS $incr => $alreadyMarkedFormer) {
  101.         $matchId = $core->search2dArrayForValue($contactArr,$alreadyMarkedFormer["email"]);
  102.         if($matchId > 0 && $contactArr[$matchId]["status"] == "Former") {
  103.             echo $contactArr[$matchId]["email"] . " is already on the former list (".$contactArr[$matchId]["status"].").<br>"; 
  104.             unset($contactArr[$matchId]);  
  105.         } elseif($matchId > 0) {
  106.             $removal = $marketo->removeLeadsFromList("1041",array($alreadyMarkedFormer["id"]));  // Remove lead from wrong list        
  107.         }
  108.     }
  109. }
  110.  
  111. echo "<h3>ADDING/ORGANIZING REMAINING CLIENTS IN MARKETO</H3>";
  112.  
  113. // Step 3:  Add people to the right lists.
  114. $groupEmailSignals = array("abuse","billing","support","sales","info","bill.com","bills","management","accounting","accounts");
  115. $counter = 0;
  116. foreach($contactArr AS $incr => $contactToSync) {
  117.     $haltTheAdd = 0;
  118.     if(!array_key_exists("firstname",$contactToSync)) $contactToSync["firstname"] = "";
  119.     if(!array_key_exists("lastname",$contactToSync)) $contactToSync["lastname"] = "";
  120.  
  121.     // Skip crappy group email addresses.
  122.     foreach($groupEmailSignals AS $incr2 => $signal) {
  123.         if(stristr($contactToSync["email"],$signal)) $haltTheAdd = 1;
  124.     }
  125.  
  126.     if($haltTheAdd == 0) {
  127.         $counter++; // let's be nice to their rate limiting
  128.         if($counter < 50) {
  129.             // Determine the correct Marketo list ID
  130.             if(trim($contactToSync["status"]) == "Free") $listId = 1024;
  131.             elseif(trim($contactToSync["status"]) == "Paying") $listId = 1040;     
  132.             else $listId = 1041;
  133.            
  134.             $result = accessProtected($marketo->getLeadByFilterType("email",trim($contactToSync["email"])),"data");
  135.             if(array_key_exists("result",$result) && $result["success"] == 1 && count($result["result"]) > 0) {   // Already in Marketo - add to list
  136.                 // Add to list only - already in Marketo.  If they're on the wrong list currently, Smart Campaigns will fix that.
  137.                 $result = $marketo->addLeadsToList($listId,$result["result"][0]["id"]);
  138.                 echo "Put " . $contactToSync["email"] . " on the " . $contactToSync["status"] . " list (" .$listId .") in Marketo!<br>";
  139.             } elseif(isset($result["errors"])) {
  140.                 echo "<b>Error ". $result["errors"][0]["code"] .": </b>" .$result["errors"][0]["message"] ."<br>";
  141.             } else {
  142.                 // Add to Marketo - then add to list
  143.                 if(isset($contactToSync["firstname"]) && isset($contactToSync["lastname"])) $result = accessProtected($marketo->createLeads(array(array("email"=>$contactToSync["email"],"firstName"=>$contactToSync["firstname"],"lastName"=>$contactToSync["lastname"]))),"data");
  144.                 else $result = accessProtected($marketo->createLeads(array(array("email"=>$contactToSync["email"]))),"data");
  145.    
  146.                 // Then add to list
  147.                 if($result["success"] == 1 && array_key_exists("id",$result["result"][0])) $result = $marketo->addLeadsToList($listId,$result["result"][0]["id"]);
  148.                 echo "Added " . $contactToSync["email"] . " to Marketo and placed on the " . $contactToSync["status"] . " list (" .$listId .")!<br>";
  149.             }
  150.         } else {
  151.             echo "Rate limit hit.<br>";
  152.         }
  153.     }
  154. }
RAW Paste Data