コード例 #1
0
 public function handle($headers, $body, $outputClass = null)
 {
     if ($outputClass !== null) {
         $this->getUnmarshaller()->setOutputClass($outputClass);
     }
     Logger::info('Handling callback');
     Logger::trace('Headers: {0}', $headers);
     Logger::trace('Body: {0}', $body);
     if (!$this->isSignatureCheckDisabled()) {
         $securityHelper = new SecurityHelper($this->apiKey, $this->signatureKey, $this->partnerApiKey, $this->partnerSignatureKey);
         $signature = $securityHelper->getSignature($headers);
         //var_dump($headers);
         $securityHelper->checkSignature($signature, $body);
     }
     return $this->getUnmarshaller()->unmarshall($body);
 }
コード例 #2
0
 public function call()
 {
     $queryString = !empty($this->getQuery()) ? http_build_query($this->getQuery()) : null;
     $targetUrl = $this->url . $this->getMethodPath() . ($queryString !== null ? '?' . $queryString : '');
     Logger::trace('Call target URL: ' . $targetUrl);
     $bodyContent = $this->getBody() !== null ? $this->getRequestMarshaller()->marshall($this->getBody()) : null;
     Logger::trace('Call body: ' . $bodyContent);
     $securityHelper = new SecurityHelper($this->apiKey, $this->signatureKey, $this->partnerApiKey, $this->partnerSignatureKey);
     $signature = $securityHelper->calculateSignature($queryString, $bodyContent);
     Logger::trace('Signature: ' . $signature);
     $headers = $this->getHeaders($signature);
     Logger::trace('Headers: {0}', $headers);
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $targetUrl);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_HEADER, true);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     if ($bodyContent !== null) {
         curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyContent);
     }
     switch ($this->getHttpMethod()) {
         case self::HTTP_GET:
             curl_setopt($ch, CURLOPT_HTTPGET, true);
             break;
         case self::HTTP_POST:
             curl_setopt($ch, CURLOPT_POST, true);
             break;
         case self::HTTP_PUT:
             curl_setopt($ch, CURLOPT_PUT, true);
             break;
         case self::HTTP_DELETE:
             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
             break;
         default:
             break;
     }
     Logger::trace("Calling {0} with http method {1}, body content: {2}", $targetUrl, $this->getHttpMethod(), $bodyContent);
     $response = curl_exec($ch);
     if ($response === false) {
         throw new Exception('Connection error: ' . curl_error($ch));
     }
     $responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     $responseHeaderSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
     curl_close($ch);
     Logger::trace("Response status: {0}, header size: {1}", $responseStatus, $responseHeaderSize);
     $responseHeader = trim(substr($response, 0, $responseHeaderSize));
     $responseBody = trim(substr($response, $responseHeaderSize));
     Logger::trace("Response header: {0}", $responseHeader);
     Logger::trace("Response body: {0}", $responseBody);
     if ($responseStatus != '200') {
         $this->setIsResponseSignatureCheckDisabled(true);
         $this->getResponseUnmarshaller()->setOutputClass(new ApiOperationException());
         $this->getResponseUnmarshaller()->setOutputClassResolveFunction(array('Unmarshaller', 'ResolveExceptionClass'));
         $this->getResponseUnmarshaller()->setIsOutputAnArray(false);
         $this->getResponseUnmarshaller()->setPropertyClassResolveFunctions(array());
     }
     if (!$this->isResponseSignatureCheckDisabled()) {
         $responseSignature = $securityHelper->getSignature($responseHeader);
         $securityHelper->checkSignature($responseSignature, $responseBody);
     }
     $output = $this->getResponseUnmarshaller()->unmarshall($responseBody);
     if ($output instanceof Exception) {
         throw $output;
     }
     return $output;
 }