Created
November 6, 2014 09:46
-
-
Save cnicodeme/7b2191914f4464f064b3 to your computer and use it in GitHub Desktop.
Send SMS to Twilio without the sdk (simplified version)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* You need to know it's better to use the always up-to-date sdk from Twilio | |
* This is just a snippet that demonstrate how to send an sms to Twilio using curl. | |
* It works well though ;) | |
*/ | |
function twilio($to, $message) { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, str_replace('{auth_key}', TWILIO_KEY, 'https://api.twilio.com/2010-04-01/Accounts/{auth_key}/Messages.json')); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 5); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); | |
curl_setopt($ch, CURLOPT_USERPWD, TWILIO_KEY.':'.TWILIO_TOKEN); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, array( | |
'From' => TWILIO_PHONE, | |
'To' => $to, | |
'Body' => $message | |
)); | |
$result = curl_exec($ch); | |
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code | |
curl_close ($ch); | |
return (substr($status_code, 0, 1) === '2'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment