Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Use Twilio to Text Yourself
<?php
// Add the Twilio library via composer
require_once( __DIR__ . '/vendor/autoload.php' );
// Use the Twilio REST API Client
use Twilio\Rest\Client;
class Twilio_Gateway {
// Fill these in - see your Twilio dashboard
private $twilio_sid = '';
private $twilio_auth = '';
private $twilio_from = ''; // The phone number you bought from Twilio
private $twilio_to = ''; // Your phone number!
// the max number of messages you want to send per day
// This is totally optional, but I like to have a safeguard in place.
protected $limit = 20;
public function text_me( $msg ) {
if ( $this->over_limit() ) {
// @todo: log error message
return false;
} else {
$client = new Client( $this->twilio_sid, $this->twilio_auth );
$response = $client->messages->create(
$this->twilio_to,
array(
'from' => $this->twilio_from,
'body' => $msg,
)
);
$response = $response->toArray();
if ( ! empty( $response['errorCode'] ) ) {
//@todo: log error message
$error_message = $response['errorMessage'];
return false;
} else {
$this->log_success();
return true;
}
}
}
public function log_success() {
// implement this
}
private function over_limit() {
// implement this
// return true/false
}
}
Owner

mslinnea commented Feb 15, 2017

For more details, check out my blog post Use Twilio to Programmatically Text Yourself!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment