====== PHP:HTTP, file ======= ===== リクエスト ===== ==== GET ==== $data = file_get_contents( "http://host/index.html" ); ==== POST ==== === HTTPヘッダを指定してPOST === #!/usr/bin/php "1", 'name' => "hoge", ); # HTTPヘッダ $headers = array( "User-Agent: PHP ". PHP_VERSION, 'Authorization: Basic '.base64_encode('user:pass'), ); $opt = array( 'http' => array( 'method' => 'POST', 'content' => http_build_query($data) 'header' => implode("\r\n", $headers), ) ); $res = file_get_contents( $url, false, stream_context_create( $opt )); # レスポンスの HTTPヘッダ一覧 print_r( $http_response_header ); # 1番目の HTTPヘッダを取得し、項目名と値を表示 $h = split( ':', $http_response_header[1] ); print $h[0] . "\n"; print ltrim($h[1]) . "\n"; # HTTP body を表示 print $res; ?> ===== エラー処理 ===== $res が FALSE の場合は、$http_response_header を参照する。\\ $http_response_header[0]: HTTPステータスが格納される。\\ $http_response_header[1] 以降は HTTPヘッダが格納される。\\ (例)\\ var_dump( $http_response_header); 結果\\ array(6) { [0]=> string(22) "HTTP/1.1 404 Not Found" [1]=> string(35) "Date: Thu, 28 Jul 2011 10:02:02 GMT" [2]=> string(120) "Server: Apache/2.0.55 (Unix) mod_ssl/2.0.55 OpenSSL/0.9.7c" [3]=> string(19) "Content-Length: 368" [4]=> string(17) "Connection: close" [5]=> string(43) "Content-Type: text/html; charset=iso-8859-1" }