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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (26)

我是.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

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

?>
用户回答回答于

问题是file()将返回一个行数组,因此如果文件中只有一个URL可用于[0]传递第一行,否则传入列表并使用foreach()trim()在设置URL时,确保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."; }

扫码关注云+社区

领取腾讯云代金券