1: <?php
2: 3: 4: 5:
6: class SurveyMonkey{
7: 8: 9: 10:
11: protected $_apiKey;
12:
13: 14: 15: 16:
17: protected $_accessToken;
18:
19: 20: 21: 22:
23: protected $_protocol;
24:
25: 26: 27: 28:
29: protected $_hostname;
30:
31: 32: 33: 34:
35: protected $_version;
36:
37: 38: 39: 40:
41: private $conn = null;
42:
43: 44: 45: 46:
47: protected $_connectionOptions;
48:
49: 50: 51:
52: const SM_STATUS_SUCCESS = 0;
53:
54: public static function successfulHttpResponse($code){
55: if ($code >= 200 and $code < 300){
56: return true;
57: }
58: return false;
59: }
60: 61: 62:
63: public static $SM_STATUS_CODES = array(
64: 0 => "Success",
65: 1 => "Not Authenticated",
66: 2 => "Invalid User Credentials",
67: 3 => "Invalid Request",
68: 4 => "Unknown User",
69: 5 => "System Error",
70: 6 => "Plan Limit Exceeded"
71: );
72:
73: 74: 75: 76: 77:
78: public static function explainStatusCode($code){
79: return self::$SM_STATUS_CODES[$code];
80: }
81:
82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94:
95: public function __construct($apiKey, $accessToken, $options = array(), $connectionOptions = array()){
96:
97: if (empty($apiKey)) throw new SurveyMonkey_Exception('Missing apiKey');
98: if (empty($accessToken)) throw new SurveyMonkey_Exception('Missing accessToken');
99: $this->_apiKey = $apiKey;
100: $this->_accessToken = $accessToken;
101:
102: $this->_protocol = (!empty($options['protocol']))? $options['protocol'] : 'https';
103: $this->_hostname = (!empty($options['hostname']))? $options['hostname'] : 'api.surveymonkey.net';
104: $this->_version = (!empty($options['version']))? $options['version'] : 'v2';
105:
106: $this->_connectionOptions = $connectionOptions;
107: }
108:
109: 110: 111: 112: 113:
114: protected function buildUri($endpoint){
115: return $this->_protocol . '://' . $this->_hostname . '/' . $this->_version . '/' . $endpoint . '?api_key=' . $this->_apiKey;
116: }
117:
118: 119: 120: 121:
122: protected function getConnection(){
123: $this->conn = curl_init();
124: return is_resource($this->conn);
125: }
126:
127: 128: 129:
130: protected function closeConnection(){
131: curl_close($this->conn);
132: }
133:
134: 135: 136: 137: 138: 139:
140: protected function run($endpoint, $params = array()){
141: if (!is_resource($this->conn)) {
142: if (!$this->getConnection()) return $this->failure('Can not initialize connection');
143: }
144:
145: $request_url = $this->buildUri($endpoint);
146: curl_setopt($this->conn, CURLOPT_URL, $request_url);
147: curl_setopt($this->conn, CURLOPT_RETURNTRANSFER, 1 );
148: $headers = array('Content-type: application/json', 'Authorization: Bearer ' . $this->_accessToken);
149: curl_setopt($this->conn, CURLOPT_HTTPHEADER, $headers );
150: curl_setopt($this->conn, CURLOPT_HEADER, false );
151: curl_setopt($this->conn, CURLOPT_POST, true);
152: $postBody = (!empty($params))? json_encode($params) : "{}";
153: curl_setopt($this->conn, CURLOPT_POSTFIELDS, $postBody);
154: curl_setopt_array($this->conn, $this->_connectionOptions);
155:
156: $result = curl_exec( $this->conn );
157: if ($result === false) return $this->failure('Curl Error: ' . curl_error($this->conn));
158: $responseCode = curl_getinfo($this->conn, CURLINFO_HTTP_CODE);
159: if (!self::successfulHttpResponse($responseCode)){
160: return $this->failure('Error ['.$responseCode.']: ' . $result);
161: }
162:
163: $this->closeConnection();
164:
165: $parsedResult = json_decode($result,true);
166: $jsonErr = json_last_error();
167: if ($parsedResult === null && $jsonErr !== JSON_ERROR_NONE) return $this->failure("Error [$jsonErr] parsing result JSON");
168:
169: $status = $parsedResult['status'];
170: if ($status != self::SM_STATUS_SUCCESS) return $this->failure("API Error: Status [$status:" . self::explainStatusCode($status) . ']. Message [' . $parsedResult["errmsg"] . ']');
171: else return $this->success($parsedResult["data"]);
172: }
173:
174:
175:
176: 177: 178: 179: 180:
181: protected function failure($msg){
182: return array(
183: 'success' => false,
184: 'message' => $msg
185: );
186: }
187:
188: 189: 190: 191: 192:
193: protected function success($data){
194: return array(
195: 'success' => true,
196: 'data' => $data
197: );
198: }
199:
200:
201: 202: 203:
204:
205:
206:
207: 208: 209: 210: 211: 212:
213: public function getSurveyList($params = array()){
214: return $this->run('surveys/get_survey_list', $params);
215: }
216:
217: 218: 219: 220: 221: 222:
223: public function getSurveyDetails($surveyId){
224: $params = array('survey_id'=>$surveyId);
225: return $this->run('surveys/get_survey_details', $params);
226: }
227:
228: 229: 230: 231: 232: 233: 234:
235: public function getCollectorList($surveyId, $params = array()){
236: $params['survey_id'] = $surveyId;
237: return $this->run('surveys/get_collector_list', $params);
238: }
239:
240: 241: 242: 243: 244: 245: 246:
247: public function getRespondentList($surveyId, $params = array()){
248: $params['survey_id'] = $surveyId;
249: return $this->run('surveys/get_respondent_list', $params);
250: }
251:
252: 253: 254: 255: 256: 257: 258: 259:
260: public function getResponses($surveyId, $respondentIds, $chunkSize = 100){
261:
262: if (count($respondentIds) > $chunkSize){
263: $data = array();
264: foreach (array_chunk($respondentIds, $chunkSize) as $r){
265: $result = $this->getResponses($surveyId, $r, $chunkSize);
266: if (!$result["success"]) return $result;
267: $data = array_merge($data, $result["data"]);
268: }
269: return $this->success($data);
270: }
271:
272: $params = array(
273: 'survey_id' => $surveyId,
274: 'respondent_ids' => $respondentIds
275: );
276: return $this->run('surveys/get_responses', $params);
277: }
278:
279: 280: 281: 282: 283: 284:
285: public function getResponseCounts($collectorId){
286: $params = array('collector_id' => $collectorId);
287: return $this->run('surveys/get_response_counts', $params);
288: }
289:
290:
291:
292: 293: 294: 295: 296:
297: public function getUserDetails(){
298: return $this->run('user/get_user_details');
299: }
300:
301:
302:
303: 304: 305: 306: 307: 308:
309: public function getTemplateList($params = array()){
310: return $this->run('templates/get_template_list', $params);
311: }
312:
313:
314:
315: 316: 317: 318: 319: 320: 321: 322: 323:
324: public function createCollector($surveyId, $collectorName = null, $collectorType = 'weblink'){
325: $params = array(
326: 'survey_id'=>$surveyId,
327: 'collector'=>array(
328: 'type'=>$collectorType,
329: 'name'=>$collectorName
330: )
331: );
332: return $this->run('collectors/create_collector', $params);
333: }
334:
335:
336:
337: 338: 339: 340: 341: 342: 343:
344: public function createFlow($surveyTitle, $params = array()){
345: if (isset($params['survey'])){
346: $params['survey']['survey_title'] = $surveyTitle;
347: }
348: else{
349: $params['survey'] = array('survey_title'=>$surveyTitle);
350: }
351: return $this->run('batch/create_flow', $params);
352: }
353:
354: 355: 356: 357: 358: 359: 360:
361: public function sendFlow($surveyId, $params = array()){
362: $params['survey_id'] = $surveyId;
363: return $this->run('batch/send_flow', $params);
364: }
365: }
366:
367: 368: 369: 370: 371:
372: class SurveyMonkey_Exception extends Exception {}