[php][pear]HTTP_Request2のサンプル#5
この記事は、HTTP_Requestのマニュアルに載っている全サンプルコードをHTTP_Request2対応で書き換えてみる企画の最終回(5回目)です。HTTP_Request2クラスの概要を知りたい方は、下記リンクの#1を参照ください。
全記事へのリンク:
- [php][pear]HTTP_Request2のサンプル#1
- [php][pear]HTTP_Request2のサンプル#2
- [php][pear]HTTP_Request2のサンプル#3
- [php][pear]HTTP_Request2のサンプル#4
- [php][pear]HTTP_Request2のサンプル#5
#13 ダウンロード進捗バー(コマンドラインのみ)
php:
-
<?php
-
/**
-
* 例 48-13 ダウンロード進捗バー(コマンドラインのみ)
-
*
-
* @link http://pear.php.net/manual/ja/package.http.http-request.listeners.php
-
* @see http://www.php.net/~helly/php/ext/spl/interfaceSplObserver.html
-
*/
-
require_once 'HTTP/Request2.php';
-
require_once 'Console/ProgressBar.php';
-
-
-
class HTTP_Request2_Observer_Progress implements SplObserver
-
{
-
/**
-
* 対象となるファイルのハンドル
-
* @var int
-
*/
-
protected $fp;
-
-
/**
-
* インジケータの表示に使用する Console_ProgressBar のインスタンス
-
* @var Console_ProgressBar
-
*/
-
protected $bar;
-
-
/**
-
* 対象となるファイルの名前
-
* @var string
-
*/
-
protected $target = "filename";
-
-
/**
-
* 受信したファイルのバイト数
-
* @var int
-
*/
-
protected $size;
-
-
/**
-
* 対象となるファイルをオープンする
-
* @param string ファイル名
-
* @throws HTTP_Request2_Exception
-
*/
-
function setTarget($target)
-
{
-
$this->target = $target;
-
if (!$this->fp) {
-
throw new HTTP_Request2_Exception("'{$target}' をオープンできません");
-
}
-
}
-
-
/**
-
* HTTP_Request2から呼ばれる (例えば、値が変わった時)
-
*
-
* @param SplSubject $subject
-
*/
-
public function update(SplSubject $subject)
-
{
-
$event = $subject->getLastEvent();
-
-
switch ($event['name']) {
-
case 'connect':
-
break;
-
case 'receivedHeaders':
-
$headers = $event['data']->getHeader();
-
} else {
-
$this->setTarget($this->target);
-
}
-
$this->bar = new Console_ProgressBar(
-
'* ' . $this->target . ' %fraction% KB [%bar%] %percent%', '=>', '-',
-
);
-
$this->size = 0;
-
break;
-
case 'receivedEncodedBodyPart':
-
case 'receivedBodyPart':
-
break;
-
case 'receivedBody':
-
break;
-
}
-
}
-
}
-
-
try {
-
$req = new HTTP_Request2("http://pear.php.net/get/HTML_QuickForm-stable");
-
-
$observer = new HTTP_Request2_Observer_Progress();
-
$req->attach($observer);
-
-
$response = $req->send();
-
-
} catch (HTTP_Request2_Exception $e) {
-
} catch (Exception $e) {
-
}
-
-
/*****************************
-
* HTTP_Request_Listenerクラスを派生する代わりにSplObserverインターフェースを実装します。
-
*
-
* このとき、update(SplSubject $subject)メソッドを実装する必要があります。
-
* $subjectにはHTTP_Request2が代入されています(HTTP_Request2はSplSubjectの実装クラス)。
-
*
-
* たいていの場合、HTTP_Request2::getLastEvent()メソッドを呼び出して、イベントを取得します。
-
* 使用するアダプタによってはイベントを発生させないことがあります。
-
* 例えば、Mockアダプタはイベント発生させません。
-
*
-
* getLastEvent()メソッドの返り値はarray('name' => string, 'data' => mixed)の配列です。
-
* イベントは以下の8種類が用意されています。
-
* - connect
-
* - sentHeaders
-
* - sentBodyPart
-
* - receivedHeaders
-
* - receivedBodyPart
-
* - receivedEncodedBodyPart
-
* - receivedBody
-
* - disconnect
-
* イベントの種類によって、データの種類は異なります。
-
*****************************/
-
-
/*
-
-
// HTTP_Request でのリスナーの使用例です。これは、ファイルの
-
// ダウンロードと保存を行うと同時に、処理の進捗状況をバーで表示します
-
//
-
// 注意すべき点は以下のとおりです
-
// 1) ブラウザでなく、コンソールから実行しないといけません
-
// 2) 出力バッファリングを OFF にしないと正常に動作しません
-
-
require_once 'HTTP/Request.php';
-
require_once 'HTTP/Request/Listener.php';
-
require_once 'Console/ProgressBar.php';
-
-
PEAR::setErrorHandling(PEAR_ERROR_DIE);
-
-
set_time_limit(0);
-
-
class HTTP_Request_DownloadListener extends HTTP_Request_Listener
-
{
-
// 対象となるファイルのハンドル
-
// @var int
-
var $_fp;
-
-
// インジケータの表示に使用する Console_ProgressBar のインスタンス
-
// @var object
-
var $_bar;
-
-
// 対象となるファイルの名前
-
// @var string
-
var $_target;
-
-
// 受信したファイルのバイト数
-
// @var int
-
var $_size = 0;
-
-
function HTTP_Request_DownloadListener()
-
{
-
$this->HTTP_Request_Listener();
-
}
-
-
// 対象となるファイルをオープンする
-
// @param string ファイル名
-
// @throws PEAR_Error
-
function setTarget($target)
-
{
-
$this->_target = $target;
-
$this->_fp = @fopen($target, 'wb');
-
if (!$this->_fp) {
-
PEAR::raiseError("'{$target}' をオープンできません");
-
}
-
}
-
-
function update(&$subject, $event, $data = null)
-
{
-
switch ($event) {
-
case 'sentRequest':
-
$this->_target = basename($subject->_url->path);
-
break;
-
-
case 'gotHeaders':
-
if (isset($data['content-disposition']) &&
-
preg_match('/filename="([^"]+)"/', $data['content-disposition'], $matches)) {
-
-
$this->setTarget(basename($matches[1]));
-
} else {
-
$this->setTarget($this->_target);
-
}
-
$this->_bar =& new Console_ProgressBar(
-
'* ' . $this->_target . ' %fraction% KB [%bar%] %percent%', '=>', '-',
-
79, (isset($data['content-length'])? round($data['content-length'] / 1024): 100)
-
);
-
$this->_size = 0;
-
break;
-
-
case 'tick':
-
$this->_size += strlen($data);
-
$this->_bar->update(round($this->_size / 1024));
-
fwrite($this->_fp, $data);
-
break;
-
-
case 'gotBody':
-
fclose($this->_fp);
-
break;
-
-
default:
-
PEAR::raiseError("イベント '{$event}' が処理できません");
-
} // switch
-
}
-
}
-
-
// 別のパッケージを使用してもかまいませんが、できるだけ大き目のものを
-
// 選ばないと進捗バーが見えません
-
$url = 'http://pear.php.net/get/HTML_QuickForm-stable';
-
-
$req =& new HTTP_Request($url);
-
-
$download =& new HTTP_Request_DownloadListener();
-
$req->attach($download);
-
$req->sendRequest(false);
-
*/
-
?>
最後のサンプルになりましたが、昔のHTTP_Request_Listenerに当たるSplObserver派生クラスのサンプルとなりました。HTTP_Request2のパッケージにはHTTP_Request2_Observer_Logが同梱されているので、そちらを参考にすると、SplObserver派生クラスについての理解が深められます。
全サンプルコードを載せましたが、0.2.0(alpha)の時点でだいたいの機能はそろっている気がしますね。
No Responses to “[php][pear]HTTP_Request2のサンプル#5”
コメントはまだありません。
Comments RSS
TrackBack Identifier URI
コメントする
