PHP获取域重定向数组

时间:2017-04-16 11:49:27

标签: php arrays

我正在寻找一个PHP函数,我插入一个域,数组输出响应代码。

例如,如果domain123.com执行302重定向到www.domain123.com,然后设置为301重定向到https://www.domain123.com,则通过PHP我得到数组或输出如下:

domain123.com [302]
www.domain123.com [301]
https://www.domain123.com [200]

有人可以帮忙吗?

谢谢:)

2 个答案:

答案 0 :(得分:0)

我想出了类似的东西:

if let budget = budgetData.first {
    budget.attributeName
}

给你:

<?php

$url = "http://google.co.uk";

$redirects = [];

getHeaders($url);

function getHeaders($url) {
    echo 'getting headers for: ' . $url . PHP_EOL;
    global $redirects;
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_NOBODY, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, true);

    $header = curl_exec($curl);
    $info = curl_getinfo($curl);
    curl_close($curl);

    if (in_array($info['http_code'], [301,302])) {
        $redirects[] = $info;
        getHeaders($info['redirect_url']);
    }
    if ($info['http_code'] == 200) {
        $redirects[] = $info;
    }
}


//print_r($redirects);

foreach ($redirects as $redirect) {
    printf("%s [%d]%s", $redirect['url'], $redirect['http_code'], PHP_EOL);
}

希望这会给你一个开始,我知道代码有一些不好的逻辑,这里没有处理403,404,500个错误代码,但是你应该得到这个jist。

答案 1 :(得分:0)

你好。

<?php

/**
 * @param $url
 * @return array|bool
 */
function curl_trace($url)
{
    // return false if no url supplied
    if (!$url) return false;

    // initialize cURL
    $c = curl_init();

    // set some cURL parameters
    // info: https://curl.haxx.se/libcurl/c/
    curl_setopt_array($c, array(
        CURLOPT_URL => $url,
        CURLOPT_FOLLOWLOCATION => 1,
        CURLOPT_HEADER => 1,
        CURLOPT_NOBODY => 1,
        CURLOPT_RETURNTRANSFER => 1
    ));

    // fetch the response and info
    $response = curl_exec($c);
    $status = curl_getinfo($c);

    // close cURL
    curl_close($c);

    // we simply only check for anything less than HTTP 400
    // TODO: could do more validation here
    if ($status['http_code'] < 400) {
        // trim away any excess whitespace since we're gonna use regex later
        $response = trim($response);
        // declare a result placeholder array
        $result = array();

        // match all lines beginning with Location: and extract the URI
        preg_match_all('/Location:\s(.*)\r\n/', $response, $locations);

        // the iteration counter I will use below
        $i = 0;

        // here we split the headers by empty lines and looped through them
        foreach (preg_split('/\n\s*\r\n/Uis', $response) as $header) {
            if ($i == 0) {
                // in the first iteration use the queried URL
                $result[] = sprintf(
                    '%s [%d]',
                    $url,
                    substr($header, 9, 3)
                );
            } else {
                // in all subsequent iterations use the locations we extracted above
                $result[] = sprintf(
                    '%s [%d]',
                    strtok($locations[1][$i - 1], '?'),
                    substr($header, 9, 3)
                );
            }

            // increment the iteration counter
            $i++;
        }

        return $result;
    } else {
        // return false if HTTP code above or equal to 400
        return false;
    }
}

print_r(curl_trace('microsoft.com'));

返回类似的内容:

Array
(
    [0] => microsoft.com [301]
    [1] => https://microsoft.com/ [301]
    [2] => https://www.microsoft.com/ [200]
)