• <small id='wjJeb'></small><noframes id='wjJeb'>

    <tfoot id='wjJeb'></tfoot>
  • <legend id='wjJeb'><style id='wjJeb'><dir id='wjJeb'><q id='wjJeb'></q></dir></style></legend>

        <bdo id='wjJeb'></bdo><ul id='wjJeb'></ul>

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

      1. 如何使用 PHP 以编程方式检查有效(非死)链接?

        How do I check for valid (not dead) links programmatically using PHP?(如何使用 PHP 以编程方式检查有效(非死)链接?)
        • <bdo id='EtRsg'></bdo><ul id='EtRsg'></ul>

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

                  本文介绍了如何使用 PHP 以编程方式检查有效(非死)链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  给定一个网址列表,我想检查每个网址:

                  Given a list of urls, I would like to check that each url:

                  • 返回 200 OK 状态代码
                  • 在 X 时间内返回响应

                  最终目标是一个能够将 URL 标记为可能已损坏的系统,以便管理员可以查看它们.

                  The end goal is a system that is capable of flagging urls as potentially broken so that an administrator can review them.

                  脚本将用 PHP 编写,并且很可能每天通过 cron 运行.

                  The script will be written in PHP and will most likely run on a daily basis via cron.

                  该脚本将一次处理大约 1000 个网址.

                  The script will be processing approximately 1000 urls at a go.

                  问题有两部分:

                  • 这样的操作是否有任何重大问题?您遇到了哪些问题?
                  • 考虑到准确性和性能,在 PHP 中检查 url 状态的最佳方法是什么?

                  推荐答案

                  使用 PHP cURL 扩展.与 fopen() 不同,它还可以发出 HTTP HEAD 请求,这些请求足以检查 URL 的可用性并为您节省大量带宽,因为您不必下载整个页面进行检查.

                  Use the PHP cURL extension. Unlike fopen() it can also make HTTP HEAD requests which are sufficient to check the availability of a URL and save you a ton of bandwith as you don't have to download the entire body of the page to check.

                  作为起点,您可以使用如下函数:

                  As a starting point you could use some function like this:

                  function is_available($url, $timeout = 30) {
                      $ch = curl_init(); // get cURL handle
                  
                      // set cURL options
                      $opts = array(CURLOPT_RETURNTRANSFER => true, // do not output to browser
                                    CURLOPT_URL => $url,            // set URL
                                    CURLOPT_NOBODY => true,         // do a HEAD request only
                                    CURLOPT_TIMEOUT => $timeout);   // set timeout
                      curl_setopt_array($ch, $opts); 
                  
                      curl_exec($ch); // do it!
                  
                      $retval = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200; // check if HTTP OK
                  
                      curl_close($ch); // close handle
                  
                      return $retval;
                  }
                  

                  但是,有很多可能的优化:您可能想要重新使用 cURL 实例,如果每个主机检查多个 URL,甚至可以重新使用连接.

                  However, there's a ton of possible optimizations: You might want to re-use the cURL instance and, if checking more than one URL per host, even re-use the connection.

                  哦,这段代码确实严格检查 HTTP 响应代码 200.它不遵循重定向 (302) -- 但也有一个 cURL 选项.

                  Oh, and this code does check strictly for HTTP response code 200. It does not follow redirects (302) -- but there also is a cURL-option for that.

                  这篇关于如何使用 PHP 以编程方式检查有效(非死)链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 的问题)

                        <tbody id='jZ2FS'></tbody>

                        <tfoot id='jZ2FS'></tfoot>

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

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