<?
/**
 * Project: Oxysms.com API
 * Author: PIERRE Olivier <olivier@oxeron.com>
 * Date: 01/10/2012
 * File: api_sms.class.php
 * Version: 2.1
 */
 
class sms
{
	var $server = "www.oxysms.com";
	var $port = "80";
	var $script_receive = "scripts/receive.php";
	var $script_query = "scripts/query.php";
	var $flash = 0;
	var $unicode = 0;
	var $use_backup_route_on_failure = '0';
	
	/**
	* Function to send a message to a multiple list of recipient 
	*/
	function send($project_login="",$project_password="")
	{			
		// Build and encode the URL.
		$url = "http://".$this->server.":".$this->port."/".$this->script_receive;	
		
		$fields = array(
            'project_login' => urlencode($project_login),
            'project_password' => urlencode($project_password),
            'from' => urlencode($this->from),
            'to' => urlencode($this->to),
            'flash' => urlencode($this->flash),
            'unicode' => urlencode($this->unicode),
            'use_backup_route_on_failure' => urlencode($this->use_backup_route_on_failure),
            'text' => urlencode($this->text)            
        );        
	
		$fields_string = '';
		foreach($fields as $key => $value) 
		{ 
			$fields_string .= $key.'='.$value.'&'; 
		}
		$fields_string = rtrim($fields_string, '&');
		
		// Make the HTTP call
		$ch = curl_init();		
	    curl_setopt($ch, CURLOPT_HEADER, 0);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch,CURLOPT_URL, $url);
		curl_setopt($ch,CURLOPT_POST, count($fields));
		curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
	    $result=curl_exec($ch);
	    curl_close($ch);
		
		// Error connecting to the server
		if ($result === false)
			return false;
			
		// explode the result with "\n" for each message 
		// (multiple recipients or cut message)
		$sms_list=explode("\n",$result);
		$return=array();
		foreach($sms_list as $k => $sms)
		{
			if (trim($sms)!="") 
			{
				// then explode with "-" : 
				$sms_info=explode("||",$sms);
				// remove spaces
				foreach($sms_info as $k2 => $info)
				{
					$sms_info[$k2]=trim($sms_info[$k2]);
				}
				array_push($return,$sms_info);
			}
		}
		return $return;
	}		
	
	/**
	* Function to query 
	*/
	function query($project_login="",$project_password="",$action="",$other_parameter="")
	{
		
		// Build and encode the URL.
		$url = "http://".$this->server.":".$this->port."/".$this->script_query;	
		
		$fields = array(
            'project_login' => urlencode($project_login),
            'project_password' => urlencode($project_password),
            'action' => urlencode($action)
        );        
	
		$fields_string = '';
		foreach($fields as $key => $value) 
		{ 
			$fields_string .= $key.'='.$value.'&'; 
		}
		if ($other_parameter != '') 
		{
        	$fields_string .= $other_parameter.'&';
        }
		$fields_string = rtrim($fields_string, '&');

		// Make the HTTP call
		$ch = curl_init();		
	    curl_setopt($ch, CURLOPT_HEADER, 0);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch,CURLOPT_URL, $url);
		curl_setopt($ch,CURLOPT_POST, count($fields));
		curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
	    $result=curl_exec($ch);
	    curl_close($ch);
		
		// Error connecting to the server
		if ($result === false)
			return false;
		
		$return=array();
		$sms_info=explode("||",$result);
		foreach($sms_info as $k2 => $info)
		{
			$sms_info[$k2] = trim($sms_info[$k2]);
		}
		array_push($return,$sms_info);

		return $return;
	}
		
}
?>