[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:
-
<?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:
-
<?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) {
-
echo $name . " = " . $value . "n";
-
}
-
-
} 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:
-
<?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();
-
echo $response->getHeader('Date');
-
-
} 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");
-
-
-
*/
-
?>