- examples fixup
[m6w6/ext-http] / docs / examples / tutorial.txt
index 0a629ee47d8807daba4e8e97561a0593b0f83ffa..96ee9de72cd4652ccb7b9c1f3e856a77beaa6ae4 100644 (file)
@@ -12,11 +12,12 @@ $Revision$
        read from and written to a file.
 
 <?php
-$r = new HttpRequest('http://www.google.com');
+$r = new HttpRequest('http://www.google.com/search');
 
 // store Googles cookies in a dedicated file
+touch('google.txt');
 $r->setOptions(
-       array(  'cookiestore'   => '../cookies/google.txt',
+       array(  'cookiestore'   => 'google.txt',
        )
 );
 
@@ -44,7 +45,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 +83,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 +94,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 +113,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 +122,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 +133,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 +187,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');
        }
 
@@ -205,7 +206,7 @@ class XmlRpcClient
                if ($this->namespace) {
                        $method = $this->namespace .'.'. $method;
                }
-               $this->request->setPostData(xmlrpc_encode_request($method, $params));
+               $this->request->setRawPostData(xmlrpc_encode_request($method, $params));
                $response = $this->request->send();
                if ($response->getResponseCode() != 200) {
                        throw new Exception($response->getBody(), $response->getResponseCode());
@@ -263,7 +264,7 @@ class FeedAggregator
        {
                $pool = new HttpRequestPool;
                foreach ($urls as $url) {
-                       $pool->attach($this->setupRequest($url));
+                       $pool->attach($r = $this->setupRequest($url));
                }
                $pool->send();
 
@@ -299,7 +300,7 @@ class FeedAggregator
 
        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 {
@@ -318,7 +319,7 @@ class FeedAggregator
                $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;
@@ -333,7 +334,7 @@ class FeedAggregator
                        if (!strlen($body = $r->getResponseBody())) {
                                throw new Exception("Received empty feed from ". $r->getUrl());
                        }
-                       $this->saveFeed($file, $body);
+                       $this->saveFeed($this->url2name($r->getUrl()), $body);
                }
        }
 }