Beispiel #1
0
 public function testAppendBody()
 {
     $expected = 'content for the response body';
     $this->_response->setBody($expected);
     $additional = '; and then there was more';
     $this->_response->appendBody($additional);
     $this->assertEquals($expected . $additional, $this->_response->getBody());
 }
Beispiel #2
0
 /** Set up the response rendering
  * 
  * @param string $response
  */
 public function formatResponse(Zend_Http_Response $response)
 {
     if ('json' === $this->getResponseType()) {
         return json_decode($response->getBody());
     } else {
         return new Zend_Rest_Client_Result($response->getBody());
     }
 }
Beispiel #3
0
 public function run()
 {
     if ($this->debugMode) {
         echo "Restricting crawl to {$this->domain}\n";
     }
     //loop across available items in the queue of pages to crawl
     while (!$this->queue->isEmpty()) {
         if (isset($this->limit) && $this->counter >= $this->limit) {
             break;
         }
         $this->counter++;
         //get a new url to crawl
         $url = $this->queue->pop();
         if ($this->debugMode) {
             echo "Queue Length: " . $this->queue->queueLength() . "\n";
             echo "Crawling " . $url . "\n";
         }
         //set the url into the http client
         $this->client->setUri($url);
         //make the request to the remote server
         $this->currentResponse = $this->client->request();
         //don't bother trying to parse this if it's not text
         if (stripos($this->currentResponse->getHeader('Content-type'), 'text') === false) {
             continue;
         }
         //search for <a> tags in the document
         $body = $this->currentResponse->getBody();
         $linksQuery = new Zend_Dom_Query($body);
         $links = $linksQuery->query('a');
         if ($this->debugMode) {
             echo "\tFound " . count($links) . " links...\n";
         }
         foreach ($links as $link) {
             //get the href of the link and find out if it links to the current host
             $href = $link->getAttribute('href');
             $urlparts = parse_url($href);
             if ($this->stayOnDomain && isset($urlparts["host"]) && $urlparts["host"] != $this->domain) {
                 continue;
             }
             //if it's an absolute link without a domain or a scheme, attempt to fix it
             if (!isset($urlparts["host"])) {
                 $href = 'http://' . $this->domain . $href;
                 //this is a really naive way of doing this!
             }
             //push this link into the queue to be crawled
             $this->queue->push($href);
         }
         //for each page that we see, run every registered task across it
         foreach ($this->tasks as $task) {
             $task->task($this->currentResponse, $this->client);
         }
     }
     //after we're done with everything, call the shutdown hook on all the tasks
     $this->shutdownTasks();
 }
Beispiel #4
0
 public function load(\Zend_Http_Response $httpResponse)
 {
     $this->_reset();
     if ($httpResponse->getBody()) {
         set_error_handler(array($this, 'handleLoadErrors'));
         $this->_json = json_decode($httpResponse->getBody());
         restore_error_handler();
     }
     $this->_httpResponse = $httpResponse;
     return $this;
 }
Beispiel #5
0
 public function load(\Zend_Http_Response $httpResponse)
 {
     $this->_reset();
     if ($httpResponse->getBody()) {
         set_error_handler(array($this, 'handleLoadErrors'));
         $this->_simpleXml = simplexml_load_string($httpResponse->getBody());
         restore_error_handler();
     }
     $this->_httpResponse = $httpResponse;
     return $this;
 }
Beispiel #6
0
 public function load(\Zend_Http_Response $httpResponse)
 {
     $this->_reset();
     if ($httpResponse->getBody()) {
         set_error_handler(array($this, 'handleLoadErrors'));
         $this->_dom = new \DOMDocument();
         $this->_dom->loadXml($httpResponse->getBody());
         restore_error_handler();
     }
     $this->_httpResponse = $httpResponse;
     return $this;
 }
Beispiel #7
0
 /**
  *
  * @param string $path
  * @param array $options
  */
 protected function _callApi($path, $options)
 {
     $param['format'] = 'json';
     $this->_response = $restClient->restGet($path, $options);
     switch ($param['format']) {
         case 'json':
             $this->_data = json_decode($this->_response->getBody());
             break;
         case 'xml':
             throw new \Thin\Exception('Not yet implemented. Please use json format.');
             break;
     }
     $this->_checkErrors();
     return $this->_data['data'];
 }
Beispiel #8
0
    /**
     * Gets the document object for this response
     *
     * @return DOMDocument the DOM Document for this response.
     */
    public function getDocument()
    {
        try {
            $body = $this->_httpResponse->getBody();
        } catch (\Zend\Http\Exception $e) {
            $body = false;
        }

        if ($this->_document === null) {
            if ($body !== false) {
                // turn off libxml error handling
                $errors = libxml_use_internal_errors();

                $this->_document = new \DOMDocument();
                if (!$this->_document->loadXML($body)) {
                    $this->_document = false;
                }

                // reset libxml error handling
                libxml_clear_errors();
                libxml_use_internal_errors($errors);
            } else {
                $this->_document = false;
            }
        }

        return $this->_document;
    }
Beispiel #9
0
 /**
  * Parses the JSON encoded request body returned by the Recensus API and 
  * returns a PHP array representing the resourse.
  * 
  * @return array
  */
 protected function parseResponse()
 {
     $parseResult = json_decode($this->lastResponse->getBody(), true);
     if (!$parseResult) {
         $this->handleError("Error decoding response from API.");
     }
     return $parseResult;
 }
Beispiel #10
0
 /**
  * @param Zend_Http_Response $response
  * @return json string
  * @throws
  */
 public function parseZendResponse(Zend_Http_Response $response)
 {
     if ($response->getStatus() == 200) {
         return $response->getBody();
     } else {
         throw new Exception("Error: Status is: " . $response->getStatus() . " message: " . $response->getMessage());
     }
 }
Beispiel #11
0
 public function testClearBody()
 {
     $this->_response->append('some', "some content\n");
     $this->assertTrue($this->_response->clearBody());
     $body = $this->_response->getBody(true);
     $this->assertTrue(is_array($body));
     $this->assertEquals(0, count($body));
 }
 /**
  * Sends a request and returns a response
  *
  * @param CartRecover_Request $request
  * @return Cart_Recover_Response
  */
 public function sendRequest(CartRecover_Request $request)
 {
     $this->client->setUri($request->getUri());
     $this->client->setParameterGet($request->getParams());
     $this->client->setMethod($request->getMethod());
     $this->client->setHeaders('Accept', 'application/json');
     $this->response = $this->client->request();
     if ($this->response->getHeader('Content-Type') != 'application/json') {
         throw new CartRecover_Exception_UnexpectedValueException("Unknown response format.");
     }
     $body = json_decode($this->response->getBody(), true);
     $response = new CartRecover_Response();
     $response->setRawResponse($this->response->asString());
     $response->setBody($body);
     $response->setHeaders($this->response->getHeaders());
     $response->setStatus($this->response->getMessage(), $this->response->getStatus());
     return $response;
 }
Beispiel #13
0
 /**
  *
  *
  * @param Zend_Http_Response $response JSON response from the PinPayments gateway
  * @throws Dwyera_Pinpay_Model_ResponseParseException If an invalid JSON response object is passed
  */
 public function __construct(Zend_Http_Response $response)
 {
     $this->response = $response;
     $this->httpResponseCode = $response->getStatus();
     $this->msgObj = json_decode($response->getBody());
     if ($this->msgObj == null) {
         throw new Dwyera_Pinpay_Model_ResponseParseException("Could not parse PinPayments gateway response");
     }
 }
Beispiel #14
0
 public function task(Zend_Http_Response $response, Zend_Http_Client $client)
 {
     $query = new Zend_Dom_Query($response->getBody());
     $images = $query->query('img');
     foreach ($images as $image) {
         $this->images[] = $image->getAttribute('src');
     }
     $this->images = array_unique($this->images);
 }
 /**
  * base constructor for Response objects
  *
  * @param Zend_Http_Response $response
  */
 public function __construct($response)
 {
     if ($response instanceof Zend_Http_Response) {
         $this->_response = WirecardCEE_Stdlib_SerialApi::decode($response->getBody());
     } elseif (is_array($response)) {
         $this->_response = $response;
     } else {
         throw new WirecardCEE_Stdlib_Exception_InvalidResponseException(sprintf('Invalid response from WirecardCEE thrown in %s.', __METHOD__));
     }
 }
Beispiel #16
0
 /**
  * (non-PHPdoc)
  * @see Tid_Rest_Processor_Input_Abstract::processInputData()
  */
 public function processInputData(Zend_Http_Response $response = null)
 {
     try {
         return isset($response) ? Zend_Json::decode($response->getBody()) : array();
     } catch (Exception $e) {
         $this->getRestService()->log("Error parsing JSON response: " . $e->getMessage(), Zend_Log::ERR);
         $this->getRestService()->log($e, Zend_Log::ERR);
         throw new App_Rest_Processor_Input_Exception('Error response: [code] ' . $response->getStatus() . ' [error] ' . $response->getMessage());
     }
 }
Beispiel #17
0
 /**
  * Extract the API response data from the given HTTP response object.
  *
  * @param Zend_Http_Response $response
  *
  * @return $this
  */
 protected function parseRawResponse(Zend_Http_Response $response)
 {
     if ($response->isSuccessful()) {
         $content = $response->getBody();
         if (strlen($content) > 0) {
             try {
                 $xml = simplexml_load_string($response->getBody());
             } catch (Exception $e) {
                 // Failed to parse XML
                 $this->successful = false;
                 $this->setMessage("Failed to parse a response from Klevu.");
                 Mage::helper('klevu_search')->log(Zend_Log::ERR, sprintf("Failed to parse XML response: %s", $e->getMessage()));
                 return $this;
             }
             $this->xml = $xml;
             $this->successful = true;
         } else {
             // Response contains no content
             $this->successful = false;
             $this->setMessage('Failed to parse a response from Klevu.');
             Mage::helper('klevu_search')->log(Zend_Log::ERR, "API response content is empty.");
         }
     } else {
         // Unsuccessful HTTP response
         $this->successful = false;
         switch ($response->getStatus()) {
             case 403:
                 $message = "Incorrect API keys.";
                 break;
             case 500:
                 $message = "API server error.";
                 break;
             case 503:
                 $message = "API server unavailable.";
                 break;
             default:
                 $message = "Unexpected error.";
         }
         $this->setMessage(sprintf("Failed to connect to Klevu: %s", $message));
         Mage::helper('klevu_search')->log(Zend_Log::ERR, sprintf("Unsuccessful HTTP response: %s %s", $response->getStatus(), $response->responseCodeAsText($response->getStatus())));
     }
     return $this;
 }
 /**
  * Retrieve latitude and longitude from the API response
  *
  * @param Zend_Http_Response|boolean $response
  * @return array|boolean
  */
 protected function _parseResponse($response)
 {
     if ($response->isSuccessful() && $response->getStatus() == 200) {
         $_response = json_decode($response->getBody());
         $_coordinates = $_response->results[0]->geometry->location;
         $geo = array('lat' => $_coordinates->lat, 'lng' => $_coordinates->lng);
         return $geo;
     }
     return false;
 }
Beispiel #19
0
 /**
  * Decode request response
  *
  * @param Zend_Http_Response $response response of request
  * @throws Zend_Db_Exception
  * @return array
  */
 protected function _decodeResponse(Zend_Http_Response $response)
 {
     $result = Zend_Json::decode($response->getBody());
     if (is_null($result)) {
         throw new Zend_Db_Exception($response->getMessage());
     }
     if ($result["error"]) {
         throw new Exception($result["reason"], $this->_errors[$result["error"]]);
     }
     return $result;
 }
Beispiel #20
0
 /**
  * Constructeur
  *
  * Affecte la réponse HTTP à une propriété, ainsi que le body.
  * Il tente ensuite de déchiffrer le corps en tant que JSON.
  *
  * @param  Zend_Http_Response $httpResponse
  * @throws SDIS62_Service_Generic_Exception
  */
 public function __construct(Zend_Http_Response $httpResponse)
 {
     $this->httpResponse = $httpResponse;
     $this->rawBody = $httpResponse->getBody();
     try {
         $jsonBody = Zend_Json::decode($this->rawBody, Zend_Json::TYPE_OBJECT);
         $this->jsonBody = $jsonBody;
     } catch (Zend_Json_Exception $e) {
         throw new SDIS62_Service_Generic_Exception('impossible de décoder la réponse: ' . $e->getMessage(), 0, $e);
     }
 }
Beispiel #21
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'];
             }
         }
     }
 }
Beispiel #22
0
 public function testGetBodyAsArray()
 {
     $string1 = 'content for the response body';
     $string2 = 'more content for the response body';
     $string3 = 'even more content for the response body';
     $this->_response->appendBody($string1);
     $this->_response->appendBody($string2);
     $this->_response->appendBody($string3);
     $expected = array($string1, $string2, $string3);
     $this->assertEquals($expected, $this->_response->getBody(true));
 }
Beispiel #23
0
 /**
  * Constructor
  *
  * Assigns the HTTP response to a property, as well as the body
  * representation. It then attempts to decode the body as JSON.
  *
  * @param  Zend_Http_Response $httpResponse
  * @throws Zend_Service_Twitter_Exception if unable to decode JSON response
  */
 public function __construct(Zend_Http_Response $httpResponse)
 {
     $this->httpResponse = $httpResponse;
     $this->rawBody = $httpResponse->getBody();
     try {
         $jsonBody = Zend_Json::decode($this->rawBody, Zend_Json::TYPE_OBJECT);
         $this->jsonBody = $jsonBody;
     } catch (Zend_Json_Exception $e) {
         require_once 'Zend/Service/Twitter/Exception.php';
         throw new Zend_Service_Twitter_Exception(sprintf('Unable to decode response from twitter: %s', $e->getMessage()), 0, $e);
     }
 }
 /**
  * @param $ticketId
  * @return array|mixed
  */
 public function listComments($ticketId)
 {
     try {
         $this->init()->setMethod('desk/comments')->addFilter('ticket', $ticketId)->doRequest();
     } catch (Exception $e) {
         Mage::logException($e);
     }
     if ($this->result->getStatus() == 200) {
         return json_decode($this->result->getBody());
     }
     return array();
 }
 /**
  * base ctor for Response objects
  * @param Zend_Http_Response $response
  */
 public function __construct($response)
 {
     if ($response instanceof Zend_Http_Response) {
         $this->_response = WirecardCEE_SerialApi::decode($response->getBody());
     } else {
         if (is_array($response)) {
             $this->_response = $response;
         } else {
             throw new WirecardCEE_Exception('Invalid response from WirecardCEE');
         }
     }
 }
Beispiel #26
0
 public function testClearBodySegment()
 {
     $this->_response->append('some', "some content\n");
     $this->_response->append('more', "more content\n");
     $this->_response->append('superfluous', "superfluous content\n");
     $this->assertFalse($this->_response->clearBody('many'));
     $this->assertTrue($this->_response->clearBody('more'));
     $body = $this->_response->getBody(true);
     $this->assertTrue(is_array($body));
     $this->assertEquals(2, count($body));
     $this->assertTrue(isset($body['some']));
     $this->assertTrue(isset($body['superfluous']));
 }
Beispiel #27
0
 protected function processMessages(Zend_Http_Response $response)
 {
     $lastMessageId = 0;
     $messages = json_decode($response->getBody(), true);
     if (!$messages) {
         return $lastMessageId;
     }
     foreach ($messages['messages'] as $message) {
         if ($message) {
             $this->triggerEvents($message);
             $lastMessageId = $message['id'];
         }
     }
     return $lastMessageId;
 }
 /**
  * Retrieve latitude and longitude from the API response
  *
  * @param Zend_Http_Response|boolean $response
  * @return array|boolean
  */
 protected function _parseResponse($response)
 {
     if ($response->isSuccessful() && $response->getStatus() == 200) {
         $_response = json_decode($response->getBody());
         if (is_array($_response->postalcodes)) {
             $_response = array_shift($_response->postalcodes);
         }
         if ($_response) {
             $geo = array('lat' => $_response->lat, 'lng' => $_response->lng);
         } else {
             $geo = false;
         }
         return $geo;
     }
     return false;
 }
Beispiel #29
0
 /**
  * Parses response body and generates NP_Gravatar_Profile
  * instance.
  *
  * @param Zend_Http_Response $response
  * @return NP_Service_Gravatar_Profiles_Profile|Zend_Http_Response
  */
 public static function profileFromHttpResponse(Zend_Http_Response $response)
 {
     $body = $response->getBody();
     $profile = @unserialize($body);
     if ($profile === false) {
         require_once 'NP/Service/Gravatar/Profiles/ResponseFormat/Exception.php';
         throw new NP_Service_Gravatar_Profiles_ResponseFormat_Exception('Invalid PHP response.');
     }
     if (is_array($profile) && isset($profile['entry'])) {
         //Valid response?
         require_once 'NP/Service/Gravatar/Profiles/Profile.php';
         return new NP_Service_Gravatar_Profiles_Profile($profile['entry'][0]);
     } else {
         //Probably unexisting user is supplied.
         return $response;
     }
 }
Beispiel #30
0
 /**
  * Parses response body and generates NP_Gravatar_Profile
  * instance.
  *
  * @param Zend_Http_Response $response
  * @return NP_Service_Gravatar_Profiles_Profile|Zend_Http_Response
  */
 public static function profileFromHttpResponse(Zend_Http_Response $response)
 {
     $body = $response->getBody();
     try {
         $xml = simplexml_load_string($body);
     } catch (Exception $ex) {
         require_once 'NP/Service/Gravatar/Profiles/ResponseFormat/Exception.php';
         throw new NP_Service_Gravatar_Profiles_ResponseFormat_Exception('Invalid XML response.');
     }
     if (!$xml->entry) {
         //Probably unexisting user is supplied.
         return $response;
     }
     $profileData = self::_xmlToArray($xml->entry[0]);
     require_once 'NP/Service/Gravatar/Profiles/Profile.php';
     return new NP_Service_Gravatar_Profiles_Profile($profileData);
 }