Last active
February 15, 2017 14:02
-
-
Save mslinnea/8410515358553cfd09c2fbd8a7b8f1ba to your computer and use it in GitHub Desktop.
Use Twilio to Text Yourself
This file contains 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
<?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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For more details, check out my blog post Use Twilio to Programmatically Text Yourself!