SMSAPI dokumentacija

Dokumentacija PrimeriGenerator kode

Pošiljanje SMS-a

SMS lahko pošiljamo na več različnih načinov. Najbolj vam priporočamo pošiljanje preko HTTPS POST protokola, saj je dolžina URL naslova pri HTTP GET omejena, HTTPS pa pomeni varno pošiljanje podatkov, ki vas obvaruje pred morebitno zlorabo. V primeru, da želite imeti v vaši aplikaciji obrazec, preko katerega bodo SMS-e pošiljali uporabniki, ne "POST-ajte" vsebine obrazca direktno na naš url, ampak njegovo vsebino najprej filtrirajte in validirajte.

SMS-e lahko pošiljate preko HTTP ali HTTPS. Slednji je varnejši.

Zgled pošiljanja SMS-a s pomočjo HTTP GET

//prepare data
$url = 'http://www.smsapi.si/poslji-sms';            //url we are posting to (defined in sms api documentation)
$data = array('un' => urlencode('api_username'),     //api username
              'ps' => urlencode('api password'),     //api pass
              'from' => urlencode('031492148'),      //don't send as int
              'to' => urlencode('031492148'),        //don't send as int
              'm' => urlencode('Testno sporocilo'),  //msg
              'cc' => urlencode('386')               //don't send as int 
             );
             
//send SMS via HTTP GET
$url = $url . http_build_query($data);
$response = file_get_contents($url);
echo $response;


Zgled pošiljanja SMS-a s pomočjo HTTP GET iz ukazne vrstice (MS-DOS, Linux, Unix)

wget.exe -nv --output-document .\smsapisi.log "http://www.smsapi.si/poslji-sms?un=api_username&ps=api password&from=031492148&to=031492148&m=Testno sporocilo&cc=386"

Zgled pošiljanja SMS-a s pomočjo HTTP POST

 
//prepare data
$url = 'http://www.smsapi.si/poslji-sms';            //url we are posting to (defined in sms api documentation)
$data = array('un' => urlencode('api_username'),     //api username
              'ps' => urlencode('api password'),     //api pass
              'from' => urlencode('031492148'),      //don't send as int
              'to' => urlencode('040859974'),        //don't send as int
              'm' => urlencode('Testno sporocilo'),  //msg
              'cc' => urlencode('386')               //don't send as int 
             );

$response  = doPostRequest($url, $data);
echo($response);


/**
 * Posts $data on $url and returns content of website
 * POST request was made to.
 * 
 * @param $url - url where we are posting data
 * @param $data - array with key => value pairs for post 
 *                e.g. array('un' => 'example', 'ps' => 'pass', ...)
 *
 * @return string
 */
function doPostRequest($url, $data) {
    // Initialisation
    $ch=curl_init();
    
    // Set parameters
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);

    // Activate the POST method
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1) ;
    
    // Request
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    
    // execute the connexion
    $result = curl_exec($ch);
    
    // Close it
    curl_close($ch);
    
    return $result;
}

Pošiljanje SMS-a in ID pošiljatelja

SMS lahko pošljete tudi s poljubnim ID pošiljateljem. ID pošiljatelja morate najprej potrditi preko smsapi.si -> Moja stran.

Zgled pošiljanja SMS-a in poljubnim ID pošiljateljem s pomočjo HTTP GET

//prepare data
$url = 'http://www.smsapi.si/poslji-sms';            //url we are posting to (defined in sms api documentation)
$data = array('un' => urlencode('api_username'),     //api username
              'ps' => urlencode('api password'),     //api pass
              'from' => urlencode('031492148'),      //don't send as int
              'to' => urlencode('031492148'),        //don't send as int
              'm' => urlencode('Testno sporocilo'),  //msg
              'cc' => urlencode('386'),               //don't send as int 
              'sid' => urlencode('1'),                  //don't send as int
              'sname' => urlencode('Izbran ID pošiljatelja'),  // Sender ID
             );
             
//send SMS via HTTP GET
$url = $url . http_build_query($data);
$response = file_get_contents($url);
echo $response;



Prejemanje poročil o dostavi in odgovorov na SMS-e

Poročilo o dostavi vključite tako, da med poslane parametre dodate "dr=1". Če lahko uporabniki odgovarjajo na vaša sporočila, jih prejmete na isti URL naslov, kot poročila o dostavi.

Zgled prejemanja poročila o dostavi

//if $_GET['id'] is set, we know it is a delivery report, otherwise reply text
if(isset($_GET['id'])) {
    $id = (int)$_GET['id'];  //id-s are allways int
    $status = strip_tags($_GET['status']);
    
    //do whatever you want here. Maybe store it to database
    //we will just echo sms ID and status.
    echo 'SMS ID: ' . $id . ' STATUS: ' . $status;
} else {
    $id = (int)$_POST['smsId'];
    $msg = strip_tags($_POST['m']);
    $fromNumber = $_POST['from']; //replier
    $toNumber = $_POST['to']; //number to which user replied to (can be shared from SMSAPI or own)
    $date = date('Y-m-d', (int)$_POST['time']);
    
    echo 'SMS ID: ' . $id . ' MSG: ' . $msg . ' FROM: ' . $fromNumber . ' TIME: ' . $date;
}

Poizvedba o ceni sporočila

//send API username and pass to SMSAPI and fetch result 
$response = file_get_contents('http://www.smsapi.si/dobi-ceno?un=API_USERNAME&ps=API_PASS');

//do we have more than one quote for SMS-es?
if(strpos($response, '##') !== false) {
    //get values in array
    $quotes = explode('##', $response);
    print_r($quotes);    
} else {
    echo $response;
}

Poizvedba o stanju kreditov

//send API username and pass to SMSAPI and fetch result 
$response = file_get_contents('http://www.smsapi.si/preveri-stanje-kreditov?un=API_USERNAME&ps=API_PASS');

echo $response;