<i id='Wdhw2'><tr id='Wdhw2'><dt id='Wdhw2'><q id='Wdhw2'><span id='Wdhw2'><b id='Wdhw2'><form id='Wdhw2'><ins id='Wdhw2'></ins><ul id='Wdhw2'></ul><sub id='Wdhw2'></sub></form><legend id='Wdhw2'></legend><bdo id='Wdhw2'><pre id='Wdhw2'><center id='Wdhw2'></center></pre></bdo></b><th id='Wdhw2'></th></span></q></dt></tr></i><div id='Wdhw2'><tfoot id='Wdhw2'></tfoot><dl id='Wdhw2'><fieldset id='Wdhw2'></fieldset></dl></div>
      1. <small id='Wdhw2'></small><noframes id='Wdhw2'>

        <legend id='Wdhw2'><style id='Wdhw2'><dir id='Wdhw2'><q id='Wdhw2'></q></dir></style></legend>
          <bdo id='Wdhw2'></bdo><ul id='Wdhw2'></ul>
      2. <tfoot id='Wdhw2'></tfoot>

        PHP:get_headers 设置临时 stream_context

        时间:2023-11-30

        1. <legend id='b0MAA'><style id='b0MAA'><dir id='b0MAA'><q id='b0MAA'></q></dir></style></legend>
            • <bdo id='b0MAA'></bdo><ul id='b0MAA'></ul>

              <small id='b0MAA'></small><noframes id='b0MAA'>

              <i id='b0MAA'><tr id='b0MAA'><dt id='b0MAA'><q id='b0MAA'><span id='b0MAA'><b id='b0MAA'><form id='b0MAA'><ins id='b0MAA'></ins><ul id='b0MAA'></ul><sub id='b0MAA'></sub></form><legend id='b0MAA'></legend><bdo id='b0MAA'><pre id='b0MAA'><center id='b0MAA'></center></pre></bdo></b><th id='b0MAA'></th></span></q></dt></tr></i><div id='b0MAA'><tfoot id='b0MAA'></tfoot><dl id='b0MAA'><fieldset id='b0MAA'></fieldset></dl></div>
                <tbody id='b0MAA'></tbody>

              • <tfoot id='b0MAA'></tfoot>
                  本文介绍了PHP:get_headers 设置临时 stream_context的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我猜 PHP 的 get_headers 不允许上下文,所以我必须更改默认流上下文以仅获取请求的 HEAD.这会导致页面上的其他请求出现一些问题.我似乎无法弄清楚如何重置默认流上下文.我正在尝试类似:

                  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 other requests on the page. I can't seem to figure out how to reset the default stream context. I'm trying something like:

                  $default = stream_context_get_default(); //Get default stream context so we can reset it
                  stream_context_set_default( //Only fetch the HEAD
                        array(
                      'http' => array(
                         'method' => 'HEAD'
                       )
                    )
                  );
                  $headers = get_headers($url, 1); //Url can be whatever you want it to be
                  //var_dump($headers);
                  var_dump($default);
                  stream_context_set_default($default); //This doesn't work as it expects an array and not a resource pointer
                  

                  有人知道解决这个问题的方法吗?

                  Does anyone know a fix for this?

                  我知道有人建议使用 Curl,但我宁愿不使用这个.谢谢!

                  I know it has been suggested to use Curl, but I would rather not for this one. Thanks!

                  推荐答案

                  我最终使用了 stream_get_meta_data() 函数获取 HTTP 标头.

                  I ended up using the stream_get_meta_data() function to get the HTTP headers.

                  我是这样实现的:

                  function get_headers_with_stream_context($url, $context, $assoc = 0) {
                      $fp = fopen($url, 'r', null, $context);
                      $metaData = stream_get_meta_data($fp);
                      fclose($fp);
                  
                      $headerLines = $metaData['wrapper_data'];
                  
                      if(!$assoc) return $headerLines;
                  
                      $headers = array();
                      foreach($headerLines as $line) {
                          if(strpos($line, 'HTTP') === 0) {
                              $headers[0] = $line;
                              continue;
                          }
                  
                          list($key, $value) = explode(': ', $line);
                          $headers[$key] = $value;
                      }
                  
                      return $headers;
                  }
                  

                  这样称呼,

                  $context = stream_context_create(array('http' => array('method' => 'HEAD')));
                  $headers = get_headers_with_stream_context($url, $context, 1);
                  

                  它在保持标准 stream_context 不变的情况下为您提供您所追求的.

                  it gives you what you're after while leaving the standard stream_context unmodified.

                  请注意,如果传递的不是 http url,此函数将失败.

                  Please note that this function will fail if passed anything other than an http url.

                  似乎有一个 功能请求 用于 get_headers() 的附加参数,但错误在我写这篇文章时跟踪器已关闭,所以我无法在那里检查其他解决方案.

                  There seems to be a feature request for an additional argument for get_headers(), but the bug tracker is down as I'm writing this, so I can't check for other solutions there.

                  这篇关于PHP:get_headers 设置临时 stream_context的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Laravel:如何在重定向到任何网址时设置自定义标头 下一篇:PHP 位置标头忽略哈希

                  相关文章

                    <legend id='69Jxc'><style id='69Jxc'><dir id='69Jxc'><q id='69Jxc'></q></dir></style></legend>

                      <bdo id='69Jxc'></bdo><ul id='69Jxc'></ul>
                    <i id='69Jxc'><tr id='69Jxc'><dt id='69Jxc'><q id='69Jxc'><span id='69Jxc'><b id='69Jxc'><form id='69Jxc'><ins id='69Jxc'></ins><ul id='69Jxc'></ul><sub id='69Jxc'></sub></form><legend id='69Jxc'></legend><bdo id='69Jxc'><pre id='69Jxc'><center id='69Jxc'></center></pre></bdo></b><th id='69Jxc'></th></span></q></dt></tr></i><div id='69Jxc'><tfoot id='69Jxc'></tfoot><dl id='69Jxc'><fieldset id='69Jxc'></fieldset></dl></div>

                    1. <tfoot id='69Jxc'></tfoot>
                    2. <small id='69Jxc'></small><noframes id='69Jxc'>