예제 #1
0
파일: sqs.php 프로젝트: 42medien/spreadly
 /**
  * Get the response
  *
  * @return object | false
  */
 public function getResponse()
 {
     if ($this->expires) {
         $this->parameters['Expires'] = gmdate('c');
     } else {
         $this->parameters['Timestamp'] = gmdate('c');
     }
     $params = array();
     foreach ($this->parameters as $var => $value) {
         $params[] = $var . '=' . rawurlencode($value);
     }
     sort($params, SORT_STRING);
     $query = implode('&', $params);
     $strtosign = $this->verb . "\n" . $this->url . "\n/" . $this->queue . "\n" . $query;
     $query .= '&Signature=' . rawurlencode(SQS::__getSignature($strtosign));
     $ssl = SQS::$useSSL && extension_loaded('openssl');
     $url = ($ssl ? 'https://' : 'http://') . $this->url . '/' . $this->queue . '?' . $query;
     // Basic setup
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_USERAGENT, 'SQS/php');
     if (SQS::$useSSL) {
         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, SQS::$verifyHost);
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, SQS::$verifyPeer);
     }
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_HEADER, false);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
     curl_setopt($curl, CURLOPT_WRITEFUNCTION, array(&$this, '__responseWriteCallback'));
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
     // Request types
     switch ($this->verb) {
         case 'GET':
             break;
         case 'PUT':
         case 'POST':
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb);
             $headers[] = 'Content-Type: application/x-www-form-urlencoded';
             break;
         case 'HEAD':
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
             curl_setopt($curl, CURLOPT_NOBODY, true);
             break;
         case 'DELETE':
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
             break;
         default:
             break;
     }
     if (count($headers) > 0) {
         curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     }
     // Execute, grab errors
     if (curl_exec($curl)) {
         $this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     } else {
         $this->response->error = array('curl' => true, 'code' => curl_errno($curl), 'message' => curl_error($curl), 'resource' => $this->resource);
     }
     @curl_close($curl);
     // Parse body into XML
     if ($this->response->error === false && isset($this->response->body)) {
         $this->response->body = simplexml_load_string($this->response->body);
         // Grab SQS errors
         if (!in_array($this->response->code, array(200, 204)) && isset($this->response->body->Error)) {
             $this->response->error = array('curl' => false, 'Type' => (string) $this->response->body->Error->Type, 'Code' => (string) $this->response->body->Error->Code, 'Message' => (string) $this->response->body->Error->Message, 'Detail' => (string) $this->response->body->Error->Detail);
             unset($this->response->body);
         }
     }
     return $this->response;
 }