- example fixup
[m6w6/ext-http] / docs / examples / Simple_Feed_Aggregator.php
index f82e31675be71a89e28bf26122709617a1392bd7..c28e83873fa2cb09429dd01d324aec327ba777e5 100644 (file)
@@ -30,15 +30,15 @@ class FeedAggregator
        public function addFeed($url)
        {
                $r = $this->setupRequest($url);
-        $r->send();
-        $this->handleResponse($r);
+               $r->send();
+               $this->handleResponse($r);
        }
 
        public function addFeeds($urls)
        {
                $pool = new HttpRequestPool;
                foreach ($urls as $url) {
-                       $pool->attach($this->setupRequest($url));
+                       $pool->attach($r = $this->setupRequest($url));
                }
                $pool->send();
 
@@ -65,16 +65,16 @@ class FeedAggregator
 
        protected function saveFeed($file, $contents)
        {
-        if (file_put_contents($this->directory .'/'. $file .'.xml', $contents)) {
-            $this->feeds[$file] = time();
-        } else {
-            throw new Exception("Could not save feed contents to $file.xml");
-        }
+               if (file_put_contents($this->directory .'/'. $file .'.xml', $contents)) {
+                       $this->feeds[$file] = time();
+               } else {
+                       throw new Exception("Could not save feed contents to $file.xml");
+               }
        }
 
        protected function loadFeed($file)
        {
-               if (isset($this->feeds[$file]) {
+               if (isset($this->feeds[$file])) {
                        if ($data = file_get_contents($this->directory .'/'. $file .'.xml')) {
                                return $data;
                        } else {
@@ -87,13 +87,13 @@ class FeedAggregator
 
        protected function setupRequest($url)
        {
-        $r = new HttpRequest($url);
-        $r->setOptions(array('redirect' => true));
+               $r = new HttpRequest($url);
+               $r->setOptions(array('redirect' => true));
 
                $file = $this->url2name($url);
 
                if (isset($this->feeds[$file])) {
-                       $r->addOptions(array('lastmodified' => $this->feeds[$file]));
+                       $r->setOptions(array('lastmodified' => $this->feeds[$file]));
                }
 
                return $r;
@@ -102,14 +102,14 @@ class FeedAggregator
        protected function handleResponse(HttpRequest $r)
        {
                if ($r->getResponseCode() != 304) {
-            if ($r->getResponseCode() != 200) {
-                throw new Exception("Unexpected response code ". $r->getResponseCode());
-            }
-            if (!strlen($body = $r->getResponseBody())) {
-                throw new Exception("Received empty feed from ". $r->getUrl());
-            }
-            $this->saveFeed($file, $body);
-        }
+                       if ($r->getResponseCode() != 200) {
+                               throw new Exception("Unexpected response code ". $r->getResponseCode());
+                       }
+                       if (!strlen($body = $r->getResponseBody())) {
+                               throw new Exception("Received empty feed from ". $r->getUrl());
+                       }
+                       $this->saveFeed($this->url2name($r->getUrl()), $body);
+               }
        }
 }
 ?>