[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();
-
echo $response->getBody();
-
-
} 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();
-
echo $response->getBody();
-
-
} 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());
-
}
-
*/
-
?>