SMS API / Gateway

madhawas1

Active member
  • Jul 5, 2015
    542
    249
    43
    මචන් මගේ project එකක demonstration වලට use කරන්න ලංකාවේ SMS API එකක් ඕන, REST API එකක් නම් වැඩේ ලේසියි. ඔයගොල්ලෝ දන්න එකක් තියෙනවානම් මට පොඩ්ඩක් කියනවද?
     

    smtsampath

    Active member
  • Dec 11, 2008
    344
    65
    28
    New York City, NY, USA
    Me man PHP walin karapu REST API Backend Class ekak. Twillio thamai use karanne. Lankawe nam nam newe.

    ////////////////////////////////////////////////////////////////
    class Twilio {

    const account = 'xxxxxxxxxxxxxx';
    const authToken = 'xxxxxxxxxxxxxx';
    const fromnumber = 'xxxxxxxxxx';

    public static function generateCode() {
    return rand(1000, 9999) . "";
    }

    public static function sendCode($phone_number, $code) {
    $body = "Your verification code is: " . $code;
    $data = array();
    $data[] = "To=" . urlencode($phone_number);
    $data[] = "From=" . urlencode(Twilio::fromnumber);
    $data[] = "Body=" . urlencode($body);

    $postString = implode("&", $data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.twilio.com/2010-04-01/Accounts/' . Twilio::account . '/Messages.json');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
    curl_setopt($ch, CURLOPT_USERPWD, Twilio::account . ':' . Twilio::authToken);

    $output = curl_exec($ch);
    $decodedOutput = json_decode($output, true);

    curl_close($ch);

    if (isset($decodedOutput['code']) || isset($decodedOutput['code']) > 0) {
    $custom_message = "The number '$phone_number' is not a valid phone number";

    if (isset($decodedOutput['detail'])) {
    $message = $decodedOutput['detail'];
    } else if (isset($decodedOutput['message'])) {
    $message = $decodedOutput['message'];
    }
    $errorCode = ErrorCode::create(ErrorCode::INVALID_PHONE_NUMBER, $custom_message);
    throw new ErrorCodeException($errorCode);
    }
    return SuccessCode::create(SuccessCode::GENERIC_SUCCESS, "Confirmation Code Sent");
    }
    }
    /////////////////////////////
     
    Last edited:
    • Like
    Reactions: madhawas1

    madhawas1

    Active member
  • Jul 5, 2015
    542
    249
    43
    ගයාන් ලක්මාල්;20667468 said:

    Thank you Rep 2+


    Thank you machan Rep 2+

    Me man PHP walin karapu REST API Backend Class ekak. Twillio thamai use karanne. Lankawe nam nam newe.

    ////////////////////////////////////////////////////////////////
    class Twilio {

    const account = 'xxxxxxxxxxxxxx';
    const authToken = 'xxxxxxxxxxxxxx';
    const fromnumber = 'xxxxxxxxxx';

    public static function generateCode() {
    return rand(1000, 9999) . "";
    }

    public static function sendCode($phone_number, $code) {
    $body = "Your verification code is: " . $code;
    $data = array();
    $data[] = "To=" . urlencode($phone_number);
    $data[] = "From=" . urlencode(Twilio::fromnumber);
    $data[] = "Body=" . urlencode($body);

    $postString = implode("&", $data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.twilio.com/2010-04-01/Accounts/' . Twilio::account . '/Messages.json');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
    curl_setopt($ch, CURLOPT_USERPWD, Twilio::account . ':' . Twilio::authToken);

    $output = curl_exec($ch);
    $decodedOutput = json_decode($output, true);

    curl_close($ch);

    if (isset($decodedOutput['code']) || isset($decodedOutput['code']) > 0) {
    $custom_message = "The number '$phone_number' is not a valid phone number";

    if (isset($decodedOutput['detail'])) {
    $message = $decodedOutput['detail'];
    } else if (isset($decodedOutput['message'])) {
    $message = $decodedOutput['message'];
    }
    $errorCode = ErrorCode::create(ErrorCode::INVALID_PHONE_NUMBER, $custom_message);
    throw new ErrorCodeException($errorCode);
    }
    return SuccessCode::create(SuccessCode::GENERIC_SUCCESS, "Confirmation Code Sent");
    }
    }
    /////////////////////////////

    code ekata godak sthuthie machan, Twilio wala account ekak hada ganna mama balannam. poddak e gena visthara kiyanna puluwanda? Rep 2+