HTTP と HTTPS

PHP 3, PHP 4, PHP 5。PHP 4.3.0以降ではhttps://

HTTP 1.0によりHTTP GETメソッドを用いてファイル/リソースに読み込み のみのアクセスが可能です。仮想ホストにホスト名でアクセスするために、 Host: ヘッダが送信されます。 iniファイルまたはストリームコンテキストにより、 user_agent文字列を設定してい る場合、リクエストの中にも含まれます。

警告

IISのような、いくつかの標準に 対応してないWebサーバーは、PHPに警告を発生させるような手順でデータを送信します。 このようなサーバーを使用する場合は、 error_reportingを警告を発生しないレベルまで小さくする必要があります。 PHP 4.3.7 以降では、https:// ラッパーでストリームをオープンする際に バグがある IISサーバーソフトウエアを検出することができ、この警告を抑制することができます。 あなたが ssl:// ソケットを作性するために fsockopen() を使用している場合、 自らこの警告を検出し、抑制する必要があります。

PHP 4.0.5以降、リダイレクトがサポートされています。これ以前のバー ジョンを使用している場合、URLの末尾にスラッシュを付ける必要があります。 (全てのリダイレクトが処理された後に)ドキュメント取得元のリソースのURLを知ることが 重要な場合、ストリームから返された一連のレスポンスヘッダを処理する必要があります。

<?php
$url
= 'http://www.example.com/redirecting_page.php';

$fp = fopen($url, 'r');

/* PHP 4.3.0の場合、stream_get_meta_data()の代わりに
   $http_response_header を使用してください */
$meta_data = stream_get_meta_data($fp);
foreach(
$meta_data['wrapper_data'] as $response) {

  
/* リダイレクトされているか? */
  
if (substr(strtolower($response), 0, 10) == 'location: ') {
    
/* update $url with where we were redirected to */
    
$url = substr($response, 10);
  }

}

?>

ストリームにより、リソースのbodyにアクセスすることが できます。ヘッダは、$http_response_header変数に保存されます。 PHP 4.3.0以降は、ヘッダは stream_get_meta_data() により取得できます。

HTTP接続は読み込みのみ可で、HTTPリソースにデータを書き込んだり、ファイルをコピーすることはできません。

注意: HTTPS は、PHP 4.3.0以降でOpenSSLのサポートを組み込んだ場合にサポートされます。

表 L-2. ラッパーの概要

属性サポートの可否
allow_url_fopenの制限を受けるかYes
読み込み可Yes
書き込み可No
追記可能No
同時読み書き可N/A
stat()をサポートNo
unlink()をサポートNo
rename()をサポートNo
mkdir()をサポートNo
rmdir()をサポートNo

表 L-3. コンテクストのオプション (PHP 5.0.0以降)

名前使用法デフォルト
method GET, POST, or any other HTTP method supported by the remote server. GET
headerAdditional headers to be sent during request. Values in this option will override other values (such as User-agent:, Host:, and Authentication:).  
user_agentValue to send with User-Agent: header. This value will only be used if user-agent is not specified in the header context option above. php.ini setting: user_agent
content ヘッダの後に送信する追加データ。通常、POSTまたはPUTリクエストの際に使用される。  
proxy URI specifying address of proxy server. (e.g. tcp://proxy.example.com:5100 ).  
request_fulluri When set to TRUE, the entire URI will be used when constructing the request. (i.e. GET http://www.example.com/path/to/file.html HTTP/1.0). While this is a non-standard request format, some proxy servers require it. FALSE

Underlying socket stream context options: Additional context options may be supported by the underlying transport For http:// streams, refer to context options for the tcp:// transport. For https:// streams, refer to context options for the ssl:// transport.