X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=docs%2Fexamples%2Ftutorial.txt;h=8d8998d0a05a5df8c588e85b38a60507d31ec3c9;hb=refs%2Fheads%2Fv1.7.x;hp=6dcae5ad4367577c4b7e86c8bb26c93e806c2278;hpb=b562e34cf4f8c8fae7b8fe773e0eed71592b09c2;p=m6w6%2Fext-http diff --git a/docs/examples/tutorial.txt b/docs/examples/tutorial.txt index 6dcae5a..8d8998d 100644 --- a/docs/examples/tutorial.txt +++ b/docs/examples/tutorial.txt @@ -12,11 +12,12 @@ $Revision$ read from and written to a file. setOptions( - array( 'cookiestore' => '../cookies/google.txt', + array( 'cookiestore' => 'google.txt', ) ); @@ -27,7 +28,7 @@ $r->setQueryData( ); // HttpRequest::send() returns an HttpMessage object -// of type HttpMessage::RESPONSE or throws an exception +// of type HttpMessage::TYPE_RESPONSE or throws an exception try { print $r->send()->getBody(); } catch (HttpException $e) { @@ -44,7 +45,7 @@ try { redirect option. setPostFields( ) ); // add the file to post (form name, file name, file type) +touch('profile.jpg'); $r->addPostFile('image', 'profile.jpg', 'image/jpeg'); try { @@ -82,8 +84,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 +95,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 +114,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 +123,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 +134,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; @@ -171,172 +173,3 @@ HttpResponse::setContentType('application/x-zip'); HttpResponse::setFile('../archive.zip'); HttpResponse::send(); ?> - -Exemplar Use Cases ------------------- - -- KISS XMLRPC Client - -namespace = $namespace; - $this->request = new HttpRequest($url, HTTP_POST); - $this->request->setContentType('text/xml'); - } - - public function setOptions($options = array()) - { - return $this->request->setOptions($options); - } - - public function addOptions($options) - { - return $this->request->addOptions($options); - } - - public function __call($method, $params) - { - if ($this->namespace) { - $method = $this->namespace .'.'. $method; - } - $this->request->setPostData(xmlrpc_encode_request($method, $params)); - $response = $this->request->send(); - if ($response->getResponseCode() != 200) { - throw new Exception($response->getBody(), $response->getResponseCode()); - } - return xmlrpc_decode($response->getBody(), 'utf-8'); - } - - public function getHistory() - { - return $this->request->getHistory(); - } -} - -?> - -- Simple Feed Aggregator - -setDirectory($directory); - } - - public function setDirectory($directory) - { - $this->directory = $directory; - foreach (glob($this->directory .'/*.xml') as $feed) { - $this->feeds[basename($feed, '.xml')] = filemtime($feed); - } - } - - public function url2name($url) - { - return preg_replace('/[^\w\.-]+/', '_', $url); - } - - public function hasFeed($url) - { - return isset($this->feeds[$this->url2name($url)]); - } - - public function addFeed($url) - { - $r = $this->setupRequest($url); - $r->send(); - $this->handleResponse($r); - } - - public function addFeeds($urls) - { - $pool = new HttpRequestPool; - foreach ($urls as $url) { - $pool->attach($this->setupRequest($url)); - } - $pool->send(); - - foreach ($pool as $request) { - $this->handleResponse($request); - } - } - - public function getFeed($url) - { - $this->addFeed($url); - return $this->loadFeed($this->url2name($url)); - } - - public function getFeeds($urls) - { - $feeds = array(); - $this->addFeeds($urls); - foreach ($urls as $url) { - $feeds[] = $this->loadFeed($this->url2name($url)); - } - return $feeds; - } - - 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"); - } - } - - protected function loadFeed($file) - { - if (isset($this->feeds[$file]) { - if ($data = file_get_contents($this->directory .'/'. $file .'.xml')) { - return $data; - } else { - throw new Exception("Could not load feed contents from $file.xml"); - } - } else { - throw new Exception("Unknown feed/file $file.xml"); - } - } - - protected function setupRequest($url) - { - $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])); - } - - return $r; - } - - 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); - } - } -} -?> - -