PHP: get_headers set temporary stream_context

前端 未结 3 1558
孤街浪徒
孤街浪徒 2020-12-19 05:51

I guess PHP\'s get_headers does not allow for a context, so I have to change the default stream context to only get the HEAD of a request. This causes some issues with othe

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-19 06:01

    Instead of the accepted answer, I did the following, which will work in PHP 5.3 and above, though I haven't fully tested it. (There's also a stream_context_get_params($context) but I think this is enough.)

    $stream_context_defaults = stream_context_get_options(stream_context_get_default());
    stream_context_set_default(
        array(
            'http' => array(
                'method' => 'HEAD'
            )
        )
    );
    for ($i = 1; $i <= 10; $i++) {
        $headers = get_headers('http://www.example.org');
    }
    stream_context_set_default($stream_context_defaults); // reset to defaults
    

提交回复
热议问题