Project

General

Profile

SMS API

Pasii necesari pentru a utiliza SMS API:

  • Modemul are restrictie pe IP (sunt permise doar IP-ul centralei si al nostru de aici, pt teste)
  • Pentru a le modifica, se editeaza /var/www/sms/sms-api.php unde se adauga IP-ul.
  • Dispozitivul are interfata grafica, daca se conecteaza un HDMI, tastatura si mouse se poate edita, seta IP/ Wifi, etc.
  • Dispozitivul este setat in mod DHCP, trebuie pus in spatele unui router si forwardat portul 80 (de trimis portul si IP-ul pentru a le conecta cu centrala catre noi).
  • SIM-ul folosit in modem trebuie sa fie fara PIN.

Apel API:
http://IP_APARAT/sms-api.php?action=send&login[username]=XXXX&login[password]=XXXX&data[from]=XXXX&data[to]=XXXX&data[priority]=1&data[text]=Hello+world

Exemplu:
user api: YOUR-USERNAME
parola: YOUR-PASSWORD

http://192.168.0.160/sms-api.php?user=YOUR-USERNAME&password=YOUR-PASSWORD&to=0123456789&text=Hello+world+from+API

Limbajul de programare folosit pentru exemplele urmatoare este PHP

Exemplu trimitere prin SMS API:

$postdata = array(
    'action' => 'send',
    'login' => array(
        'username' => 'YOUR-USERNAME',
        'password' => 'YOUR-PASSWORD'
    ),
    'data' => array(
        'from' => 'BRAND-NAME', // carriers will ignore that
        'to' => '0123456789',
        'text' => 'test message',
        'priority' => 1
    )
);

$result = curlPost('http://YOUR-IP-OR-URL/sms-api.php', http_build_query($postdata));

if (empty($result)) {
    $error = 'SMS error: empty result';
}
else {
    $json = json_decode($result);

    if (empty($json->response)) {
        $error = 'SMS error: invalid json response - ' . $result;
    }
    elseif (empty($json->message)) {
        $error = 'SMS error: reponse message is missing - ' . $result;
    }
    elseif ($json->response != 'success') {
        $error = 'SMS error: ' . (!empty($json->message) ? $json->message : 'N/A');
    }
    elseif ($json->message != 'SMS sent') {
        $error = 'SMS error: invalid reponse message - ' . $json->message;
    }
}

Exemplu citire prin SMS API:


 $postdata = array( 
     'action' => 'receive', 
     'login' => array( 
         'username' => 'YOUR-USERNAME', 
         'password' => 'YOUR-PASSWORD' 
     ) 
 ); 

$result = curlPost('http://YOUR-IP-OR-URL/sms-api.php', http_build_query($postdata)); 

 if (empty($result)) { 
     $error = 'SMS error: empty result'; 
 } 
 else { 
     $json = json_decode($result); 

     if (empty($json->response)) { 
         $error = 'SMS error: invalid json response - ' . $result; 
     } 
     elseif (empty($json->message)) { 
         $error = 'SMS error: reponse message is missing - ' . $result; 
     } 
     elseif ($json->response != 'success') { 
         $error = 'SMS error: ' . (!empty($json->message) ? $json->message : 'N/A'); 
     } 
     elseif (!empty($json->data) && is_array($json->data)) { 
         $messages = $json->data; 
     } 
 } 
 

Functie curlPost:

/**
 * @param string $url
 * @param array $postdata
 * @return bool|mixed
 */
function curlPost($url, $postdata) {
    if (!function_exists('curl_init')) {
        //error_log("PHP Warning: CURL is not installed. curlPost function will now exit");
        return false;
    }

    // init CURL
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSLVERSION, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postdata));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 5);

    // get response
    $output = curl_exec($ch);

    if ($output === false) {
        $error_code = curl_error($ch);
    }
    else {
        $error_code = 'N/A';
    }

    // get headers
    $headers = curl_getinfo($ch);

    // close connection
    curl_close($ch);

    // if request is OK, return contents
    if ($headers['http_code'] == 200 || $headers['http_code'] == 226) {
        return $output;
    }
    // return an error
    else {
        //error_log("PHP Warning: Bad CURL header ({$headers['http_code']}) on URL: {$url}, error code: {$error_code}");
        return false;
    }
}

Disponibil in alte limbi: EN

Go to top