I am storing sms received from twilio in a database so I can use them later. When I did this in the sandbox it worked. However when I upgraded to a regular phone number the number received is the same as was sent to, but +1 (or for xxxxxxxxxx where the x's are the original number, it looks more like 1xxxxxxxxxx+)
I therefore changed the mysql_query to the following: but it is still not working. What can be done to recognize that this is the original phone number?
<?php
$starttime = time();
$number = $_POST['number'];
$number1 = "1" . $number;
$number2 = $number . "1";
$number3 = "+1" . $number;
$number4 = $number . "+1";
$number5 = "+" . $number . "1";
$number6 = "1" . $number . "+";
$number7 = $number."1+";
$received = mysql_query("SELECT * FROM sms_received
WHERE (responder='$number' OR responder='$number1'
OR responder='$number2' OR responder='$number3'
OR responder='$number4' OR responder='$number5'
OR responder='$number6' OR responder='$number6')
AND (body='y' OR body='yes' OR body='Y' OR body='Yes' OR 'yea' OR 'Yea')
AND timestamp BETWEEN ".date('Y-m-d H:i:s', strtotime($starttime))." AND NOW()");
?>
But still, nothing is being received. Any ideas how else I can check whether an sms has been received from the user? I can see in the database that it's there... but the mysql isn't finding it. It worked before, when the number sent was identical to the number received from, but with the added +1 it screws it up. (the code before just had WHERE responder = '$number'
and it worked, but the additional variables didn't help it).
Does this code have too many OR's? Is that even a problem?
UPDATE:
Thanks, here is the function I'm using to strip the number down to xxxxxxxxxx format, before saving it to the database:
function checkPhone($responder){
$items = Array('/\ /', '/\+/', '/\-/', '/\./', '/\,/', '/\(/', '/\)/', '/[a-zA-Z]/');
$clean = preg_replace($items, '', $responder);
if (substr($clean, 0, 1) == "1") {
return substr($clean, 1, 10);
}
else {
return substr($clean, 0, 10);
}
}
$number = checkPhone($responder);
Text +1 (855) 747-7626* with a phone number to check if it is from Twilio. That's +1 (855) 747-ROBO. If you're getting spam calls or texts from a Twilio number, this text hotline will help you report it.
To format phone numbers in the US, Canada, and other NANP (North American Numbering Plan) countries, enclose the area code in parentheses followed by a nonbreaking space, and then hyphenate the three-digit exchange code with the four-digit number.
Calling these numbers from outside the UK Depending on the service provider within the country of origin, you may be able to call an 08 or 03 number from outside the UK. To do so, you will need to drop the 0 from the start of the number and instead dial +44 followed by the rest of the number.
A Twilio number can include different communication capabilities such as voice, SMS, MMS, and fax. You can programmatically manage these numbers and build complex IVR apps and chatbots using Twilio's REST APIs.
Twilio returns numbers in a format called E.164, which is an internationally recognized standard for phone number formatting.
In general, it's best practice to standardize the number to E164 BEFORE you store it in the database. That way you don't have to worry about storing different data with two different copies of the same number - eg 925-555-1234 and (925) 5551234.
Google has a libphonenumber library that will convert numbers for you. It works with Javascript, C++, Java, and Python.
If you are using PHP, and only using US/Canadian numbers, you can write a function to normalize phone numbers, that does something like the following:
- Strip out all non number characters from the phone number
(parentheses, dashes, spaces) - you can use a function like preg_replace
- if the phone number begins with a +1, do nothing
- if the phone number begins with a 1, add a +
- else, add a +1 to the beginning of the number.
- finally, store it in the database.
I hope that helps - please let me know if you have more questions.
Kevin
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With