2011-12-16 68 views
6

Tôi đang cố gắng mã hóa trình kiểm tra chuyển hướng, để kiểm tra xem URL có thân thiện với công cụ tìm kiếm hay không. Nó phải kiểm tra xem một URL được chuyển hướng hay không, và nếu nó được chuyển hướng nó phải cho biết nếu nó thân thiện với SEO (301 mã trạng thái) hay không (302/304).Tìm chuyển hướng URL với tiêu đề HTTP và curl?

Dưới đây là một cái gì đó tương tự tôi đã tìm thấy: http://www.webconfs.com/redirect-check.php

Nó cũng sẽ có thể làm theo nhiều chuyển hướng (ví dụ như từ A đến B đến C) và cho tôi biết rằng A chuyển hướng đến C.

Đây là những gì tôi có cho đến nay, nhưng nó không hoạt động hoàn toàn đúng (ví dụ: khi nhập vào www.example.com nó không tìm thấy chuyển hướng đến www.example.com/page1)

<?php 
// You can edit the messages of the respective code over here 
$httpcode = array(); 
$httpcode["200"] = "Ok"; 
$httpcode["201"] = "Created"; 
$httpcode["302"] = "Found"; 
$httpcode["301"] = "Moved Permanently"; 
$httpcode["304"] = "Not Modified"; 
$httpcode["400"] = "Bad Request"; 


if(count($_POST)>0) 
{ 
    $url = $_POST["url"]; 
    $curlurl = "http://".$url."/"; 
    $ch = curl_init(); 
    // Set URL to download 
    curl_setopt($ch, CURLOPT_URL, $curlurl); 

    // User agent 
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); 
    // Include header in result? (0 = yes, 1 = no) 
    curl_setopt($ch, CURLOPT_HEADER, 0); 

    // Should cURL return or print out the data? (true = return, false = print) 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    // Timeout in seconds 
    curl_setopt($ch, CURLOPT_TIMEOUT, 15); 

    // Download the given URL, and return output 
    $output = curl_exec($ch); 

    $curlinfo = curl_getinfo($ch); 

    if(($curlinfo["http_code"]=="301") || ($curlinfo["http_code"]=="302")) 
    { 
     $ch = curl_init(); 
     // Set URL to download 
     curl_setopt($ch, CURLOPT_URL, $curlurl); 

     // User agent 
     curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); 
     // Include header in result? (0 = yes, 1 = no) 
     curl_setopt($ch, CURLOPT_HEADER, 0); 

     // Should cURL return or print out the data? (true = return, false = print) 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

     // Timeout in seconds 
     curl_setopt($ch, CURLOPT_TIMEOUT, 15); 


     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
     // Download the given URL, and return output 
     $output = curl_exec($ch); 

     $curlinfo = curl_getinfo($ch); 
     echo $url." is redirected to ".$curlinfo["url"]; 
    } 
    else 
    { 
     echo $url." is not getting redirected"; 
    } 

    // Close the cURL resource, and free system resources 
    curl_close($ch); 
} 
?> 
<form action="" method="post"> 
http://<input type="text" name="url" size="30" />/ <b>e.g. www.google.com</b><br/> 
<input type="submit" value="Submit" /> 
</form> 
+0

Thú vị rằng một 'curl_setopt() 'dòng đó gây ra vấn đề là một trong đó không có một nhận xét. –

+0

Vui lòng kiểm tra câu trả lời thay thế tại đây, http://stackoverflow.com/questions/4062819/curl-get-redirect-url-to-a-variable/7616942#comment15921700_7616942 –

Trả lời

7

Vâng nếu bạn muốn để ghi lại mọi chuyển hướng bạn phải tự mình thực hiện và xoay ra khỏi "vị trí sau" tự động:

function curl_trace_redirects($url, $timeout = 15) { 

    $result = array(); 
    $ch = curl_init(); 

    $trace = true; 
    $currentUrl = $url; 

    $urlHist = array(); 
    while($trace && $timeout > 0 && !isset($urlHist[$currentUrl])) { 
     $urlHist[$currentUrl] = true; 

     curl_setopt($ch, CURLOPT_URL, $currentUrl); 
     curl_setopt($ch, CURLOPT_HEADER, true); 
     curl_setopt($ch, CURLOPT_NOBODY, true); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); 
     curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); 

     $output = curl_exec($ch); 

     if($output === false) { 
      $traceItem = array(
       'errorno' => curl_errno($ch), 
       'error' => curl_error($ch), 
      ); 

      $trace = false; 
     } else { 
      $curlinfo = curl_getinfo($ch); 

      if(isset($curlinfo['total_time'])) { 
       $timeout -= $curlinfo['total_time']; 
      } 

      if(!isset($curlinfo['redirect_url'])) { 
       $curlinfo['redirect_url'] = get_redirect_url($output); 
      } 

      if(!empty($curlinfo['redirect_url'])) { 
       $currentUrl = $curlinfo['redirect_url']; 
      } else { 
       $trace = false; 
      } 

      $traceItem = $curlinfo; 
     } 

     $result[] = $traceItem; 
    } 

    if($timeout < 0) { 
     $result[] = array('timeout' => $timeout); 
    } 

    curl_close($ch); 

    return $result; 
} 

// apparently 'redirect_url' is not available on all curl-versions 
// so we fetch the location header ourselves 
function get_redirect_url($header) { 
    if(preg_match('/^Location:\s+(.*)$/mi', $header, $m)) { 
     return trim($m[1]); 
    } 

    return ""; 
} 

Và bạn sử dụng nó như thế:

$res = curl_trace_redirects("http://www.example.com"); 
foreach($res as $item) { 
    if(isset($item['timeout'])) { 
     echo "Timeout reached!\n"; 
    } else if(isset($item['error'])) { 
     echo "error: ", $item['error'], "\n"; 
    } else { 
     echo $item['url']; 
     if(!empty($item['redirect_url'])) { 
      // redirection 
      echo " -> (", $item['http_code'], ")"; 
     } 

     echo "\n"; 
    } 
} 

Có thể là mã của tôi là không hoàn toàn nghĩ ra, nhưng tôi đoán đó là một khởi đầu tốt.

Sửa

Dưới đây là một số mẫu Output:

http://midas/~stefan/test/redirect/fritzli.html -> (302) 
http://midas/~stefan/test/redirect/hansli.html -> (301) 
http://midas/~stefan/test/redirect/heiri.html 
Các vấn đề liên quan