重定向到 URL 抛出错误
Redirect to URL throws error
我有一个函数用来测试 URL 在我将它存储到我的数据库之前是否有效。
function url_exists($url)
{
ini_set("default_socket_timeout","5");
set_time_limit(5);
$f = fopen($url, "r");
$r = fread($f, 1000);
fclose($f);
return strlen($r) > 1;
}
if( !url_exists($test['urlRedirect']) ) { ... }
效果很好,但是我的一位用户今天报告了一个问题,当我测试时,确实以下 URL 被标记为无效:
http://www.artleaguehouston.org/charge-grant-survey
所以我尝试删除页面名称并仅使用域,但仍然出现错误。我的脚本阻塞的域是什么?
你试试那里用瑞士刀吃汤!
PHP 在 file_exists
:
中支持 URL 包装器
if (file_exists("http://www.artleaguehouston.org/charge-grant-survey")) {
// URL returns a good status code for your IP and User Agent "PHP/x.x.x"
}
CURL:
$ch = curl_init('http://www.artleaguehouston.org/charge-grant-survey');
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_USERAGENT,
'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0'
);
curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($statusCode == 200) {
// Site up and good status code
}
(大部分取自 How can one check to see if a remote file exists using PHP? ,只是为了给出正确的信用)。
我有一个函数用来测试 URL 在我将它存储到我的数据库之前是否有效。
function url_exists($url)
{
ini_set("default_socket_timeout","5");
set_time_limit(5);
$f = fopen($url, "r");
$r = fread($f, 1000);
fclose($f);
return strlen($r) > 1;
}
if( !url_exists($test['urlRedirect']) ) { ... }
效果很好,但是我的一位用户今天报告了一个问题,当我测试时,确实以下 URL 被标记为无效:
http://www.artleaguehouston.org/charge-grant-survey
所以我尝试删除页面名称并仅使用域,但仍然出现错误。我的脚本阻塞的域是什么?
你试试那里用瑞士刀吃汤!
PHP 在 file_exists
:
if (file_exists("http://www.artleaguehouston.org/charge-grant-survey")) {
// URL returns a good status code for your IP and User Agent "PHP/x.x.x"
}
CURL:
$ch = curl_init('http://www.artleaguehouston.org/charge-grant-survey');
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_USERAGENT,
'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0'
);
curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($statusCode == 200) {
// Site up and good status code
}
(大部分取自 How can one check to see if a remote file exists using PHP? ,只是为了给出正确的信用)。