Exemple #1
0
 private function requestToAS2Request(Request $request)
 {
     $flattenedHeaders = array();
     foreach ($request->headers as $key => $header) {
         $flattenedHeaders[$key] = reset($header);
     }
     return $this->requestFactory->build($request->getContent(), new Header($flattenedHeaders));
 }
Exemple #2
0
 /**
  * Send request to the partner (manage headers, security, ...)
  *
  * @param AbstractBase $request The request to send (instanceof : AS2Message | AS2MDN)
  *
  * @return array
  */
 public function sendRequest($request)
 {
     if (!$request instanceof Message && !$request instanceof MDN) {
         throw new AS2Exception('Unexpected format');
     }
     // format headers
     $headers = $request->getHeaders()->toFormattedArray();
     // initialize variables for building response headers with curl
     $this->response_headers = array();
     $this->response_content = '';
     $this->response_indice = 0;
     // send as2 message with headers
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $request->getUrl());
     curl_setopt($ch, CURLOPT_HEADER, 0);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
     curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
     curl_setopt($ch, CURLOPT_TIMEOUT, 30);
     curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
     curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getContent());
     curl_setopt($ch, CURLOPT_USERAGENT, 'AS2Secure - PHP Lib for AS2 message encoding / decoding');
     curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'handleResponseHeader'));
     // authentication setup
     $auth = $request->getAuthentication();
     if ($auth['method'] != Partner::METHOD_NONE) {
         curl_setopt($ch, CURLOPT_HTTPAUTH, $auth['method']);
         curl_setopt($ch, CURLOPT_USERPWD, urlencode($auth['login']) . ':' . urlencode($auth['password']));
     }
     $response = curl_exec($ch);
     $info = curl_getinfo($ch);
     $error = curl_error($ch);
     curl_close($ch);
     $this->response_content = $response;
     // handle http error response
     if ($info['http_code'] != 200) {
         throw new AS2Exception('HTTP Error Code : ' . $info['http_code'] . '(url:' . $request->getUrl() . ').');
     }
     // handle curl error
     if ($error) {
         throw new AS2Exception($error);
     }
     if ($request instanceof Message && $request->getPartnerTo()->mdn_request == Partner::ACK_SYNC) {
         $temp_response = $this->requestFactory->build($response, new Header($this->response_headers[count($this->response_headers) - 1]));
         $as2_response = $temp_response->getObject();
         $as2_response->decode();
     } else {
         $as2_response = null;
     }
     return array('request' => $request, 'headers' => $this->response_headers[count($this->response_headers) - 1], 'response' => $as2_response, 'info' => $info);
 }