Note: the Classes are Only available in PHP5, the functions however work in both PHP4 and PHP5.
LV. HTTP
導入
この HTTP 拡張モジュールの狙いは、PHP アプリケーションのために便利で強力な機能を提供することです。
HTTP の URL、日付、リダイレクト、ヘッダおよびメッセージを 使いやすくし、クライアントの希望する言語および文字セットの ネゴシエーション手段を提供します。 また、任意のデータを送信する際にキャッシュやリジュームの機能を もたせることができます。
CURL サポート込みでビルドされている場合は、 強力なリクエスト機能を提供します。PHP 5 以降では、 複数のリクエストを平行して実行することができます。
このマニュアルでは、API リファレンスに加えて インストールや設定の方法、定義済みのグローバル定数などを 以下の節で説明しています。
| インストール |
| 設定 |
| グローバル定数 |
| リソース型 |
クラス
以下のクラスが定義されています。この拡張モジュールが PHP 組み込みでコンパイルされているか、実行時に動的にロードされている場合のみ 使用可能です。
HttpResponse クラスは PHP v5.1 以降が必要です。
その他のクラスは PHP v5.0 以降で使用できます。
注意: しかし、PHP v5.0 ではいくつか使用できないメソッドがあることに注意しましょう。
HttpMessage |
HttpQueryString |
HttpDeflateStream |
HttpInflateStream |
HttpRequest |
HttpRequestPool |
HttpResponse |
関数
このページですでに取り上げられた組み込みの HTTP 関連関数については ネットワーク関数 に説明があります。
以下の関数は、HTTP モジュールを必要としません。 header()、headers_list()、headers_sent()、 setcookie() そして setrawcookie()。
| http_build_cookie() |
| http_date() |
| http_get_request_body_stream() |
| http_get_request_body() |
| http_get_request_headers() |
| http_match_etag() |
| http_match_modified() |
| http_match_request_header() |
| http_support() |
| http_redirect() |
| http_send_content_disposition() |
| http_send_content_type() |
| http_send_data() |
| http_send_file() |
| http_send_last_modified() |
| http_send_status() |
| http_send_stream() |
| http_throttle() |
目次
- インストール — HTTP 拡張モジュールのインストール
- 設定 — http モジュールの設定ディレクティブ
- リソース — HTTP 拡張モジュールが作成するリソース
- 定数 — http モジュールの定義済みの定数
- リクエストのオプション — HttpRequest クラスおよびリクエスト関数で使用するオプション
- HttpMessage — HTTP メッセージクラス
- HttpMessage::__construct — HttpMessage のコンストラクタ
- HttpMessage::factory — 文字列から HttpMessage を作成する
- HttpMessage::fromEnv — 環境から HttpMessage を作成する
- HttpMessage::fromString — 文字列から HttpMessage を作成する
- HttpMessage::toString — 文字列表現を取得する
- HttpMessage::toMessageTypeObject — メッセージの型に応じた HTTP オブジェクトを作成する
- HttpMessage::guessContentType — content type を推測する
- HttpMessage::detach — HttpMessage をデタッチする
- HttpMessage::prepend — メッセージを先頭に追加する
- HttpMessage::reverse — メッセージチェインを逆順にする
- HttpMessage::send — メッセージを送信する
- HttpMessage::getParentMessage — 親メッセージを取得する
- HttpMessage::getType — メッセージの型を取得する
- HttpMessage::setType — メッセージの型を設定する
- HttpMessage::getHttpVersion — HTTP バージョンを取得する
- HttpMessage::setHttpVersion — HTTP バージョンを設定する
- HttpMessage::getHeaders — メッセージのヘッダを取得する
- HttpMessage::getHeader — ヘッダを取得する
- HttpMessage::addHeaders — ヘッダを追加する
- HttpMessage::setHeaders — ヘッダを設定する
- HttpMessage::getBody — メッセージの本文を取得する
- HttpMessage::setBody — メッセージの本文を設定する
- HttpMessage::getRequestMethod — リクエストメソッドを取得する
- HttpMessage::setRequestMethod — リクエストメソッドを設定する
- HttpMessage::getRequestUrl — リクエスト URL を取得する
- HttpMessage::setRequestUrl — リクエスト URL を設定する
- HttpMessage::getResponseCode — レスポンスコードを取得する
- HttpMessage::setResponseCode — レスポンスコードを設定する
- HttpMessage::getResponseStatus — レスポンスのステータスを取得する
- HttpMessage::setResponseStatus — レスポンスのステータスを設定する
- HttpQueryString — HTTP クエリ文字列クラス
- HttpQueryString::__construct — HttpQueryString のコンストラクタ
- HttpQueryString::singleton — HttpQueryString のシングルトン
- HttpQueryString::get — クエリ文字列 (の一部) を取得する
- HttpQueryString::mod — クエリ文字列の複製を変更する
- HttpQueryString::set — クエリ文字列パラメータを設定する
- HttpQueryString::toArray — クエリ文字列を配列で取得する
- HttpQueryString::toString — クエリ文字列を取得する
- HttpQueryString::xlate — クエリ文字列の文字セットを変更する
- HttpDeflateStream — HTTP 圧縮ストリームクラス
- HttpDeflateStream::factory — HttpDeflateStream クラスのファクトリ
- HttpDeflateStream::__construct — HttpDeflateStream クラスのコンストラクタ
- HttpDeflateStream::update — 圧縮ストリームを更新する
- HttpDeflateStream::flush — 圧縮ストリームをフラッシュする
- HttpDeflateStream::finish — 圧縮ストリームを終了する
- HttpInflateStream — HTTP 展開ストリーム
- HttpInflateStream::factory — HttpInflateStream クラスのファクトリ
- HttpInflateStream::__construct — HttpInflateStream クラスのコンストラクタ
- HttpInflateStream::update — 展開ストリームを更新する
- HttpInflateStream::flush — 展開ストリームをフラッシュする
- HttpInflateStream::finish — 展開ストリームを終了する
- HttpRequest — HTTP リクエストクラス
- HttpRequest::addCookies — クッキーを追加する
- HttpRequest::addHeaders — ヘッダを追加する
- HttpRequest::addPostFields — POST フィールドを追加する
- HttpRequest::addPostFile — POST ファイルを追加する
- HttpRequest::addPutData — PUT データを追加する
- HttpRequest::addQueryData — クエリデータを追加する
- HttpRequest::addRawPostData — 生の POST データを追加する
- HttpRequest::addSslOptions — SSL オプションを追加する
- HttpRequest::clearHistory — 履歴を消去する
- HttpRequest::__construct — HttpRequest のコンストラクタ
- HttpRequest::enableCookies — クッキーを有効にする
- HttpRequest::getContentType — content type を取得する
- HttpRequest::getCookies — クッキーを取得する
- HttpRequest::getHeaders — ヘッダを取得する
- HttpRequest::getHistory — 履歴を取得する
- HttpRequest::getMethod — メソッドを取得する
- HttpRequest::getOptions — オプションを取得する
- HttpRequest::getPostFields — POST フィールドを取得する
- HttpRequest::getPostFiles — POST ファイルを取得する
- HttpRequest::getPutData — PUT データを取得する
- HttpRequest::getPutFile — PUT ファイルを取得する
- HttpRequest::getQueryData — クエリデータを取得する
- HttpRequest::getRawPostData — 生の POST データを取得する
- HttpRequest::getRawRequestMessage — 名前のリクエストメッセージを取得する
- HttpRequest::getRawResponseMessage — 生のレスポンスメッセージを取得する
- HttpRequest::getRequestMessage — リクエストメッセージを取得する
- HttpRequest::getResponseBody — レスポンスの本文を取得する
- HttpRequest::getResponseCode — レスポンスコードを取得する
- HttpRequest::getResponseCookies — レスポンスのクッキーを取得する
- HttpRequest::getResponseData — レスポンスデータを取得する
- HttpRequest::getResponseHeader — レスポンスヘッダを取得する
- HttpRequest::getResponseInfo — レスポンスの情報を取得する
- HttpRequest::getResponseMessage — レスポンスメッセージを取得する
- HttpRequest::getResponseStatus — レスポンスのステータスを取得する
- HttpRequest::getSslOptions — ssl オプションを取得する
- HttpRequest::getUrl — url を取得する
- HttpRequest::resetCookies — クッキーをリセットする
- HttpRequest::send — リクエストを送信する
- HttpRequest::setContentType — content type を設定する
- HttpRequest::setCookies — クッキーを設定する
- HttpRequest::setHeaders — ヘッダを設定する
- HttpRequest::setMethod — メソッドを設定する
- HttpRequest::setOptions — オプションを設定する
- HttpRequest::setPostFields — POST フィールドを設定する
- HttpRequest::setPostFiles — POST ファイルを設定する
- HttpRequest::setPutData — PUT データを設定する
- HttpRequest::setPutFile — PUT ファイルを設定する
- HttpRequest::setQueryData — クエリデータを設定する
- HttpRequest::setRawPostData — 生の POST データを設定する
- HttpRequest::setSslOptions — SSL オプションを設定する
- HttpRequest::setUrl — URL を設定する
- HttpRequestPool — HTTP リクエストプールクラス
- HttpRequestPool::attach — HttpRequest をアタッチする
- HttpRequestPool::__construct — HttpRequestPool のコンストラクタ
- HttpRequestPool::__destruct — HttpRequestPool のデストラクタ
- HttpRequestPool::detach — HttpRequest をデタッチする
- HttpRequestPool::getAttachedRequests — アタッチされているリクエストを取得する
- HttpRequestPool::getFinishedRequests — 終了したリクエストを取得する
- HttpRequestPool::reset — リクエストプールをリセットする
- HttpRequestPool::send — すべてのリクエストを送信する
- HttpRequestPool::socketPerform — ソケットアクションを実行する
- HttpRequestPool::socketSelect — ソケットの選択を実行する
- HttpResponse — HTTP レスポンスクラス
- HttpResponse::capture — スクリプトの出力を取り込む
- HttpResponse::getBufferSize — バッファサイズを取得する
- HttpResponse::getCacheControl — cache control を取得する
- HttpResponse::getCache — キャッシュを取得する
- HttpResponse::getContentDisposition — content disposition を取得する
- HttpResponse::getContentType — content type を取得する
- HttpResponse::getData — データを取得する
- HttpResponse::getETag — ETag を取得する
- HttpResponse::getFile — ファイルを取得する
- HttpResponse::getGzip — gzip を取得する
- HttpResponse::getHeader — ヘッダを取得する
- HttpResponse::getLastModified — 最終更新日時を取得する
- HttpResponse::getStream — ストリームを取得する
- HttpResponse::getThrottleDelay — throttle delay を取得する
- HttpResponse::getRequestBody — リクエストの本文を取得する
- HttpResponse::getRequestBodyStream — リクエストの本文をストリームとして取得する
- HttpResponse::getRequestHeaders — リクエストのヘッダを取得する
- HttpResponse::guessContentType — content type を推測する
- HttpResponse::redirect — リダイレクトする
- HttpResponse::send — レスポンスを送信する
- HttpResponse::setBufferSize — バッファサイズを設定する
- HttpResponse::setCacheControl — cache control を設定する
- HttpResponse::setCache — キャッシュを設定する
- HttpResponse::setContentDisposition — content disposition を設定する
- HttpResponse::setContentType — content type を設定する
- HttpResponse::setData — データを設定する
- HttpResponse::setETag — ETag を設定する
- HttpResponse::setFile — ファイルを設定する
- HttpResponse::setGzip — gzip を設定する
- HttpResponse::setHeader — ヘッダを設定する
- HttpResponse::setLastModified — 最終更新日時を設定する
- HttpResponse::setStream — ストリームを設定する
- HttpResponse::setThrottleDelay — throttle delay を設定する
- HttpResponse::status — HTTP レスポンスステータスを送信する
- http_cache_etag — ETag でキャッシュする
- http_cache_last_modified — 最終更新日時でキャッシュする
- http_chunked_decode — chunked-encoded データをデコードする
- http_deflate — データを圧縮する
- http_inflate — データを展開する
- http_get_request_body_stream — リクエストの本文をストリームとして取得する
- http_get_request_body — リクエストの本文を文字列として取得する
- http_get_request_headers — リクエストヘッダを配列として取得する
- http_date — HTTP の RFC に準拠した日付を作成する
- http_support — 組み込みの HTTP サポートを調べる
- http_match_etag — ETag を比較する
- http_match_modified — 最終更新日時を比較する
- http_match_request_header — 任意のヘッダを比較する
- http_build_cookie — クッキー文字列を作成する
- http_negotiate_charset — クライアントが希望している文字セットを選択する
- http_negotiate_content_type — クライアントが希望している content type を選択する
- http_negotiate_language — クライアントが希望している言語を選択する
- ob_deflatehandler — 圧縮出力ハンドラ
- ob_etaghandler — ETag 出力ハンドラ
- ob_inflatehandler — 展開出力ハンドラ
- http_parse_cookie — HTTP クッキーをパースする
- http_parse_headers — HTTP ヘッダをパースする
- http_parse_message — HTTP メッセージをパースする
- http_parse_params — パラメータリストをパースする
- http_persistent_handles_count — 持続ハンドルの状況
- http_persistent_handles_ident — 持続ハンドルの ident を取得/設定する
- http_persistent_handles_clean — 持続ハンドルを消去する
- http_get — GET リクエストを実行する
- http_head — HEAD リクエストを実行する
- http_post_data — エンコードされたデータを使用して POST リクエストを実行する
- http_post_fields — エンコードされる前のデータを使用して POST リクエストを実行する
- http_put_data — データを使用して PUT リクエストを実行する
- http_put_file — ファイルを使用して PUT リクエストを実行する
- http_put_stream — ストリームを使用して PUT リクエストを実行する
- http_request_method_exists — リクエストメソッドが存在するかどうかを調べる
- http_request_method_name — リクエストメソッド名を取得する
- http_request_method_register — リクエストメソッドを登録する
- http_request_method_unregister — リクエストメソッドの登録を解除する
- http_request — 独自のリクエストを実行する
- http_request_body_encode — リクエスト本文をエンコードする
- http_redirect — HTTP リダイレクトを発行する
- http_send_content_disposition — Content-Disposition を送信する
- http_send_content_type — Content-Type を送信する
- http_send_data — 任意のデータを送信する
- http_send_file — ファイルを送信する
- http_send_last_modified — Last-Modified を送信する
- http_send_status — HTTP レスポンスステータスを送信する
- http_send_stream — ストリームを送信する
- http_throttle — HTTP 抑止処理
- http_build_str — クエリ文字列を組み立てる
- http_build_url — URL を組み立てる
HTTP
11-Aug-2006 01:19
15-Jan-2006 06:01
If you want to make outgoing http connections with php, concider the curl extension.
01-Dec-2005 12:57
Actually, if you want to redirect a user why let HTML or JavaScript do it? Simply do this:
header("Location: http://www.example.com/");
08-Jul-2004 03:39
Regarding what the guy before said. We've experienced problems where certain firewalls have encrypted the HTTP_REFERER meaning that it doesnt always contain the place you've come from.
Better to track where the user has come from either in a form post or in the url.
in reference to toashwinisidhu's and breaker's note, a more effective way would be to use meta-tag redirect, for example.
<?php
$url = "http://somesite.com/index.php"; // target of the redirect
$delay = "3"; // 3 second delay
echo '<meta http-equiv="refresh" content="'.$delay.';url='.$url.'">';
?>
The meta goes in the head of the HTML.
This method does not require javascript and is supported by most browsers and is rarely, if ever, filterd out.
21-Apr-2004 07:55
The method given below may not sometimes work.
The following method has always worked with me:
just put the following 3 lines in your PHP code
?>
<body onload=setTimeout("location.href='$url'",$sec)>
<?PHP
-------?>
$sec is the time in second after which the browser would automatically go to the url. Set it to 0 if you do not want to give any time.
You can use this function on the events of various html/form objects (eg.-onclick for button).eg.
<input type=button value="Go to Php.net" onclick=setTimeout("location.href='php.net'",0)>
Use this to one step back
<input type="button" value="Back" onclick=history.go(-1)>
05-Jan-2001 01:37
$HTTP_RAW_POST_DATA --
You'll usually access variables from forms sent via POST method by just accessing the associated PHP global variable.
However, if your POST data is not URI encoded (i.e., custom application that's not form-based) PHP won't parse the data into nice variables for you. You will need to use $HTTP_RAW_POST_DATA to access the raw data directly. (This should return a copy of the data given to the PHP process on STDIN; note that you wan't be able to open STDIN and read it yourself because PHP already did so itself.)