コード例 #1
0
ファイル: Request.php プロジェクト: bkuhl/simple-ups
 /**
  * Perform the request and get the response XML for a given request
  * @return Response|\SimpleXMLElement Returns SimpleXMLElement when response class is omitted
  * @throws \SimpleUPS\Api\InvalidResponseException
  * @throws \SimpleUPS\Api\RequestFailedException
  * @throws \SimpleUPS\Api\ResponseErrorException
  * @throws \SimpleUPS\Api\MissingParameterException
  */
 public function sendRequest()
 {
     try {
         $xml = $this->buildXml();
     } catch (\SimpleUPS\Api\MissingParameterException $e) {
         throw new \SimpleUPS\Api\MissingParameterException($e->getMessage());
     }
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $this->getUrl());
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
     $result = curl_exec($ch);
     if (curl_errno($ch)) {
         throw new \SimpleUPS\Api\RequestFailedException('#' . curl_errno($ch) . ': ' . curl_error($ch));
     } else {
         $returnCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
         if ($returnCode != 200) {
             throw new \SimpleUPS\Api\RequestFailedException('Request returned header: ' . $returnCode);
         }
     }
     curl_close($ch);
     if ($this->getDebug()) {
         UPS::$request = $this;
         $dom = new \DOMDocument();
         $dom->preserveWhiteSpace = false;
         $dom->formatOutput = true;
         //if xml fails to load, still record the response even if it's not formatted
         if ($dom->loadXML($result)) {
             UPS::$response = $dom->saveXML();
         } else {
             UPS::$response = $result;
         }
     }
     try {
         $xml = new \SimpleXMLElement($result);
         if ($xml === false) {
             throw new \Exception();
         }
     } catch (\Exception $e) {
         throw new \SimpleUPS\Api\InvalidResponseException('Unable to parse XML response');
     }
     $this->handleResponseErrors($xml);
     if ($this->responseClass === false) {
         return $xml;
     }
     $response = new $this->responseClass();
     $response->fromXml($xml);
     return $response;
 }