/** * Send request to UPS * * @param string $access The access request xml * @param string $request The request xml * @param string $endpointurl The UPS API Endpoint URL * @return ResponseInterface * @throws Exception * todo: make access, request and endpointurl nullable to make the testable */ public function request($access, $request, $endpointurl) { $this->setAccess($access); $this->setRequest($request); $this->setEndpointUrl($endpointurl); // Create POST request $form = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $this->getAccess() . $this->getRequest())); $request = stream_context_create($form); if (!($handle = fopen($this->getEndpointUrl(), 'rb', false, $request))) { throw new Exception("Failure: Connection to Endpoint URL failed."); } $response = stream_get_contents($handle); fclose($handle); if ($response != false) { $text = $response; $response = new SimpleXMLElement($response); if (isset($response->Response) && isset($response->Response->ResponseStatusCode)) { $responseInstance = new Response(); return $responseInstance->setText($text)->setResponse($response); } } throw new Exception("Failure: Response is invalid."); }
/** * Send request to UPS. * * @param string $access The access request xml * @param string $request The request xml * @param string $endpointurl The UPS API Endpoint URL * * @throws Exception * todo: make access, request and endpointurl nullable to make the testable * * @return ResponseInterface */ public function request($access, $request, $endpointurl) { $this->setAccess($access); $this->setRequest($request); $this->setEndpointUrl($endpointurl); // Log request $date = new DateTime(); $id = $date->format('YmdHisu'); $this->logger->info('Request To UPS API', ['id' => $id, 'endpointurl' => $this->getEndpointUrl()]); $this->logger->debug('Request: ' . $this->getRequest(), ['id' => $id, 'endpointurl' => $this->getEndpointUrl()]); try { $client = new Guzzle(); $response = $client->post($this->getEndpointUrl(), ['body' => $this->getAccess() . $this->getRequest(), 'headers' => ['Content-type' => 'application/x-www-form-urlencoded; charset=utf-8', 'Accept-Charset' => 'UTF-8'], 'http_errors' => true]); $body = (string) $response->getBody(); $this->logger->info('Response from UPS API', ['id' => $id, 'endpointurl' => $this->getEndpointUrl()]); $this->logger->debug('Response: ' . $body, ['id' => $id, 'endpointurl' => $this->getEndpointUrl()]); if ($response->getStatusCode() === 200) { if (function_exists('mb_convert_encoding')) { $body = mb_convert_encoding($body, 'UTF-8', mb_detect_encoding($body)); } $xml = new SimpleXMLElement($body); if (isset($xml->Response) && isset($xml->Response->ResponseStatusCode)) { if ($xml->Response->ResponseStatusCode == 1) { $responseInstance = new Response(); return $responseInstance->setText($body)->setResponse($xml); } elseif ($xml->Response->ResponseStatusCode == 0) { throw new InvalidResponseException('Failure: ' . $xml->Response->Error->ErrorDescription . ' (' . $xml->Response->Error->ErrorCode . ')'); } } else { throw new InvalidResponseException('Failure: response is in an unexpected format.'); } } } catch (\GuzzleHttp\Exception\TransferException $e) { // Guzzle: All of the exceptions extend from GuzzleHttp\Exception\TransferException $this->logger->alert($e->getMessage(), ['id' => $id, 'endpointurl' => $this->getEndpointUrl()]); throw new RequestException('Failure: ' . $e->getMessage()); } }
/** * Send request to UPS. * * @param string $access The access request xml * @param string $request The request xml * @param string $endpointurl The UPS API Endpoint URL * @param string $operation Operation to perform on SOAP endpoint * @param string $wsdl Which WSDL file to use * * @throws Exception * @todo: make access, request and endpointurl nullable to make the testable * * @return ResponseInterface */ public function request($access, $request, $endpointurl, $operation = null, $wsdl = null) { // Check for operation if (is_null($operation)) { throw new \Exception('Operation is required'); } // Check for WSDL if (is_null($wsdl)) { throw new \Exception('WSDL is required'); } // Set data $this->setAccess($access); $this->setRequest($request); $this->setEndpointUrl($endpointurl); // Settings based on UPS PHP Example $mode = array('soap_version' => 'SOAP_1_1', 'trace' => 1, 'connection_timeout' => 2, 'cache_wsdl' => WSDL_CACHE_BOTH); // Initialize soap client $client = new SoapClient(__DIR__ . '/WSDL/' . $wsdl . '.wsdl', $mode); // Set endpoint URL + auth & request data $client->__setLocation($endpointurl); $auth = (array) new SimpleXMLElement($access); $request = (array) new SimpleXMLElement($request); // Build auth header $header = new \SoapHeader('http://www.ups.com/schema/xpci/1.0/auth', 'AccessRequest', $auth); $client->__setSoapHeaders($header); // Log request $date = new DateTime(); $id = $date->format('YmdHisu'); $this->logger->info('Request To UPS API', ['id' => $id, 'endpointurl' => $this->getEndpointUrl()]); $this->logger->debug('Request: ' . $this->getRequest(), ['id' => $id, 'endpointurl' => $this->getEndpointUrl()]); // Perform call and get response try { $request = json_decode(json_encode((array) $request), true); $client->__soapCall($operation, [$request]); $body = $client->__getLastResponse(); $this->logger->info('Response from UPS API', ['id' => $id, 'endpointurl' => $this->getEndpointUrl()]); $this->logger->debug('Response: ' . $body, ['id' => $id, 'endpointurl' => $this->getEndpointUrl()]); // Strip off namespaces and make XML $body = preg_replace('/(<\\/*)[^>:]+:/', '$1', $body); $xml = new SimpleXMLElement($body); $responseInstance = new Response(); return $responseInstance->setText($body)->setResponse($xml); } catch (\Exception $e) { // Parse error response $xml = new SimpleXMLElement($client->__getLastResponse()); $xml->registerXPathNamespace('err', 'http://www.ups.com/schema/xpci/1.0/error'); $errorCode = $xml->xpath('//err:PrimaryErrorCode/err:Code'); $errorMsg = $xml->xpath('//err:PrimaryErrorCode/err:Description'); if (isset($errorCode[0]) && isset($errorMsg[0])) { $this->logger->alert($errorMsg[0], ['id' => $id, 'endpointurl' => $this->getEndpointUrl()]); throw new InvalidResponseException('Failure: ' . (string) $errorMsg[0] . ' (' . (string) $errorCode[0] . ')'); } else { $this->logger->alert($e->getMessage(), ['id' => $id, 'endpointurl' => $this->getEndpointUrl()]); throw new InvalidResponseException('Cannot parse error from UPS: ' . $e->getMessage(), $e->getCode(), $e); } } }
/** * Send request to UPS * * @param string $access The access request xml * @param string $request The request xml * @param string $endpointurl The UPS API Endpoint URL * @return ResponseInterface * @throws Exception * todo: make access, request and endpointurl nullable to make the testable */ public function request($access, $request, $endpointurl) { $this->setAccess($access); $this->setRequest($request); $this->setEndpointUrl($endpointurl); // Log request if ($this->logger) { $date = new \DateTime(); $id = $date->format('YmdHisu'); $this->logger->info('Request To UPS API', array('id' => $id, 'endpointurl' => $this->getEndpointUrl())); $this->logger->debug('Request: ' . $this->getRequest(), array('id' => $id, 'endpointurl' => $this->getEndpointUrl())); } // Create POST request $form = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $this->getAccess() . $this->getRequest())); $request = stream_context_create($form); if (!($handle = fopen($this->getEndpointUrl(), 'rb', false, $request))) { if ($this->logger) { $this->logger->alert('Connection to UPS API failed', array('id' => $id, 'endpointurl' => $this->getEndpointUrl())); } throw new Exception("Failure: Connection to Endpoint URL failed."); } $response = stream_get_contents($handle); fclose($handle); if ($this->logger) { $this->logger->info('Response from UPS API', array('id' => $id, 'endpointurl' => $this->getEndpointUrl())); $this->logger->debug('Response: ' . $response, array('id' => $id, 'endpointurl' => $this->getEndpointUrl())); } if ($response != false) { $text = $response; $response = new SimpleXMLElement($response); if (isset($response->Response) && isset($response->Response->ResponseStatusCode)) { $responseInstance = new Response(); return $responseInstance->setText($text)->setResponse($response); } } if ($this->logger) { $this->logger->critical('UPS Response is invalid', array('id' => $id)); } throw new Exception("Failure: Response is invalid."); }