/**
  * Constructor
  *
  * @param signature (string) Expected response signature
  * @param headers (string) HTTP headers of the response
  * @param body (string) Raw data of the response
  */
 public function __construct($signature, $headers, $body)
 {
     parent::__construct($signature, $headers, $body);
     $this->_xml = AllopassApiTools::xmlParseString($this->_body);
     $this->_verify();
 }
Ejemplo n.º 2
0
 /**
  * Internal method performing a raw request using socket
  *
  * @return (string[]) A 0-indexed array which contains response headers and body
  *
  * @throws AllopassApiUnavailableRessourceException If raw call to remote API fails
  */
 protected function _rawCall()
 {
     $host = AllopassApiConf::getInstance()->getHost();
     if (($socket = @fsockopen($host, AllopassApiConf::getInstance()->getNetworkPort(), $socket_errno, $socket_errstr, AllopassApiConf::getInstance()->getNetworkTimeout())) === FALSE) {
         throw new AllopassApiUnavailableRessourceException();
     }
     $path = AllopassApiConf::getInstance()->getHost() . self::API_PATH . $this->_getPath();
     if ($this->_isHttpPost()) {
         $method = 'POST';
         $data = http_build_query($this->_parameters);
         $length = strlen($data);
     } else {
         $method = 'GET';
         $data = '';
         $length = 0;
         $path .= self::HTTP_QUERY_CONNECTOR . http_build_query($this->_parameters);
     }
     $headers = [];
     $headers[] = 'Host: ' . $host;
     $headers[] = 'Content-Type: application/x-www-form-urlencoded';
     $headers[] = 'User-Agent: ' . self::HTTP_USER_AGENT;
     $headers[] = 'Content-Length: ' . $length;
     $headers[] = 'Connection: close';
     $request = $method . ' ' . AllopassApiConf::getInstance()->getNetworkProtocol() . self::HTTP_CONNECTOR;
     $request .= $path . ' HTTP/' . self::HTTP_VERSION . self::HTTP_CRLF;
     $request .= implode(self::HTTP_CRLF, $headers) . self::HTTP_CRLF;
     $request .= self::HTTP_CRLF . $data;
     if (fwrite($socket, $request, strlen($request)) === FALSE) {
         throw new AllopassApiUnavailableRessourceException();
     }
     $content = '';
     while (!feof($socket)) {
         if (($line = fread($socket, self::HTTP_CHUNK_SIZE)) === FALSE) {
             throw new AllopassApiUnavailableRessourceException();
         }
         $content .= $line;
     }
     fclose($socket);
     $pos = strpos($content, self::HTTP_CRLF . self::HTTP_CRLF);
     if ($pos === FALSE) {
         throw new AllopassApiUnavailableRessourceException();
     }
     $headers = substr($content, 0, $pos);
     $body = substr($content, $pos + self::HTTP_HEADER_SEPARATOR_SIZE);
     $aHeaders = explode(self::HTTP_CRLF, $headers);
     foreach ($aHeaders as &$header) {
         if (AllopassApiTools::beginsWith($header, 'Transfer-Encoding') && AllopassApiTools::endsWith($header, 'chunked')) {
             $body = $this->_decodeChunkedBody($body);
             continue;
         }
         if (AllopassApiTools::beginsWith($header, 'Content-Encoding')) {
             if (AllopassApiTools::endsWith($header, 'gzip')) {
                 $body = $this->_ungzipBody($body);
             } elseif (AllopassApiTools::endsWith($header, 'deflate')) {
                 $body = $this->_inflateBody($body);
             }
         }
     }
     return [$headers, $body];
 }