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

        <tfoot id='OwFjk'></tfoot>
      1. <small id='OwFjk'></small><noframes id='OwFjk'>

      2. <legend id='OwFjk'><style id='OwFjk'><dir id='OwFjk'><q id='OwFjk'></q></dir></style></legend>
        • <bdo id='OwFjk'></bdo><ul id='OwFjk'></ul>

      3. PHP:get_headers 设置临时流上下文

        时间:2023-10-15

            <tbody id='7Awcb'></tbody>
              <bdo id='7Awcb'></bdo><ul id='7Awcb'></ul>

              <tfoot id='7Awcb'></tfoot>

              <small id='7Awcb'></small><noframes id='7Awcb'>

              <legend id='7Awcb'><style id='7Awcb'><dir id='7Awcb'><q id='7Awcb'></q></dir></style></legend>
              <i id='7Awcb'><tr id='7Awcb'><dt id='7Awcb'><q id='7Awcb'><span id='7Awcb'><b id='7Awcb'><form id='7Awcb'><ins id='7Awcb'></ins><ul id='7Awcb'></ul><sub id='7Awcb'></sub></form><legend id='7Awcb'></legend><bdo id='7Awcb'><pre id='7Awcb'><center id='7Awcb'></center></pre></bdo></b><th id='7Awcb'></th></span></q></dt></tr></i><div id='7Awcb'><tfoot id='7Awcb'></tfoot><dl id='7Awcb'><fieldset id='7Awcb'></fieldset></dl></div>
                • 本文介绍了PHP:get_headers 设置临时流上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我猜 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 设置临时流上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:fgets() 和 fread() - 有什么区别? 下一篇:echo 是否等于 fputs( STDout )?

                  相关文章

                  <legend id='cCJgD'><style id='cCJgD'><dir id='cCJgD'><q id='cCJgD'></q></dir></style></legend>

                  <tfoot id='cCJgD'></tfoot>
                      <bdo id='cCJgD'></bdo><ul id='cCJgD'></ul>

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

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