/
appcelerator.php
132 lines (110 loc) · 3.18 KB
/
appcelerator.php
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* Laravel Appcelerator Bundle
*
* @category Bundle
* @package Laravel
* @author Ben Edmunds <http://benedmunds.com>
* @author Shealan Foreshaw <http://twitter.com/#!/shealan>
* @company Swipe & Tap <http://twitter.com/#!/swipeandtap>
* @copyright 2012 Ben Edmunds
* @license MIT License http://www.opensource.org/licenses/mit-license.php
* @version Release: 1.0
* @link https://github.com/benedmunds/Laravel-Appcelerator
*/
namespace Appcelerator;
class Appcelerator
{
protected $base_url = 'api.cloud.appcelerator.com/v1/';
protected $key = '';
protected $email = '';
protected $password = '';
protected $cookie = '/tmp/appccookie';
protected $_logged_in = FALSE;
protected $_errors = '';
function __construct($key, $email, $password)
{
$this->cookie .= time() . '.txt';
$this->key = $key;
$this->email = $email;
$this->password = $password;
}
static function init($key, $email, $password)
{
return new static($key, $email, $password);
}
function send_request($url, $method, $data, $secure=TRUE)
{
$url = $this->_build_url($url, $secure) . ($method=='GET' ? '&'.http_build_query($data) : '');
if ($this->_logged_in === FALSE)
{
$login = $this->_login($this->email, $this->password);
if ($login == FALSE)
{
return json_decode($this->_errors);
}
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
switch ($method)
{
case 'GET':
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
break;
case 'POST':
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
case 'PUT':
curl_setopt($ch, CURLOPT_PUT, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
case 'DELETE':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
}
$output = curl_exec($ch);
if ($output == FALSE)
{
return curl_error($ch);
}
return json_decode($output);
}
protected function _login($email, $password)
{
$login = array(
'login' => $email,
'password' => $password
);
$ch = curl_init($this->_build_url('users/login.json'));
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $login);
$login = curl_exec($ch);
if ($login == FALSE)
{
$this->_errors = curl_error($ch);
$this->_logged_in = FALSE;
return FALSE;
}
$this->_logged_in = TRUE;
return TRUE;
}
protected function _build_url($url, $secure=TRUE)
{
$final_url = '';
$final_url = ($secure === TRUE) ? 'https://' : 'http://';
$final_url .= $this->base_url;
$final_url .= $url . '?key=' . $this->key;
return $final_url;
}
}