我是.php的新手,我不能让我的代码工作。我有一个检查网站是否正常工作的函数,之后我只是尝试从一个.txt文件中获取链接,并用检查函数的结果回显它们,但我似乎没有得到正确的结果。下面是我的代码:
<?php
function url_test( $url ) {
$timeout = 10;
$ch = curl_init();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_TIMEOUT, $timeout );
$http_respond = curl_exec($ch);
$http_respond = trim( strip_tags( $http_respond ) );
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
if ( ( $http_code == "200" ) || ( $http_code == "302" ) ) {
return true;
} else {
// return $http_code;, possible too
return false;
}
curl_close( $ch );
}
$links = file("test.txt");
if( !url_test( $links) ) {
echo $links ." is down!";
}
else { echo $links ." functions correctly."; }
}
?>
你们能帮帮我吗?谢谢!
发布于 2019-02-21 02:56:50
这是你的代码的更新,这意味着你每行有一个url。
希望这对你有帮助,祝你编码愉快。
<?php
function url_test( $url ) {
$timeout = 10;
$ch = curl_init();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_TIMEOUT, $timeout );
$http_respond = curl_exec($ch);
$http_respond = trim( strip_tags( $http_respond ) );
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
if ( ( $http_code == "200" ) || ( $http_code == "302" ) ) {
return true;
} else {
// return $http_code;, possible too
return false;
}
curl_close( $ch );
}
$file ="test.txt";
$handle = fopen($file, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
// process the url
if(url_test(trim($line))){
print "Url $line Up\n";
}else{
print "Url $line Down\n";
}
}
fclose($handle);
} else {
// error opening the file.
}
?>
发布于 2019-02-21 02:57:29
文件(“test.txt”);将文件的所有行读入一个数组。如果你知道你的文件总是只有一行,你必须像这样读取索引为0的array的元素:
$links = file("test.txt");
if( !url_test( $links[0]) ) {
echo $links[0] ." is down!";
}
else {
echo $links[0] ." functions correctly.";
}
但是,如果您的文件可能包含多个URL(每行一个)的列表,那么您必须遍历数组并检查所有URL,如下所示:
$links = file("test.txt");
foreach ($links as $line_num => $link) {
if( !url_test( $link) ) {
echo $link ." is down!";
}
else {
echo $link ." functions correctly.";
}
}
发布于 2019-02-21 02:59:10
问题是file()
将返回一个行数组,所以如果文件中只有一个URL,那么可以使用[0]
传递第一行,否则在列表中传递并使用foreach()
。在设置网址时,使用trim()
来确保网址没有杂乱的空格等也是值得的。我还更改了测试,以检查状态代码是否处于一组可能的可接受状态,而不是检查单个值。
多URL代码...
function url_test( $url ) {
$timeout = 10;
$ch = curl_init();
foreach ( $url as $link ) {
curl_setopt ( $ch, CURLOPT_URL, trim($link) );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_TIMEOUT, $timeout );
$http_respond = curl_exec($ch);
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
if ( !in_array($http_code , ["200", "301", "302"] ) ) {
return false;
}
}
curl_close( $ch );
return true;
}
$links = file("test.txt");
if( !url_test( $links) ) {
echo " is down!";
}
else { echo " functions correctly."; }
1 URL示例...
function url_test( $url ) {
$timeout = 10;
$ch = curl_init();
curl_setopt ( $ch, CURLOPT_URL, trim($url) );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_TIMEOUT, $timeout );
$http_respond = curl_exec($ch);
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
if ( !in_array($http_code , ["200", "301", "302"] ) ) {
return false;
}
curl_close( $ch );
return true;
}
$links = file("test.txt");
if( !url_test( $links[0]) ) {
echo " is down!";
}
else { echo " functions correctly."; }
https://stackoverflow.com/questions/54793263
复制相似问题