从重定向页面获取标题信息

时间:2014-02-06 18:28:03

标签: php arrays http header http-headers

所以基本上我试图从几个页面的标题中获取一些信息,并且这样做我正在使用get_headers()。现在,只要http响应为200,那就没问题了,数组显示的信息比如Content-Type是一个关键,这对我来说足够好,我可以检查它的价值。

问题显示当响应代码为301时,在这种情况下,数组变成了一个多层数组,这就是我的结束......我无法弄清楚如何轻松获取Content-Type值(I只要我明显得到这个值,就不要在乎它是什么键。

我想我可以运行几个循环来逐个检查数组值,但这似乎是一个巨大的脚本,可以解决这个小问题。

所以我的问题是:无论如何要么压缩我得到的数组作为输出(下面的数组),还是有一种原生的方式来寻找多层数组中的正则表达式?

301:

array(12) {
    [0] = > string(30)"HTTP/1.0 301 Moved Permanently" ["Date"] = > array(2) {
        [0] = > string(29)"Thu, 06 Feb 2014 13:04:19 GMT" [1] = > string(29)"Thu, 06 Feb 2014 13:04:20 GMT"
    }["Server"] = > array(2) {
        [0] = > string(6)"Apache" [1] = > string(6)"Apache"
    }["Vary"] = > array(2) {
        [0] = > string(22)"Cookie,Accept-Encoding" [1] = > string(22)"Accept-Encoding,Cookie"
    }["Set-Cookie"] = > array(3) {
        [0] = > string(200)"wordpress_27e1f2dfbb852bd4e4295b075bfa297f=%7C1392901460%7C6714e772e8aadf7ef19238b8c45faaf5; expires=Fri, 21-Feb-2014 01:04:20 GMT; path=/wp-content/plugins; domain=www.domain.com; httponly" [1] = > string(190)"wordpress_27e1f2dfbb852bd4e4295b075bfa297f=%7C1392901460%7C6714e772e8aadf7ef19238b8c45faaf5; expires=Fri, 21-Feb-2014 01:04:20 GMT; path=/wp-admin; domain=www.domain.com; httponly" [2] = > string(192)"wordpress_logged_in_27e1f2dfbb852bd4e4295b075bfa297f=%7C1392901460%7Ccd60c9eaeab6cf8e770eb7a121440e5f; expires=Fri, 21-Feb-2014 01:04:20 GMT; path=/; domain=www.domain.com; httponly"
    }["X-Pingback"] = > string(43)"http://www.domain.com/xmlrpc.php" ["Location"] = > string(38)"http://www.domain.com/acces" ["Connection"] = > array(2) {
        [0] = > string(5)"close" [1] = > string(5)"close"
    }["Content-Type"] = > array(2) {
        [0] = > string(24)"text/html; charset=UTF-8" [1] = > string(24)"text/html; charset=UTF-8"
    }[1] = > string(15)"HTTP/1.1 200 OK" ["Cache-Control"] = > string(26)"max-age=3, must-revalidate" ["WP-Super-Cache"] = > string(31)"Served supercache file from PHP"
}

万一你需要它,这是我的代码:

public function checkhttp() {
        if ($_SESSION['started'] == 1) {
            $headers = @get_headers($_SESSION['next_page'], 1);
        //$headers['Content-Type'] will return the expected value if response is 200    
            if(!preg_match('/(200|202|300|301|302)/', $headers[0]) || !preg_match('/(html)/', $headers['Content-Type'])){
                $_SESSION['next_page'] = $_SESSION['page_list'][0];
                $_SESSION['page_list_done'][] = $_SESSION['page_list'][0] . ' ----- FAIL TO CONNECT. Debug purpose : ' . $headers[0] . $headers['Content-Type'];
                array_splice($_SESSION['page_list'], 0, 1);
                foreach ($_SESSION['page_list_done'] as $value) {
                    echo $value . '<br />';
                }
                echo $headers[0] . '<br />';
                var_dump($headers);
                header( "refresh:2;url=" . $_SERVER['PHP_SELF'] );
                exit;
            }
        }
    }

谢谢

1 个答案:

答案 0 :(得分:0)

对于那些自问相同问题的人,您要查找的变量是$headers[Content-Type][0]