Exemplo n.º 1
0
 /**
  * Fetch an URL's content. Follows redirects up until the defined 
  * constant REDIRECT_LIMIT times.
  *
  * @param   peer.http.HttpConnection connection
  * @param   array params default array()
  * @param   array headers default array()
  * @return  string
  * @throws  peer.http.UnexpectedResponseException
  */
 public static function get($connection, $params = array(), $headers = array())
 {
     $redirected = 0;
     do {
         try {
             $response = $connection->get($params, $headers);
         } catch (Throwable $e) {
             throw new UnexpectedResponseException($e->getMessage(), -1);
         }
         // Check return code
         switch ($sc = $response->getStatusCode()) {
             case 200:
                 // 200 OK - fetch data
                 $content = '';
                 while (FALSE !== ($buf = $response->readData())) {
                     $content .= $buf;
                 }
                 return $content;
             case 301:
                 // 301 Moved permanently or
             // 301 Moved permanently or
             case 302:
                 // 302 Moved temporarily - redirect
                 if (!($loc = $response->getHeader('Location'))) {
                     throw new UnexpectedResponseException('Redirect status ' . $sc . ', but no location header in ' . $response->toString(), $sc);
                 }
                 if ($redirected >= REDIRECT_LIMIT) {
                     throw new UnexpectedResponseException('Redirection limit (' . REDIRECT_LIMIT . ') reached @ ' . $loc, $sc);
                 }
                 $redirected++;
                 $connection->request = HttpRequestFactory::factory(new URL($loc));
                 break;
             default:
                 // Any other code
                 throw new UnexpectedResponseException('Unexpected answer ' . $response->toString(), $sc);
         }
     } while ($redirected < REDIRECT_LIMIT + 1);
 }