2017-04-16 45 views
1

我正在尋找一個PHP函數,我在其中插入一個域並且數組輸出響應代碼。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] 

可有人請幫助?

謝謝:)

+1

我想你必須推出自己的使用捲曲或類似的東西(驗證和)檢查每一個網址。 – techouse

+0

你能指點我的代碼示例,因爲我不知道從哪裏開始...... – Rank

回答

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] 
) 
0

我想出了這樣的事情:

<?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); 
} 

給你:

http://google.co.uk/ [301] 
http://www.google.co.uk/ [200] 

希望這會給你一個開始,我知道代碼有一些錯誤的邏輯,403,404,500錯誤代碼在這裏沒有處理,但你應該得到jist。