Bin will be removed on June 1, 2014 in favor of http://gist.github.com. Please save all your pastes before then. So long, and thanks for all the fish!×
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php  
/**
 * Twilio DataSource
 *
 * Used for reading and writing to Twilio, through models.
 *
 * PHP Version 5.x
 *
 *
 * @filesource
 * @copyright     Copyright 2010, Life Is Content
 * @link          http://lifeiscontent.net Life Is Content
 */
App::import('Core', 'HttpSocket');
class TwilioSource extends DataSource {
    private $ApiVersion = "2008-08-01";
    protected $_schema = array(
        'texts' => array(
            'From' => array(
                'type' => 'integer',
                'null' => true,
                'key' => 'primary',
                'length' => 10,
            ),
            'To' => array(
                'type' => 'integer',
                'null' => true,
                'key' => 'primary',
                'length' => 10
            ),
            'Body' => array(
                'type' => 'string',
                'null' => true,
                'key' => 'primary',
                'length' => 160
            ),
        )
    );
    public function __construct($config) {
        $auth = "{$config['AccountSid']}:{$config['AuthToken']}";
        $this->connection = new HttpSocket(
            "https://{$auth}@api.twilio.com/"
        );
        parent::__construct($config);
    }
    public function listSources() {
        return array('texts');
    }
    public function read($model, $queryData = array()) {
        $url = "/".$this->ApiVersion."/Accounts/";
        $url .= "{$this->config['AccountSid']}/SMS/Messages.json";
        $response = json_decode($this->connection->get($url), true);
        $results = array();
        foreach ($response['TwilioResponse']['SMSMessages'] as $record) {
            $record = array('Text' => $record['SMSMessage']);
            $results[] = $record;
        }
        return $results;
    }
    public function create($model, $fields = array(), $values = array()) {
        $data = array_combine($fields, $values);
        $result = $this->connection->post("/".$this->ApiVersion."/Accounts/{$this->config['AccountSid']}/SMS/Messages.json", $data);
        $result = json_decode($result, true);
        debug($result);
        if (isset($result['Sid'])) {
            $model->setInsertId($result['Sid']);
            return true;
        }
        return false;
    }
    public function describe($model) {
        return $this->_schema['texts'];
    }
}
?>