Created
May 16, 2011 14:12
-
-
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.
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 | |
/* | |
* 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