Home » Php » php – How to Send automated SMS using API?

php – How to Send automated SMS using API?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have this condition to scheduled my sms. (ERROR) but the sms I’m using is POST method I need to be automated. And send sms to the number of the current id. I don’t know how to get the number of the current id and send the message. Here is my code:

$duedate = "";
$date_now = date("Y-m-d");
date_default_timezone_set('Asia/Manila');
$date_now = strtotime($date_now);
$duedate = strtotime($duedate);

if ($duedate <= $date_now) {
    function itexmo($number, $message, $apicode) {
        $url = 'https://www.itexmo.com/php_api/api.php';
        $itexmo = array('1' => $number, '2' => $message, '3' => $apicode);
        $param = array(
            'http' => array(
                'header' => "Content-type: application/x-www-form-urlencoded\r\n",
                'method' => 'POST',
                'content' => http_build_query($itexmo),
            ),
        );
        $context = stream_context_create($param);
        return file_get_contents($url, false, $context);
    }

    if ($_POST) {
        $number = $number;
        $name = $name;
        $api = "API";
        $text = $name." : ".$number;

        if (!empty($_POST['name']) && ($_POST['number']) && ($_POST['msg'])) {
            $result = itexmo($number, $text, $api);
            if ($result == "") {
                echo "iTexMo: No response from server!!!
Please check the METHOD used (CURL or CURL-LESS). If you are using CURL then try CURL-LESS and vice versa.  
Please CONTACT US for help. ";
            } elseif ($result == 0) {
                echo "Message Sent!";
            } else {
                echo "Error Num ". $result . " was encountered!";
            }
        }
    }
}
How to&Answers:

It is better to use PHP cURL than stream_context_create and file_get_contents.
Take a look at below code, it should help you.

function itexmo($number,$message,$apicode) 
{
    $url = 'https://www.itexmo.com/php_api/api.php';        
    $postValues =  http_build_query(['1' => $number, '2' => $message, '3' => $apicode])

        $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postValues);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "cache-control: no-cache",
                "content-type: application/x-www-form-urlencoded"
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($ch);
    curl_close ($ch);

    return $response;
}

You can learn more PHP cURL.