php 检查php中的链接是否损坏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15770903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if links are broken in php
提问by StenW
I wonder if there is any good PHP script (libraries) to check if link are broken? I have links to documents in a mysql table and could possibly just check if the link leads to a the document, or if I am redirected to anther url. Any idea? I would prefer to do it in PHP.
我想知道是否有任何好的 PHP 脚本(库)来检查链接是否损坏?我在 mysql 表中有指向文档的链接,并且可能只是检查链接是否指向文档,或者我是否被重定向到花药 url。任何的想法?我更愿意用 PHP 来做。
Might be related to: Check link works and if not visually identify it as broken
回答by Sabari
You can check for broken link using this function:
您可以使用此功能检查断开的链接:
function check_url($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$headers = curl_getinfo($ch);
curl_close($ch);
return $headers['http_code'];
}
You need to have CURL
installed for this to work. Now you can check for broken links using:
你需要CURL
安装它才能工作。现在您可以使用以下方法检查损坏的链接:
$check_url_status = check_url($url);
if ($check_url_status == '200')
echo "Link Works";
else
echo "Broken Link";
Also check this link for HTTP status codes : HTTP Status Codes
另请检查此链接以获取 HTTP 状态代码:HTTP 状态代码
I think you can also check for 301
and 302
status codes.
我想你也可以检查301
和302
状态代码。
Also another method would be to use get_headers
function . But this works only if your PHP version is greater than 5 :
另一种方法是使用get_headers
function 。但这仅在您的 PHP 版本大于 5 时才有效:
function check_url($url) {
$headers = @get_headers( $url);
$headers = (is_array($headers)) ? implode( "\n ", $headers) : $headers;
return (bool)preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers);
}
In this case just check the output :
在这种情况下,只需检查输出:
if (check_url($url))
echo "Link Works";
else
echo "Broken Link";
Hope this helps you :).
希望这对你有帮助:)。
回答by Orel Biton
You can do this in few ways:
您可以通过以下几种方式做到这一点:
First way - curl
第一种方式——卷曲
function url_exists($url) {
$ch = @curl_init($url);
@curl_setopt($ch, CURLOPT_HEADER, TRUE);
@curl_setopt($ch, CURLOPT_NOBODY, TRUE);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$status = array();
preg_match('/HTTP\/.* ([0-9]+) .*/', @curl_exec($ch) , $status);
return ($status[1] == 200);
}
Second way - if you dont have curl installed - get headers
第二种方式 - 如果你没有安装 curl - 获取标题
function url_exists($url) {
$h = get_headers($url);
$status = array();
preg_match('/HTTP\/.* ([0-9]+) .*/', $h[0] , $status);
return ($status[1] == 200);
}
Third way - fopen
第三种方式 - fopen
function url_exists($url){
$open = @fopen($url,'r');
if($handle !== false){
return true;
}else{
return false;
}
}
回答by shakaran
As quick workaround check, you can use the global variable $http_response_headerwith file_get_contents()function.
作为快速解决方法检查,您可以将全局变量$http_response_header与file_get_contents()函数一起使用。
For example (extracted from PHP documentation):
例如(摘自 PHP 文档):
<?php
function get_contents() {
file_get_contents("http://example.com");
var_dump($http_response_header);
}
get_contents();
var_dump($http_response_header);
Then check the status code in first line for a "HTTP/1.1 200 OK" or other HTTP status codes.
然后检查第一行中的状态代码是否为“HTTP/1.1 200 OK”或其他HTTP 状态代码。
回答by mkjasinski
Try this:
尝试这个:
$url = '[your_url]';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
if ($result === false) {
echo 'broken url';
} else {
$newUrl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
if ($newUrl !== $url) {
echo 'redirect to: ' . $newUrl;
}
}
curl_close($curl);