Create a PHP script to connect to our API
Now we need a create php script to check values submitted by the form, prepare the message, send it to EZ Texting via our API, and process a response.
Copy and past this chunk of php into your favorite editor and save it as 'send.php' in the same directory as your form:
<?php if (!empty($_POST)) { /* check if phone number is valid */ $phone_number = trim($_POST["phone_number"]); if (empty($phone_number)) { exit("Phone number cannot be blank."); } if (strlen($phone_number) != 10) { exit("Invalid phone number. Phone number length should be 10 digits."); } if (!is_numeric($phone_number)) { exit("Invalid phone number. Phone number should contain only digits."); } /* check if message body is not blank and doesn't exceed allowable limit */ $subject = trim($_POST["subject"]); $message = trim($_POST["message"]); if (empty($subject) && empty($message)) { exit("Message cannot be blank."); } // subject is wrapped in brackets and separated with a whitespace, so count for 3 extra characters $strlen_subject = ($subject != "") ? strlen($subject) + 3 : 0; $strlen_message = strlen($message); $message_type = $_POST["message_type"]; if ($message_type == 1 && ($strlen_subject + $strlen_message > 160)) { exit("In case of express delivery, message length should not exceed 160 characters."); } elseif ($strlen_subject + $strlen_message > 130) { exit("In case of standard delivery, message length should not exceed 130 characters."); } /* prepare data for sending */ $data = array( "User" => "user", /* change to your EZ Texting username */ "Password" => "password", /* change to your EZ Texting password */ "PhoneNumbers" => array($phone_number), "Subject" => $subject, "Message" => $message, "MessageTypeID" => $message_type ); /* send message */ $curl = curl_init("https://app.eztexting.com/sending/messages?format=json"); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($curl); curl_close($curl); /* parse result of API call */ $json = json_decode($response); switch ($json->Response->Code) { case 201: exit("Message Sent"); case 401: exit("Invalid user or password"); case 403: $errors = $json->Response->Errors; exit("The following errors occurred: " . implode('; ', $errors)); case 500: exit("Service Temporarily Unavailable"); } } ?>
Now upload both files to your web server. Open your html file in a web browser. And try sending a message.