Skip to content

Instantly share code, notes, and snippets.

@mslinnea
Last active February 15, 2017 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mslinnea/8410515358553cfd09c2fbd8a7b8f1ba to your computer and use it in GitHub Desktop.
Save mslinnea/8410515358553cfd09c2fbd8a7b8f1ba to your computer and use it in GitHub Desktop.
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
}
}
@mslinnea
Copy link
Author

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