2012-03-23 8 views
0

PHPデータベースのリンクを定期的にループして、リンクが有効なページにつながっているかどうかを確認する必要があります。リンクが期限切れになっているか無効である場合は、出力したくありません。 hrefの値が有効なページに効率的につながることを確認するにはどうすればよいですか?リンクhref属性を検証する

*すべてのポインタありがとうございます。

+1

'有効なページ'はhttpステータスコード= 200ではないことを意味しますか? – safarov

+0

ええ、私は200がほしいと思う。ちょうど404または他の悪いエラーコードではない。特定のURLを持つ製品のリストがあり、サプライヤー(J.C. Pennyなど)がそれらを変更すると、これらの製品へのリンクが変更されます。私は私の顧客を「古い」リンクに送りたくないので、それらの「期限切れの」URLまたは「無効な」URLを出力したくありません。それは理にかなっていますか? – jrubins

+0

あなたはリンクの出力の前に毎回これをしたくないのです...あなたは 'cron'などでスケジュールされたバックグラウンドタスクとしてこれを実行します。 – prodigitalson

答えて

1

あなたはまた、より速く、すべてのリストを確認するために、複数のカール要求するたびに使用することができます。 Check here

0

私は自分自身のようなものですが、私はcURLを使用することをお勧めします。使用上の迅速なGoogle検索は、(私がテストしていない)、次のコードを明らかにした:

<?php 

$statusCode = validate($_REQUEST['url']); 
if ($statusCode==’200′) 
    echo ‘Voila! URL ‘.$_REQUEST['url']. 
    ’ exists, returned code is :’.$statusCode; 
else 
    echo ‘Opps! URL ‘.$_REQUEST['url']. 
    ’ does NOT exist, returned code is :’.$statusCode; 

function validateurl($url) 
{ 
    // Initialize the handle 
    $ch = curl_init(); 
    // Set the URL to be executed 
    curl_setopt($ch, CURLOPT_URL, $url); 
    // Set the curl option to include the header in the output 
    curl_setopt($ch, CURLOPT_HEADER, true); 
    // Set the curl option NOT to output the body content 
    curl_setopt($ch, CURLOPT_NOBODY, true); 
    /* Set to TRUE to return the transfer 
    as a string of the return value of curl_exec(), 
    instead of outputting it out directly */ 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    // Execute it 
    $data = curl_exec($ch); 
    // Finally close the handle 
    curl_close($ch); 
    /* In this case, we’re interested in 
    only the HTTP status code returned, therefore we 
    use preg_match to extract it, so in the second element 
    of the returned array is the status code */ 
    preg_match(“/HTTP\/1\.[1|0]\s(\d{3})/”,$data,$matches); 
    return $matches[1]; 
} 
?> 

出典:http://www.ajaxapp.com/2009/03/23/to-validate-if-an-url-exists-use-php-curl/

関連する問題