groupName = $groupName; $this->username = $apiUsername; $this->password = $apiPassword; } /** * Get any numbers that were not added because they were in a stop group * @return array */ public function getStopped() { return $this->stopped; } /** * Get any numbers that were added * @return array */ public function getAdded() { return $this->added; } /** * Get any numbers that were not added because they were already in the group * @return array */ public function getDuplicates() { return $this->duplicates; } /** * Make the HTTP request. * * @param integer $number * @return boolean success of add */ public function add($number) { if (empty($this->groupName) OR empty ($this->username) OR empty ($this->password)) return false; $valid = false; // remove non-numerics $number = preg_replace("/[^0-9]/", "", $number); if(!is_numeric($number)) return false; $url_array = array('numbers'=>$number,'name'=>$this->groupName, 'username'=>$this->username, 'password'=>$this->password); $url_string = http_build_query($url_array, '', '&'); // we're using the curl library to make the request $curlHandle = curl_init(); curl_setopt($curlHandle, CURLOPT_URL, $this->url); curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $url_string); curl_setopt($curlHandle, CURLOPT_POST, 1); $responseBody = curl_exec($curlHandle); $responseInfo = curl_getinfo($curlHandle); curl_close($curlHandle); return $this->handleResponse($responseBody,$responseInfo); } private function handleResponse($body,$info) { if ($info['http_code']==200) { // successful submission $xml_obj = simplexml_load_string($body); // extract message id and credit usuage $atts = $xml_obj->added->attributes(); $this->qtyAdded = (int) $atts->quantity; if ($this->qtyAdded > 0) foreach ($xml_obj->added->number as $xml_number) $this->added[] = (string) $xml_number; $atts = $xml_obj->stopped->attributes(); $this->qtyStopped = (int) $atts->quantity; if ($this->qtyStopped > 0) foreach ($xml_obj->stopped->number as $xml_number) $this->stopped[] = (string) $xml_number; $atts = $xml_obj->duplicates->attributes(); $this->qtyDuplicates = (int) $atts->quantity; if ($this->qtyDuplicates > 0) foreach ($xml_obj->duplicates->number as $xml_number) $this->duplicates[] = (string) $xml_number; return true; } else { $this->qtyAdded = 0; $this->qtyDuplicates = 0; $this->qtyStopped = 0; $this->stopped = array(); $this->duplicates = array(); $this->added = array(); // error handling return false; } } } ?>