Exemple #1
0
 public function __construct(\Zend_Http_Response $httpResponse)
 {
     $this->_httpResponse = $httpResponse;
     /*
     if(!$httpResponse->isError() && !preg_match('/^\s*$/', $httpResponse->getBody()))
     {
         $this->_data = simplexml_load_string($httpResponse->getBody());
     }
     */
     if (!preg_match('/^\\s*$/', $httpResponse->getBody())) {
         #print $httpResponse->getBody(); exit;
         $this->_data = simplexml_load_string($httpResponse->getBody());
         if ($httpResponse->isError()) {
             $data = (array) $this->_data;
             if (isset($data['error'])) {
                 $this->_error = $data['error'];
             }
         }
     }
 }
Exemple #2
0
 /**
  * Checks ReST response for errors.
  *
  * @param   Zend_Http_Response $response    the ReST response
  * @return  void
  * @throws  Zend_Service_Technorati_Exception
  * @access  protected
  */
 protected static function _checkResponse(Zend_Http_Response $response)
 {
     if ($response->isError()) {
         /**
          * @see Zend_Service_Technorati_Exception
          */
         // require_once 'Zend/Service/Technorati/Exception.php';
         throw new Zend_Service_Technorati_Exception(sprintf('Invalid response status code (HTTP/%s %s %s)', $response->getVersion(), $response->getStatus(), $response->getMessage()));
     }
 }
Exemple #3
0
 /**
  * Search for error from request.
  *
  * If any error is found a DOMDocument is returned, this object contains a
  * DOMXPath object as "ebayFindingXPath" attribute.
  *
  * @param  Zend_Http_Response $response
  * @link   http://developer.ebay.com/DevZone/finding/CallRef/types/ErrorSeverity.html
  * @see    Zend_Service_Ebay_Finding_Abstract::_initXPath()
  * @throws Zend_Service_Ebay_Finding_Exception When any error occurrs during request
  * @return DOMDocument
  */
 protected function _parseResponse(Zend_Http_Response $response)
 {
     // error message
     $message = '';
     // first trying, loading XML
     $dom = new DOMDocument();
     if (!($dom = @Zend_Xml_Security::scan($response->getBody(), $dom))) {
         $message = 'It was not possible to load XML returned.';
     }
     // second trying, check request status
     if ($response->isError()) {
         $message = $response->getMessage() . ' (HTTP status code #' . $response->getStatus() . ')';
     }
     // third trying, search for error message into XML response
     // only first error that contains severiry=Error is read
     $xpath = new DOMXPath($dom);
     foreach (self::$_xmlNamespaces as $alias => $uri) {
         $xpath->registerNamespace($alias, $uri);
     }
     $ns = self::XMLNS_FINDING;
     $nsMs = self::XMLNS_MS;
     $expression = "//{$nsMs}:errorMessage[1]/{$ns}:error/{$ns}:severity[.='Error']";
     $severityNode = $xpath->query($expression)->item(0);
     if ($severityNode) {
         $errorNode = $severityNode->parentNode;
         // ebay message
         $messageNode = $xpath->query("//{$ns}:message[1]", $errorNode)->item(0);
         if ($messageNode) {
             $message = 'eBay error: ' . $messageNode->nodeValue;
         } else {
             $message = 'eBay error: unknown';
         }
         // ebay error id
         $errorIdNode = $xpath->query("//{$ns}:errorId[1]", $errorNode)->item(0);
         if ($errorIdNode) {
             $message .= ' (#' . $errorIdNode->nodeValue . ')';
         }
     }
     // throw exception when an error was detected
     if (strlen($message) > 0) {
         /**
          * @see Zend_Service_Ebay_Finding_Exception
          */
         require_once LIB_DIR . '/Zend/Service/Ebay/Finding/Exception.php';
         throw new Zend_Service_Ebay_Finding_Exception($message);
     }
     // add xpath to dom document
     // it allows service_ebay_finding classes use this
     $dom->ebayFindingXPath = $xpath;
     return $dom;
 }
Exemple #4
0
 /**
  * Returns data of the call if there was no error
  *
  * @throws Robo47_Service_Bitly_Exception
  * @param Zend_Http_Response $response
  * @return string|array|object
  */
 protected function _getData(Zend_Http_Response $response)
 {
     if ($response->isError()) {
         $message = 'Error on api-call: ' . $response->getMessage();
         throw new Robo47_Service_Bitly_Exception($message);
     }
     // find errors without parsing xml / json
     // @todo parse formats always and move tests into the other switch
     //       and refactor a bit, method gets big and ugly
     switch ($this->_format) {
         case self::FORMAT_XML:
             $regex = preg_quote('<errorCode>0</errorCode>', '/');
             break;
         case self::FORMAT_JSON:
             $regex = preg_quote('"errorCode": 0', '/');
             break;
     }
     if (!preg_match('/' . $regex . '/i', $response->getBody())) {
         $message = 'Error on api-call: no errorCode=0 found';
         throw new Robo47_Service_Bitly_Exception($message);
     }
     // @todo checking for errors ALWAYS!
     switch ($this->_resultFormat) {
         case self::FORMAT_RESULT_NATIVE:
             $result = $response->getBody();
             break;
         case self::FORMAT_RESULT_ARRAY:
             switch ($this->_format) {
                 case self::FORMAT_XML:
                     $result = $this->xmlToArray($response->getBody());
                     break;
                 case self::FORMAT_JSON:
                     $result = json_decode($response->getBody(), true);
                     break;
             }
             break;
         case self::FORMAT_RESULT_OBJECT:
             switch ($this->_format) {
                 case self::FORMAT_XML:
                     $result = $this->xmlToObject($response->getBody());
                     break;
                 case self::FORMAT_JSON:
                     $result = json_decode($response->getBody(), false);
                     break;
             }
             break;
     }
     return $result;
 }
Exemple #5
0
 /**
  * Prepare a solarium response from the given request and client
  * response
  *
  * @throws HttpException
  * @param  Request             $request
  * @param  \Zend_Http_Response $response
  * @return Response
  */
 protected function prepareResponse($request, $response)
 {
     if ($response->isError()) {
         throw new HttpException($response->getMessage(), $response->getStatus());
     }
     if ($request->getMethod() == Request::METHOD_HEAD) {
         $data = '';
     } else {
         $data = $response->getBody();
     }
     // this is used because getHeaders doesn't return the HTTP header...
     $headers = explode("\n", $response->getHeadersAsString());
     return new Response($data, $headers);
 }
Exemple #6
0
 /**
  * Checks ReST response for errors.
  *
  * @param   Zend_Http_Response $response    the ReST response
  * @return  void
  * @throws  Zend_Service_Technorati_Exception
  * @access  protected
  */
 protected static function _checkResponse(Zend_Http_Response $response)
 {
     if ($response->isError()) {
         throw new Zend_Service_Technorati_Exception(sprintf('Invalid response status code (HTTP/%s %s %s)', $response->getVersion(), $response->getStatus(), $response->getMessage()));
     }
 }
Exemple #7
0
 /**
  * Search for error from request.
  *
  * If any error is found a DOMDocument is returned, this object contains a
  * DOMXPath object as "ebayFindingXPath" attribute.
  *
  * @param  Zend_Http_Response $response
  * @throws Zend_Service_Ebay_Trading_Exception When any error occurrs during request
  * @return SimpleXMLElement
  */
 protected function _parseResponse(Zend_Http_Response $response)
 {
     // error message
     $message = '';
     // first trying, loading XML
     $xml = simplexml_load_string($response->getBody());
     if (!$xml) {
         $message = 'It was not possible to load XML returned.';
     }
     if ($xml->Ack != 'Success') {
         $message = 'Ack not success.';
     }
     // second trying, check request status
     if ($response->isError()) {
         $message = $response->getMessage() . ' (HTTP status code #' . $response->getStatus() . ')';
     }
     // throw exception when an error was detected
     if (strlen($message) > 0) {
         /**
          * @see Zend_Service_Ebay_Finding_Exception
          */
         require_once 'Zend/Service/Ebay/Trading/Exception.php';
         throw new Zend_Service_Ebay_Trading_Exception($message);
     }
     return $xml;
 }
 /**
  * Parse response object and check for errors
  *
  * @param \Zend_Http_Response $response
  * @return stdClass
  */
 protected function _parseResponse(\Zend_Http_Response $response)
 {
     if ($response->isError()) {
         switch ($response->getStatus()) {
             case 401:
                 throw new Exception('Postmark request error: Unauthorized - Missing or incorrect API Key header.');
                 break;
             case 422:
                 $error = \Zend_Json::decode($response->getBody());
                 if (is_object($error)) {
                     throw new Exception(sprintf('Postmark request error: Unprocessable Entity - API error code %s, message: %s', $error->ErrorCode, $error->Message));
                 } else {
                     throw new Exception(sprintf('Postmark request error: Unprocessable Entity - API error code %s, message: %s', $error['ErrorCode'], $error['Message']));
                 }
                 break;
             case 500:
                 throw new Exception('Postmark request error: Postmark Internal Server Error');
                 break;
             default:
                 throw new Exception('Unknown error during request to Postmark server');
         }
     }
     return \Zend_Json::decode($response->getBody());
 }
Exemple #9
0
 /**
  * Initializes response object.
  *
  * @param   Zend_Http_Response  $response
  *
  * @return  Jirafe_HttpConnection_Response
  */
 protected function initializeResponse(Zend_Http_Response $response)
 {
     return new Jirafe_HttpConnection_Response($response->getBody(), $response->getHeaders(), $response->isError() ? $response->getStatus() : 0, $response->getMessage());
 }
Exemple #10
0
 /**
  * Search for error from request.
  *
  * If any error is found a DOMDocument is returned, this object contains a
  * DOMXPath object as "ebayFindingXPath" attribute.
  *
  * @param  Zend_Http_Response $response
  * @throws Zend_Service_Ebay_Trading_Exception When any error occurrs during request
  * @return SimpleXMLElement
  */
 protected function _parseResponse(Zend_Http_Response $response)
 {
     // error message
     $message = '';
     $xml = simplexml_load_string($response->getBody());
     if (!$xml) {
         $message = 'It was not possible to load XML returned.';
     }
     if ($xml->Ack != 'Success' && $xml->Ack != 'Warning') {
         $message = "Ack not success: " . print_r($xml, true);
     }
     //var_dump($dom->Ack);die;
     // second trying, check request status
     if ($response->isError()) {
         $message = $response->getMessage() . ' (HTTP status code #' . $response->getStatus() . ')';
     }
     // throw exception when an error was detected
     if (strlen($message) > 0) {
         /**
          * @see Zend_Service_Ebay_Finding_Exception
          */
         require_once 'Zend/Service/Ebay/Shopping/Exception.php';
         throw new Zend_Service_Ebay_Shopping_Exception($message);
     }
     /*
     $dom = new DOMDocument();
     $dom->loadXML($response->getBody());
     Zend_Debug::dump($dom->saveXML());
     
     Zend_Debug::dump($xml);
     */
     return $xml;
 }