Exemple #1
0
 /**
  * Prepare cURL with the given request
  * 
  * @param \apiTalk\Request $requst
  * 
  * @return void
  */
 protected function prepareRequest(Request $request)
 {
     $curl = curl_init();
     // TODO Timeout and Follow Redirect settings
     $options = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_CONNECTTIMEOUT_MS => 1000, CURLOPT_TIMEOUT_MS => 3000, CURLOPT_CUSTOMREQUEST => $request->getMethod(), CURLOPT_URL => $request->getUri(), CURLOPT_HTTPHEADER => $request->getHeaders(), CURLOPT_HTTPGET => false, CURLOPT_NOBODY => false, CURLOPT_POST => false, CURLOPT_POSTFIELDS => null);
     switch ($request->getMethod()) {
         case Request::METHOD_HEAD:
             $options[CURLOPT_NOBODY] = true;
             $query = http_build_query($request->getParameters());
             if (!empty($query)) {
                 $options[CURLOPT_URL] = $request->getUri() . '?' . $query;
             }
             break;
         case Request::METHOD_GET:
             $options[CURLOPT_HTTPGET] = true;
             $query = http_build_query($request->getParameters());
             if (!empty($query)) {
                 $options[CURLOPT_URL] = $request->getUri() . '?' . $query;
             }
             break;
         case Request::METHOD_POST:
         case Request::METHOD_PUT:
             $options[CURLOPT_POST] = true;
             $options[CURLOPT_POSTFIELDS] = http_build_query($request->getParameters());
             break;
     }
     curl_setopt_array($curl, $options);
     return $curl;
 }
Exemple #2
0
 /**
  * Signed the request with the HMAC SHA256 algorithm
  * 
  * @param \apiTalk\Request $request A request object
  * @param int              $expires (Optional) The expire timestamp - Default: time() + 5
  * 
  * @return \apiTalk\Request
  */
 protected function signRequest(Request $request, $expires = null)
 {
     // @codeCoverageIgnoreStart
     if (!$expires) {
         $expires = time() + 5;
     }
     // @codeCoverageIgnoreEnd
     $request->setParameter('AWSAccessKeyId', $this->awsAccessKey);
     $request->setParameter('Version', '2011-10-01');
     $request->setParameter('Expires', gmdate('Y-m-d\\TH:i:s\\Z', $expires));
     $request->setParameter('SignatureMethod', 'HmacSHA256');
     $request->setParameter('SignatureVersion', '2');
     if ($request->getMethod() == Request::METHOD_POST) {
         $request->setHeader('Content-Type', 'application/x-www-form-urlencoded');
     }
     // Remove old signature
     $parameters = $request->getParameters();
     if (isset($parameters['Signature'])) {
         unset($parameters['Signature']);
     }
     // Sort params by key
     uksort($parameters, 'strcmp');
     $request->setParameters($parameters);
     // Parse URL
     $url = $request->getUri();
     $path = parse_url($url, PHP_URL_PATH);
     $host = parse_url($url, PHP_URL_HOST);
     // Generate raw request
     $data = strtoupper($request->getMethod()) . "\n";
     $data .= strtolower($host) . "\n";
     $data .= (!empty($path) ? $path : '/') . "\n";
     $data .= http_build_query($request->getParameters());
     // Build the signature
     $hmac = hash_hmac('sha256', $data, $this->awsSecretKey, true);
     $request->setParameter('Signature', base64_encode($hmac));
     return $request;
 }