/** * login to the Bronto API * * $username Bronto username * $password Bronto password * $sitename Bronto sitename * * return binding */ function login($username, $password, $sitename) { $BRONTO_WSDL = 'http://api.bronto.com/?q=mail_3&wsdl'; // Set some defaults set_time_limit(600); ini_set("memory_limit", "150M"); ini_set("default_socket_timeout", 300); ini_set("soap.wsdl_cache_enabled", 0); // creates a handle to bronto API to be used in all subsequent calls $bapi = new SoapClient( $BRONTO_WSDL, array("trace" => 1) ); // call login operation to obtain a sessionId $parameters = array("username" => $username, "password" => $password, "sitename" => $sitename); $result = $bapi->login($parameters); // just return null if there was a problem if(!$result->return->success) { return(null); } // setup soap header which will be used on all calls to the api $session_header = new SoapHeader("http://api.bronto.com", "sessionHeader", array("sessionId" => $result->return->sessionId)); $bapi->__setSoapHeaders(array($session_header)); return($bapi); } /** * read the deliveries for a date range. * * $bapi - binding to bronto api * $start_date - the starting date * $end_date - the ending date * * date/time format: UTC timezone "yyyy-mm-dd hh:mm:ss" * hh:mm:ss is not required to specify a date * * return an array of delivery objects */ function read_deliveries($bapi, $start_date, $end_date) { $attributes = array(); $attributes["status"] = true; $filter = array(); $filter["criteria"] = array(); $criteria1 = array(); $criteria1["attribute"] = "start"; $criteria1["comparison"] = ">"; $criteria1["value"] = array(); $criteria1["value"]["value"] = $start_date; $criteria1["value"]["type"] = "date"; $filter["criteria"][] = $criteria1; $criteria2 = array(); $criteria2["attribute"] = "start"; $criteria2["comparison"] = "<"; $criteria2["value"] = array(); $criteria2["value"]["value"] = $end_date; $criteria2["value"]["type"] = "date"; $filter["criteria"][] = $criteria2; $filter["operator"] = "and"; $parameters = array("attributes"=>$attributes, "filter"=>$filter); $results = $bapi->readDeliveries($parameters); return($results->return->deliveries); } /** * count the unique opens for the given list of deliveries * * $bapi - binding the bronto api * $deliveries - list of delivery objects * * return array of contact id associated with the number of opened emails */ function count_unique_opens_for_deliveries($bapi, $deliveries) { // ths list of contacts with the unique open count $unique_opens = array(); // print a "working" message and ... echo "Reading opens"; foreach($deliveries as $delivery) { $opens = read_opens_for_delivery($bapi, $delivery->id); if($opens) { echo "."; // get a list of unique contacts that opened this delivery $contacts = array(); foreach($opens as $open) { //if(!array_key_exists($open->contactId, $contacts)) if(!in_array($open->contactId, $contacts)) { //$opens_for_delivery[$open->contactId] = 1; $contacts[] = $open->contactId; } } // aggregate contacts list to unique_opens list foreach($contacts as $contact) { $unique_opens[$contact] += 1; } } } echo "\n"; return($unique_opens); } $bapi = login("username", "password", "sitename"); if(!$bapi) { exit("Error logging in. Exiting...\n"); } $deliveries = read_deliveries($bapi, "2009-08-01", "2009-08-10"); $unique_opens = count_unique_opens_for_deliveries($bapi, $deliveries); print_r($unique_opens);