0

次のコードで投稿リクエストを行います。

<?php
$url = 'http://www.example.com/';
$data = array('first-post-data' => 'data', 'second-post-data' => 'another-data');

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
        'user_agent' => 'Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0',

        // corporate proxy thing ...
        'proxy' => 'proxy-ip:proxy-port',
        'request_fulluri' => true,
    ),
);
$context  = stream_context_create($options);
$file = fopen($url, 'r', null, $context);
$content = stream_get_contents($file);

// Response headers:
echo '<pre>' . print_r(stream_get_meta_data($file), true) . '</pre>';

fclose($file);
echo $content;

関数 stream_get_meta_data を使用して、手作りの HTTP 要求から応答ヘッダーを簡単に取得できます。すべてのリクエストヘッダーを取得することは可能ですか? 組み込み関数はありますか?

編集

tcpflow で生の応答ヘッダーを取得しました: https://superuser.com/a/23187 ありがとう!

4

1 に答える 1

-1

http://php.net/manual/en/function.getallheaders.php

すべてのヘッダーを取得すると、うまくいくはずです。ただし、特定の php バージョンと、Web サーバーから php を実行する方法に依存することに注意してください。

例えば

PHP 5.4.0    This function became available under FastCGI. Previously, it was supported only when PHP was installed as an Apache module.

詳細は上記のリンクに含まれています。

于 2013-10-25T11:59:47.303 に答える