- example fixup
[m6w6/ext-http] / docs / examples / tutorial.txt
index 6dcae5ad4367577c4b7e86c8bb26c93e806c2278..b29fa96c99b10b83fac958979936ab950fbda156 100644 (file)
@@ -44,7 +44,7 @@ try {
        redirect option.
 
 <?php
-$r = new HttpRequest('http://dev.iworks.at/.print_request.php', HTTP_POST);
+$r = new HttpRequest('http://dev.iworks.at/.print_request.php', HTTP_METH_POST);
 
 // if redirects is set to true, a single redirect is allowed;
 // one can set any reasonable count of allowed redirects
@@ -82,8 +82,8 @@ try {
        $p = new HttpRequestPool;
        // if you want to set _any_ options of the HttpRequest object,
        // you need to do so *prior attaching* to the request pool!
-       $p->attach(new HttpRequest('http://pear.php.net', HTTP_HEAD));
-       $p->attach(new HttpRequest('http://pecl.php.net', HTTP_HEAD));
+       $p->attach(new HttpRequest('http://pear.php.net', HTTP_METH_HEAD));
+       $p->attach(new HttpRequest('http://pecl.php.net', HTTP_METH_HEAD));
 } catch (HttpException $e) {
        print $e;
        exit;
@@ -93,7 +93,7 @@ try {
        $p->send();
        // HttpRequestPool implements an iterator over attached HttpRequest objects
        foreach ($p as $r) {
-               print "Checking ", $r->getUrl(), " reported ", $r->getResponseCode(), "\n";
+               echo "Checking ", $r->getUrl(), " reported ", $r->getResponseCode(), "\n";
        }
 } catch (HttpException $e) {
        print $e;
@@ -112,8 +112,8 @@ class Pool extends HttpRequestPool
        public function __construct()
        {
                parent::__construct(
-                       new HttpRequest('http://pear.php.net', HTTP_HEAD),
-                       new HttpRequest('http://pecl.php.net', HTTP_HEAD)
+                       new HttpRequest('http://pear.php.net', HTTP_METH_HEAD),
+                       new HttpRequest('http://pecl.php.net', HTTP_METH_HEAD)
                );
 
                // HttpRequestPool methods socketPerform() and socketSelect() are
@@ -121,7 +121,7 @@ class Pool extends HttpRequestPool
                // while the requests are being executed
                print "Executing requests";
                for ($i = 0; $this->socketPerform(); $i++) {
-                       $i % 3 or print ".";
+                       $i % 10 or print ".";
                        if (!$this->socketSelect()) {
                                throw new HttpException("Socket error!");
                        }
@@ -132,7 +132,7 @@ class Pool extends HttpRequestPool
 
 try {
        foreach (new Pool as $r) {
-               print "Checking ", $r->getUrl(), " reported ", $r->getResponseCode(), "\n";
+               echo "Checking ", $r->getUrl(), " reported ", $r->getResponseCode(), "\n";
        }
 } catch (HttpException $ex) {
        print $e;
@@ -186,7 +186,7 @@ class XmlRpcClient
        public function __construct($url, $namespace = '')
        {
                $this->namespace = $namespace;
-               $this->request = new HttpRequest($url, HTTP_POST);
+               $this->request = new HttpRequest($url, HTTP_METH_POST);
                $this->request->setContentType('text/xml');
        }
 
@@ -255,15 +255,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();
 
@@ -290,16 +290,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 {
@@ -312,13 +312,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;
@@ -327,14 +327,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);
+               }
        }
 }
 ?>