Skip to content

Instantly share code, notes, and snippets.

@mheadd
Created May 16, 2011 14:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mheadd/974505 to your computer and use it in GitHub Desktop.
Save mheadd/974505 to your computer and use it in GitHub Desktop.
A simple PHP script that processes and saves inbound SMS messages from SMSified.
<?php
/*
* Convenience class that parses inbound SMSified JSON into a simple object.
*/
class InboundMessage {
// Class properties.
public $timeStamp;
public $destinationAddress;
public $message;
public $messageId;
public $senderAddress;
// Class constructor.
public function __construct($json) {
$notification = json_decode($json);
$this->timeStamp = $notification->inboundSMSMessageNotification->inboundSMSMessage->dateTime;
$this->destinationAddress = $notification->inboundSMSMessageNotification->inboundSMSMessage->destinationAddress;
$this->message = $notification->inboundSMSMessageNotification->inboundSMSMessage->message;
$this->messageId = $notification->inboundSMSMessageNotification->inboundSMSMessage->messageId;
$this->senderAddress = $notification->inboundSMSMessageNotification->inboundSMSMessage->senderAddress;
}
// Convert object to CSV string.
public function __toString() {
$csv = "";
foreach($this as $key => $value) {
$csv .= $value . ",";
}
$csv .= "\n";
return $csv;
}
}
// Get the JSON payload sumbitted from SMSified.
$json = file_get_contents("php://input");
// Create a new SMS object.
$SMS = new InboundMessage($json);
// Save SMS record to a text file.
$fh = fopen('smsified.txt', 'a');
fwrite($fh, sprintf($SMS));
fclose($fh);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment