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 设置临时流上下文

        PHP: get_headers set temporary stream_context(PHP:get_headers 设置临时流上下文)

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                    <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'>

                      • <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>

                              <tbody id='cCJgD'></tbody>