Exemple #1
0
 /**
  * Callback for when the URL has been shortened. Checks for error messages.
  *
  * @param Phergie_Plugin_Http_Response $response the response object
  *
  * @return string|bool the shortened url or false on failure
  */
 protected function onComplete($response)
 {
     if (strpos($response->getContent(), 'Error: ') === 0) {
         return false;
     }
     return $response->getContent();
 }
Exemple #2
0
 /**
  * Callback for when the URL has been shortened. Checks for error messages.
  *
  * @param Phergie_Plugin_Http_Response $response the response object
  *
  * @return string|bool the shortened url or false on failure
  */
 protected function onComplete($response)
 {
     if ($response->getCode() == 201) {
         return $response->getContent();
     }
     return false;
 }
Exemple #3
0
 /**
  * Supporting method that executes a request and handles the response.
  *
  * @param string $url URL to request
  * @param array $context Associative array of stream context parameters 
  *
  * @return Phergie_Plugin_Http_Response Object representing the response 
  *         resulting from the request
  */
 public function request($url, array $context)
 {
     $this->response = new Phergie_Plugin_Http_Response();
     $url = (string) $url;
     $context = stream_context_create(array('http' => $context));
     set_error_handler(array($this, 'handleError'), E_WARNING);
     $stream = fopen($url, 'r', false, $context);
     if ($stream) {
         $meta = stream_get_meta_data($stream);
         $status = $this->parseStatusLine($meta['wrapper_data'][0]);
         $code = $status['code'];
         $message = $status['message'];
         $headers = array();
         foreach (array_slice($meta['wrapper_data'], 1) as $header) {
             list($name, $value) = explode(': ', $header, 2);
             $headers[$name] = $value;
         }
         unset($meta['wrapper_data']);
         $this->response->setCode($code)->setMessage($message)->setHeaders($headers)->setMeta($meta);
         $body = stream_get_contents($stream);
         $type = $this->response->getHeaders('content-type');
         foreach ($this->handlers as $expr => $handler) {
             if (preg_match('#^' . $expr . '$#i', $type)) {
                 $body = call_user_func($handler, $body);
             }
         }
         $this->response->setContent($body);
     }
     restore_error_handler();
     return $this->response;
 }
Exemple #4
0
 /**
  * Chews on reply from wunderground's API and spits out useful information
  *
  * @param Phergie_Plugin_Http_Response $response
  * @return array Array of useful information
  * @throws Phergie_Exception
  */
 public function parseWeatherInfo($response)
 {
     $xml = $response->getContent();
     if (isset($xml->results)) {
         $this->setBogusLocation(true);
         throw new Phergie_Exception("That location is too ambiguous.  Please be more specific.");
     }
     if (isset($xml->error)) {
         $this->setBogusLocation(true);
         throw new Phergie_Exception("That location was not found.");
     }
     $this->setBogusLocation(false);
     $co = $xml->current_observation;
     return array('tempString' => $co->temperature_string, 'weather' => $co->weather, 'wind_string' => $co->wind_string, 'wind_chill_string' => $co->windchill_string, 'heat_index_string' => $co->heat_index_string);
 }