What's wrong with my jQuery and twilio code?
I went to these two ( 1 , 2 ) for one of my projects.
I am on PHP. So I'm not sure how to do this.
I got an SSL certificate error one day while trying to test it. My SMS messages are not being sent.
I don't get any response or warning or anything when I test it with my HTML.
Please help me if there is a twilio and PHP expert . this is the form i am using in HTML
<form id="frm" name="frm">
<div class="form-group">
<label for="tel">Phone:</label>
<input type="tel" class="form-control" id="phoneNumber" name="phoneNumber" required>
</div>
<button class="btn" type="submit" id="submit">Submit</button>
</form>
My script
$("#frm").submit(function(e){
e.preventDefault();
$.post("sendnotifications.php", $("#frm").serialize(),
function(data){
if(data.sms_sent == 'OK'){
alert("Message Sent");
} else {
alert("Message Not Sent");
}
}, "json");
});
and here is my sendnotifications.php file
<?php/* Send an SMS using Twilio. You can run this file 3 different ways:
*
* - Save it as sendnotifications.php and at the command line, run
* php sendnotifications.php
*
* - Upload it to a web host and load mywebhost.com/sendnotifications.php
* in a web browser.
* - Download a local server like WAMP, MAMP or XAMPP. Point the web root
* directory to the folder containing this file, and load
* localhost:8888/sendnotifications.php in a web browser.
*/
// Step 1: Download the Twilio-PHP library from twilio.com/docs/libraries,
// and move it into the folder containing this file.
require "Services/Twilio.php";
// Step 2: set our AccountSid and AuthToken from www.twilio.com/user/account
$AccountSid = "ACexxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$AuthToken = "fafyyyyyyyyyyyyyyyyyyyyyyyyyyyyy";
// Step 3: instantiate a new Twilio Rest Client
$http = new Services_Twilio_TinyHttp(
'https://api.twilio.com',
array('curlopts' => array(
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
))
);
$client = new Services_Twilio($sid, $token, "2010-04-01", $http);
// Step 4: Get phone number from the test-sms form
$phone=$_POST["phoneNumber"];
// Step 5: Create SMS
$sms = $client->account->sms_messages->create(
// Change the 'From' number below to be a valid Twilio number
// that you've purchased, or the (deprecated) Sandbox number
"717-xxx-xxxx",
// the number we are sending to - Any phone number
$phone,
// the sms body
"Get our app now: http://example.com/ourapp"
);
// Display a confirmation message on the screen
$sms_check='OK'; //Use Twilio callback here
$return_json = '{"sms_sent":"' . $email_check . '"}';
echo $return_json;
?>
Please tell me what I need to change to get my site working.
I changed $ http in sendnotifications.php after going to github faq twilio to prevent ssl certificate error. What else do I need to do?
Edit: I solved the problem using the code below.
<?php
// Step 1: Download the Twilio-PHP library from twilio.com/docs/libraries,
// and move it into the folder containing this file.
require_once "Services/Twilio.php";
// Step 2: set our AccountSid and AuthToken from www.twilio.com/user/account
$AccountSid = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$AuthToken = "04dxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// Twilio REST API version
$version = "2010-04-01";
// Step 3: instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken, $version);
// Step 4: Get phone number from the test-sms form
$phone=$_POST["phoneNumber"];
try{
$message = $client->account->messages->create(array(
"From" => "+1xxxxxxxxxx",
"To" => $phone,
"Body" => "This code is for testing!",
));
$sms_check='OK';
}
catch (Exception $e) {
$sms_check = 'Error';
}
$return_json = '{"sms_sent":"' . $sms_check . '"}';
echo $return_json;
?>
But now I am getting an error on my server.
SMS is being sent but this error appears.
Error: Could not decode response body as JSON. This probably indicates a server error 500
Does anyone know any way to solve this problem. My site is hosted on altervista.
Edit I solved the 500 error
By replacing the tinyHttp.php file inside "Services / Twilio" with the edited tinyhttp.php file from github .. The link of the edited github file is in the comments section.
source to share
Current issue:
try it
require_once "Services/Twilio.php";
$AccountSid = "ACexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$AuthToken = "fafxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$client = new Services_Twilio($$AccountSid, $AuthToken);
$phone=$_POST["phoneNumber"];
try{
$client->account->messages->sendMessage("+17xxxxxx", $phone, "This code is for testing an android app website!! Get our app now: http://bit.ly/ourapp");
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
Past issue:
It looks like you are not consistent with your variables. $sid
and $token
they can not be installed, you have used $AccountSid
and $AuthToken
to install your variables. try it
$client = new Services_Twilio($AccountSid, $AuthToken, "2010-04-01", $http);
source to share