[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
-
/**
-
* 例 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)の時点でだいたいの機能はそろっている気がしますね。
[php][pear]HTTP_Request2のサンプル#4
この記事は、HTTP_Requestのマニュアルに載っている全サンプルコードをHTTP_Request2対応で書き換えてみる企画の4回目です。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
#10 レスポンスコードをチェックする
-
<?php
-
/**
-
* 例 48-10 レスポンスコードをチェックする
-
*
-
* @link http://pear.php.net/manual/ja/package.http.http-request.response-eval.php
-
*/
-
require_once 'HTTP/Request2.php';
-
-
"http://www.example.com/",
-
"http://example.com/thisdoesnotexist.html"
-
);
-
-
try {
-
$req = new HTTP_Request2();
-
foreach ($urls as $url) {
-
$req->setUrl($url);
-
$response = $req->send();
-
-
$code = $response->getStatus();
-
switch ($code) {
-
case 404:
-
echo "Document not foundn";
-
break;
-
-
case 200:
-
echo "Everything's okn";
-
break;
-
}
-
}
-
-
} catch (HTTP_Request2_Exception $e) {
-
} catch (Exception $e) {
-
}
-
-
/*****************************
-
* HTTP_Request::getResponseCode()はHTTP_Request2_Response::getStatus()に変更されました。
-
*****************************/
-
-
/*
-
require_once "HTTP/Request.php";
-
-
$urls = array(
-
"http://www.example.com/",
-
"http://example.com/thisdoesnotexist.html"
-
);
-
-
$req =& new HTTP_Request("");
-
foreach ($urls as $url) {
-
$req->setURL($url);
-
$req->sendRequest();
-
-
$code = $req->getResponseCode();
-
switch ($code) {
-
case 404:
-
echo "Document not foundn";
-
break;
-
-
case 200:
-
echo "Everything's okn";
-
break;
-
-
}
-
}
-
-
*/
-
?>
#11 レスポンスからの全てのヘッダを取得する
-
<?php
-
/**
-
* 例 48-11 レスポンスからの全てのヘッダを取得する
-
*
-
* @link http://pear.php.net/manual/ja/package.http.http-request.response-eval.php
-
*/
-
require_once 'HTTP/Request2.php';
-
-
try {
-
$req = new HTTP_Request2("http://example.com/");
-
$response = $req->send();
-
foreach ($response->getHeader() as $name => $value) {
-
}
-
-
} catch (HTTP_Request2_Exception $e) {
-
} catch (Exception $e) {
-
}
-
-
/*****************************
-
* HTTP_Request::getResponseHeader()はHTTP_Request2_Response::getHeader()に変更されました。
-
*****************************/
-
-
/*
-
require_once "HTTP/Request.php";
-
-
$req =& new HTTP_Request("http://example.com/");
-
$req->sendRequest();
-
-
foreach ($req->getResponseHeader() as $name => $value) {
-
echo $name . " = " . $value . "n";
-
}
-
-
*/
-
?>
#12 特定のヘッダを取得する
-
<?php
-
/**
-
* 例 48-12 特定のヘッダを取得する
-
*
-
* @link http://pear.php.net/manual/ja/package.http.http-request.response-eval.php
-
*/
-
require_once 'HTTP/Request2.php';
-
-
try {
-
$req = new HTTP_Request2("http://example.com/");
-
$response = $req->send();
-
-
} catch (HTTP_Request2_Exception $e) {
-
} catch (Exception $e) {
-
}
-
-
/*****************************
-
* HTTP_Request::getResponseHeader()はHTTP_Request2_Response::getHeader()に変更されました。
-
*****************************/
-
-
/*
-
require_once "HTTP/Request.php";
-
-
$req =& new HTTP_Request("http://example.com/");
-
$req->sendRequest();
-
-
echo $req->getResponseHeader("Date");
-
-
-
*/
-
?>
[php][pear]HTTP_Request2のサンプル#3
この記事は、HTTP_Requestのマニュアルに載っている全サンプルコードをHTTP_Request2対応で書き換えてみる企画の3回目です。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
#6 PDF 文章をアップロードする
-
<?php
-
/**
-
* 例 48-6 PDF 文章をアップロードする
-
*
-
* この例では、/home/johndoe/text.pdf というファイルが
-
* リモートにあるマシン upload.example.com に johndoe-text.pdf という名前で
-
* アップロードされます。 さらに、ジョンが何かをアップロードする事を許可されていることを
-
* 保証するために、Basic 認証が使用されています。
-
*
-
* @link http://pear.php.net/manual/ja/package.http.http-request.file-upload.php
-
*/
-
require_once 'HTTP/Request2.php';
-
-
try {
-
$req = new HTTP_Request2("http://upload.example.com/upload.php", HTTP_Request2::METHOD_POST);
-
$req->setAuth("johndoe", "foo", HTTP_Request2::AUTH_BASIC);
-
$req->addUpload("johndoe-txt.pdf", "/home/johndoe/text.pdf");
-
$response = $req->send();
-
} catch (HTTP_Request2_Exception $e) {
-
} catch (Exception $e) {
-
}
-
-
/*****************************
-
* HTTP_Request::addFile()はHTTP_Request2->addUpload()に変更されました。
-
*****************************/
-
-
/*
-
require_once "HTTP/Request.php";
-
-
$req =& new HTTP_Request("http://upload.example.com/upload.php");
-
$req->setBasicAuth("johndoe", "foo");
-
$req->setMethod(HTTP_REQUEST_METHOD_POST);
-
-
$result = $req->addFile("johndoe-txt.pdf", "/home/johndoe/text.pdf");
-
if (PEAR::isError($result)) {
-
echo $result->getMessage();
-
} else {
-
-
$response = $req->sendRequest();
-
-
if (PEAR::isError($response)) {
-
echo $response->getMessage();
-
} else {
-
echo $req->getResponseBody();
-
}
-
}
-
*/
-
?>
#7 カスタムリクエストヘッダの追加
-
<?php
-
/**
-
* 例 48-7 カスタムリクエストヘッダの追加
-
*
-
* 以下の例では、HTTP ヘッダ X-PHP-Version が HTTP リクエストに追加されます。
-
* このヘッダの値は、 HTTP_Request インスタンスが動作している PHP インタプリタのバージョン文字列です。
-
*
-
* @link http://pear.php.net/manual/ja/package.http.http-request.headers.php
-
*/
-
require_once 'HTTP/Request2.php';
-
-
try {
-
$req = new HTTP_Request2("http://example.com/");
-
-
$response = $req->send();
-
-
} catch (HTTP_Request2_Exception $e) {
-
} catch (Exception $e) {
-
}
-
-
/*****************************
-
* HTTP_Request::addHeader()はHTTP_Request2->setHeader()に変更されました。
-
* 配列での一括指定や"header:value"形式の文字列でも受け付けるようになりました。
-
*****************************/
-
-
/*
-
require_once "HTTP/Request.php";
-
-
$req =& new HTTP_Request("http://example.com/");
-
$req->addHeader("X-PHP-Version", phpversion());
-
-
$response = $req->sendRequest();
-
-
if (PEAR::isError($response)) {
-
echo $response->getMessage();
-
} else {
-
echo $req->getResponseBody();
-
}
-
-
*/
-
?>
#8 匿名アクセスでプロキシサーバを利用する
-
<?php
-
/**
-
* 例 48-8 匿名アクセスでプロキシサーバを利用する
-
*
-
* この例では、プロキシサーバがポート 8080 で待機しており、
-
* example.com への外向き接続の代理(プロキシ)を行っている
-
* proxy.example.com というホスト名のマシンを 利用しようとしている、と想定しています。
-
*
-
* setProxy() の二番目のパラメータはオプションで、 デフォルトは 8080 です。
-
*
-
* @link http://pear.php.net/manual/ja/package.http.http-request.proxy-auth.php
-
*/
-
require_once 'HTTP/Request2.php';
-
-
try {
-
$req = new HTTP_Request2("http://example.com/");
-
'proxy_host' => 'proxy.example.com',
-
'proxy_port' => '8080',
-
));
-
// $req->setConfig('proxy_host', 'proxy.example.com');
-
// $req->setConfig('proxy_port', '8080');
-
-
$response = $req->send();
-
-
} catch (HTTP_Request2_Exception $e) {
-
} catch (Exception $e) {
-
}
-
-
/*****************************
-
* HTTP_Request::setProxy()に対応するメソッドはありません。
-
* 代わりにHTTP_Request2::setConfig()メソッドで1つ1つ設定するか、配列でまとめて設定します。
-
*****************************/
-
-
/*
-
require_once "HTTP/Request.php";
-
-
$req =& new HTTP_Request("http://example.com/");
-
$req->setProxy("proxy.example.com", 8080);
-
*/
-
?>
#9 プロキシ認証の利用
-
<?php
-
/**
-
* 例 48-9 プロキシ認証の利用
-
*
-
* これは、プロキシサーバでのユーザー認証のためのユーザー名/パスワードの
-
* タプルが提供されていることを除いて、上記と同じ例です:
-
* ユーザー名は johndoe で付随するパスワードは foo です。
-
*
-
* @link http://pear.php.net/manual/ja/package.http.http-request.proxy-auth.php
-
*/
-
require_once 'HTTP/Request2.php';
-
-
try {
-
$req = new HTTP_Request2("http://example.com/");
-
'proxy_host' => 'proxy.example.com',
-
'proxy_port' => '8080',
-
'proxy_user' => 'johndoe',
-
'proxy_password' => 'foo',
-
));
-
// $req->setConfig('proxy_host', 'proxy.example.com');
-
// $req->setConfig('proxy_port', '8080');
-
// $req->setConfig('proxy_user', 'johndoe');
-
// $req->setConfig('proxy_password', 'foo');
-
-
$response = $req->send();
-
-
} catch (HTTP_Request2_Exception $e) {
-
} catch (Exception $e) {
-
}
-
-
/*****************************
-
* HTTP_Request::setProxy()に対応するメソッドはありません。
-
* 代わりにHTTP_Request2::setConfig()メソッドで1つ1つ設定するか、配列でまとめて設定します。
-
*****************************/
-
-
/*
-
require_once "HTTP/Request.php";
-
-
$req =& new HTTP_Request("http://example.com/");
-
$req->setProxy("proxy.example.com", 8080, "johndoe", "foo");
-
-
*/
-
?>
[php][pear]HTTP_Request2のサンプル#2
この記事は、HTTP_Requestのマニュアルに載っている全サンプルコードをHTTP_Request2対応で書き換えてみる企画の2回目です。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
#3 Basic 認証
-
<?php
-
/**
-
* 例 48-3 Basic 認証
-
*
-
* @link http://pear.php.net/manual/ja/package.http.http-request.basic-auth.php
-
*/
-
require_once 'HTTP/Request2.php';
-
-
try {
-
$req = new HTTP_Request2("http://example.com/protected.html");
-
$req->setAuth("johndoe", "foo", HTTP_Request2::AUTH_BASIC);
-
$response = $req->send();
-
-
} catch (HTTP_Request2_Exception $e) {
-
} catch (Exception $e) {
-
}
-
-
/*****************************
-
* HTTP_Request::setBasicAuth()はHTTP_Request2->setAuth()に変更されました。
-
* 第3引数で認証の種類(AUTH_BASIC、AUTH_DIGEST)が指定できますが、
-
* Basic認証(AUTH_BASIC)がデフォルト値なので、今回は省略してもかまいません。
-
*****************************/
-
-
/*
-
require_once "HTTP/Request.php";
-
-
$req =& new HTTP_Request("http://example.com/protected.html");
-
$req->setBasicAuth("johndoe", "foo");
-
-
$response = $req->sendRequest();
-
-
if (PEAR::isError($response)) {
-
echo $response->getMessage();
-
} else {
-
echo $req->getResponseBody();
-
}
-
-
*/
-
?>
#4 リクエストにクッキーを追加する
-
<?php
-
/**
-
* 例 48-4 リクエストにクッキーを追加する
-
*
-
* この例では、version というクッキーが HTTP リクエストに追加されます。
-
* このクッキーの値は、 HTTP_Request2 インスタンスが動作している PHP インタプリタのバージョン文字列です。
-
*
-
* @link http://pear.php.net/manual/ja/package.http.http-request.cookie.php
-
*/
-
require_once 'HTTP/Request2.php';
-
-
try {
-
$req = new HTTP_Request2("http://example.com/");
-
$response = $req->send();
-
-
} catch (HTTP_Request2_Exception $e) {
-
} catch (Exception $e) {
-
}
-
-
/*****************************
-
* HTTP_Request::addCookie()はHTTP_Request2->addCookie()としてそのまま受け継がれています。
-
*****************************/
-
-
/*
-
require_once "HTTP/Request.php";
-
-
$req =& new HTTP_Request("http://example.com/");
-
$req->addCookie("version", phpversion());
-
-
$response = $req->sendRequest();
-
-
if (PEAR::isError($response)) {
-
echo $response->getMessage();
-
} else {
-
echo $req->getResponseBody();
-
}
-
*/
-
?>
#5 HTTP レスポンスからクッキーを読み込む
-
<?php
-
/**
-
* 例 48-5 HTTP レスポンスからクッキーを読み込む
-
*
-
* HTTP レスポンスのクッキーを読み込みは、以下の例で示されます。
-
*
-
* @link http://pear.php.net/manual/ja/package.http.http-request.cookie.php
-
*/
-
require_once 'HTTP/Request2.php';
-
-
try {
-
$req = new HTTP_Request2("http://example.com/");
-
$response = $req->send();
-
-
} catch (HTTP_Request2_Exception $e) {
-
} catch (Exception $e) {
-
}
-
-
/*****************************
-
* HTTP_Request::getResponseCookies()はHTTP_Request2_Response->getCookies()に変更されました。
-
*****************************/
-
-
/*
-
require_once "HTTP/Request.php";
-
-
$req =& new HTTP_Request("http://example.com/");
-
-
$response = $req->sendRequest();
-
-
if (PEAR::isError($response)) {
-
echo $response->getMessage();
-
} else {
-
print_r($req->getResponseCookies());
-
}
-
*/
-
?>
[php][pear]HTTP_Request2のサンプル#1
誰もが愛用しているPEAR::HTTP_Requestクラスですが、最近は後継のPEAR::HTTP_Request2クラスがリリースされてちょっと話題になりました。2009/01/21現在、バージョンは0.2.0(alpha)になっています。今後は利用される場面が増えてくると思いますので、HTTP_Requestのマニュアルに載っている全サンプルコードをHTTP_Request2対応で書き換えてみました。ソースコードやAPIリファレンスから推測して書いているので、100%正しいソースコードという保証はできませんが、そこら辺はあなたの技量でカバーしてください(笑。
とはいえ、HTTP_Request2の概要を知らない方も多いと思いますので、まずは0.2.0(alpha)時点でのHTTP_Request2の概要をまとめておきます。
- 特徴
- HTTP_RequestをPHP5対応に書きなおした
- アダプタに対応
- SocketAdapter : 従来のHTTP_Requestに相当する
- CurlAdapter : cURL拡張のラッパー
- MockAdapter : HTTP_Request2に依存しているパッケージのテスト用
- POSTリクエストにおいて以下の項目に対応
- データとファイルのアップロード
- クッキー認証
- プロキシ
- gzipとdeflateエンコーディング
- Observerを用いたリクエスト処理のモニタリング
- 動作環境
- PHP 5.1.4以上
- PEAR::Net_URL2
- 0.3.0のロードマップ
- Socketアダプタにて、HTTPSプロキシのサポート
- Socketアダプタにて、ダイジェスト認証のサポート
個人的にはcURL拡張以外でダイジェスト認証が使えるようになるのか?っていうのがちょっと期待しているところです。今まで探してもなかったもので・・・。
さてさて、本題のサンプルですね。HTTP_Requestのマニュアルには、全13個のサンプルソースコードが載っています。ブログが長くなるのも面倒なので、小分けにして紹介していきたいと思います。一応、記述の根拠となるように、変更点もコメントしておきましたので、参考にしてください。
#1 yahoo.com の内容を取得し、それを表示する
-
/**
-
* 例 48-1 yahoo.com の内容を取得し、それを表示する
-
*
-
* @link http://pear.php.net/manual/ja/package.http.http-request.intro.php
-
*/
-
require_once 'HTTP/Request2.php';
-
-
try {
-
$req = new HTTP_Request2("http://www.yahoo.com/");
-
$response = $req->send();
-
-
} catch (HTTP_Request2_Exception $e) {
-
} catch (Exception $e) {
-
}
-
-
/*****************************
-
* クラス名はHTTP_Request2になりました。
-
*
-
* コンストラクタも内部で使用するメソッドが例外を吐く可能性があるので、
-
* HTTP_Request2を使用する箇所全体をtry/catchで囲みましょう。
-
*
-
* HTTP_Request::sendRequest()はHTTP_Request2->send()に変更されました。
-
* このとき、返り値はHTTP_Request2_Responseクラスになります。
-
*
-
* HTTP_Request::getResponseBody()はHTTP_Request2_Response::getBody()に変更されました。
-
*****************************/
-
-
/*
-
///////////////////////////////
-
// 旧ソースコード
-
///////////////////////////////
-
require_once 'HTTP/Request.php';
-
-
$req =& new HTTP_Request("http://www.yahoo.com/");
-
if (!PEAR::isError($req->sendRequest())) {
-
echo $req->getResponseBody();
-
}
-
*/
-
?>
#2 二つの Web サイトの内容を取得し、一行で表示する
-
/**
-
* 例 48-2 二つの Web サイトの内容を取得し、一行で表示する
-
*
-
* この例では、二つの Web サイトの内容が取得され、表示されます。
-
* 前者は POST パラメータが処理されます。POST データスタックは、
-
* 二番目の Web サイトの内容が取得される前にクリアされます。
-
*
-
* @link http://pear.php.net/manual/ja/package.http.http-request.intro.php
-
*/
-
require_once 'HTTP/Request2.php';
-
-
try {
-
$req = new HTTP_Request2("http://www.php.net", HTTP_Request2::METHOD_POST);
-
// $req->setMethod(HTTP_Request2::METHOD_POST);
-
$req->addPostParameter('Foo', 'bar');
-
$response1 = $req->send();
-
-
$req->setMethod(HTTP_Request2::METHOD_GET);
-
$req->setURL("http://pear.php.net");
-
// $req->clearPostData();
-
$response2 = $req->send();
-
-
-
} catch (HTTP_Request2_Exception $e) {
-
} catch (Exception $e) {
-
}
-
-
/*****************************
-
* クラス名はHTTP_Request2になりました。
-
*
-
* HTTP_Request2::setMethod()は健在ですが、コンストラクタの第2引数にHTTPメソッドを指定するのが楽です。
-
*
-
* HTTP_Request::addPostData()はHTTP_Request2->addPostParameter()に変更されました。
-
* このとき、返り値はHTTP_Request2_Responseクラスになります。
-
*
-
* HTTP_Request2::setUrl()は今まで通りURLの文字列を受け付けますが、Net_URL2クラスも受け付けます。
-
*
-
* HTTP_Request::clearPostData()に当たるメソッドはHTTP_Request2には見受けられません。
-
* POSTメソッドの後にGETメソッドを送信しても、HTTP_Request2->addPostParameter()で設定したパラメータは
-
* 送信されることはありませんでした。(Wiresharkでパケットキャプチャをして確認済みです)
-
*****************************/
-
-
/*
-
require_once "HTTP/Request.php";
-
-
$req =& new HTTP_Request("http://www.php.net");
-
$req->setMethod(HTTP_REQUEST_METHOD_POST);
-
$req->addPostData("Foo", "bar");
-
if (!PEAR::isError($req->sendRequest())) {
-
$response1 = $req->getResponseBody();
-
} else {
-
$response1 = "";
-
}
-
-
$req->setMethod(HTTP_REQUEST_METHOD_GET);
-
$req->setURL("http://pear.php.net");
-
$req->clearPostData();
-
if (!PEAR::isError($req->sendRequest())) {
-
$response2 = $req->getResponseBody();
-
} else {
-
$response2 = "";
-
}
-
-
echo $response1;
-
echo $response2;
-
-
*/
-
?>
全記事へのリンク:
