PHP从.text列表中读取链接并检查它们是否已启动

问题描述 投票:0回答:3

我是.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."; }
}


?>

你能帮我吗?谢谢!

php file
3个回答
1
投票

这是您的代码的更新,这意味着您每行有一个网址。

希望这有帮助,快乐编码。

<?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.
} 

?>

0
投票

问题是file()将返回一个行数组,因此如果文件中只有一个URL,则可以使用[0]传递第一行,否则传入列表并使用foreach()。在设置URL时,使用trim()确保URL没有任何杂散空间等也是值得的。我还更改了测试以检查状态代码是否处于一组可能的可接受状态,而不是检查单个值。

多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."; }

0
投票

文件(“test.txt”);将文件的所有行读入数组。如果您知道您的文件将始终只有一行,则必须读取索引0处的数组元素,如下所示:

$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."; 
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.