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',
)
);
);
// 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) {
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
)
);
// add the file to post (form name, file name, file type)
+touch('profile.jpg');
$r->addPostFile('image', 'profile.jpg', 'image/jpeg');
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;
$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;
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
// 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!");
}
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;
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');
}
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());
{
$pool = new HttpRequestPool;
foreach ($urls as $url) {
- $pool->attach($this->setupRequest($url));
+ $pool->attach($r = $this->setupRequest($url));
}
$pool->send();
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 {
$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;
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);
}
}
}
?>
+- Download a big file
+
+<?php
+
+/*
+ $bigGet = BigGet::url('http://www.example.com/big_file.bin');
+ $bigGet->saveTo('file.bin');
+*/
+
+class BigGetRequest extends HttpRequest
+{
+ public $id;
+}
+
+class BigGet extends HttpRequestPool
+{
+ const SIZE = 1048576;
+
+ private $url;
+ private $size;
+ private $count = 0;
+ private $files = array();
+
+ static function url($url)
+ {
+ $head = new HttpRequest($url, HttpRequest::METH_HEAD);
+ $headers = $head->send()->getHeaders();
+ $head = null;
+
+ if (!isset($headers['Accept-Ranges'])) {
+ throw new HttpException("Did not receive an Accept-Ranges header from HEAD $url");
+ }
+ if (!isset($headers['Content-Length'])) {
+ throw new HttpException("Did not receive a Content-Length header from HEAD $url");
+ }
+
+ $bigget = new BigGet;
+ $bigget->url = $url;
+ $bigget->size = $headers['Content-Length'];
+ return $bigget;
+ }
+
+ function saveTo($file)
+ {
+ $this->send();
+ if ($w = fopen($file, 'wb')) {
+ echo "\nCopying temp files to $file ...\n";
+ foreach (glob("bigget_????.tmp") as $tmp) {
+ echo "\t$tmp\n";
+ if ($r = fopen($tmp, 'rb')) {
+ stream_copy_to_stream($r, $w);
+ fclose($r);
+ }
+ unlink($tmp);
+ }
+ fclose($w);
+ echo "\nDone.\n";
+ }
+ }
+
+ function send()
+ {
+ // use max 3 simultanous requests with a req size of 1MiB
+ while ($this->count < 3 && -1 != $offset = $this->getRangeOffset()) {
+ $this->attachNew($offset);
+ }
+
+ while ($this->socketPerform()) {
+ if (!$this->socketSelect()) {
+ throw new HttpSocketException;
+ }
+ }
+ }
+
+ private function attachNew($offset)
+ {
+ $stop = min($this->count * self::SIZE + self::SIZE, $this->size) - 1;
+
+ echo "Attaching new request to get range: $offset-$stop\n";
+
+ $req = new BigGetRequest(
+ $this->url,
+ HttpRequest::METH_GET,
+ array(
+ 'headers' => array(
+ 'Range' => "bytes=$offset-$stop"
+ )
+ )
+ );
+ $this->attach($req);
+ $req->id = $this->count++;
+ }
+
+ private function getRangeOffset()
+ {
+ return ($this->size >= $start = $this->count * self::SIZE) ? $start : -1;
+ }
+
+ protected function socketPerform()
+ {
+ try {
+ $rc = parent::socketPerform();
+ } catch (HttpRequestPoolException $x) {
+ foreach ($x->exceptionStack as $e) {
+ echo $e->getMessage(), "\n";
+ }
+ }
+
+ foreach ($this->getFinishedRequests() as $r) {
+ $this->detach($r);
+
+ if (206 != $rc = $r->getResponseCode()) {
+ throw new HttpException("Unexpected response code: $rc");
+ }
+
+ file_put_contents(sprintf("bigget_%04d.tmp", $r->id), $r->getResponseBody());
+
+ if (-1 != $offset = $this->getRangeOffset()) {
+ $this->attachNew($offset);
+ }
+ }
+
+ return $rc;
+ }
+}
+?>