[英]Validate link href attribute
我需要定期循環遍歷PHP數據庫中的鏈接,以檢查鏈接是否指向有效頁面。 如果鏈接已過期或無效,我不想輸出它。 如何檢查href值是否有效地導致有效頁面?
感謝任何*指針。
看看卷曲。 它允許您在php中提取網站http://www.php.net/manual/en/function.curl-exec.php然后只需檢查響應上的狀態代碼或類似標題標記的內容。
您還可以每次使用多個CUrl請求以更快地檢查所有列表。 點擊這里
我自己就是一個菜鳥,但我建議使用cURL。 谷歌快速使用搜索顯示以下代碼(我還沒有測試過):
<?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/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.