大家好,我用我的代码从url中获取文件数据。我测试了curl函数也能正常工作,但我不知道我的代码有什么问题,因为我没有任何输出。有人能告诉我如何解决这个问题吗?我想我在这个部分有问题:
foreach($serverlist as $line) {
$line = trim($line);
但我不知道如何解决这个问题:
<?php
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$serverlist = get_data("http://185.49.84.109/iw5msl/serverinfo.txt");
$file_headers = @get_headers($serverlist);
if(($file_headers[0] == 'HTTP/1.0 404 Not Found') || ($file_headers[0] == 'HTTP/1.0 302 Found' && $file_headers[7] == 'HTTP/1.0 404 Not Found'))
{
echo "The server list is currently being changed due to some upgrades and therefore not available. Sorry for any inconvenience.";
} else {
$odd = false;
foreach($serverlist as $line) {
$line = trim($line);
list($name,$ip,$players,$map,$type,$mod,$country) = explode("|!|!|", $line . "|!|!|");
$ixp = explode(":", $ip);
if(in_array($ixp[0], file("data/servers.banlist")) || in_array($ip, file("data/servers.banlist"))) continue;
$type = parse_gametype($type);
$map = @$_MAPS["mw3"][$map];
if(strstr($map, "<") > -1) $map = "Server by idiot.";
list($players,$maxplayers) = explode("/", $players);
$country = "<span style='"display:none'">$country</span><img alt='"$country'" title='"$country'" src='"".parse_flag($country)."'" />";
?>
<tr style="height: 50px">
<td style="text-align:left"><span style="display: none"><?php echo preg_replace("/[^a-zA-Z0-9's]/", "", strip_tags($name)); ?></span>
<span style="font-size: 1.5em; font-family: 'Play', 'OCR A Extended'"><?php echo $name; ?></span><br />
<span style="font-size: 1.3em"><?php echo $ip; ?></span>
</td>
<td style="text-align: right; font-size: 1.5em"><?php echo $players; ?></td>
<td style="font-size: 1.5em">/</td>
<td style="text-align: left; font-size: 1.5em; width: 50px"><?php echo $maxplayers; ?></td>
<td style="font-family: Electrolize; letter-spacing: 1px; font-size: 1.4em; text-align:right; display: color:white;background-size:100%;background-position:center center;background-image:url(<?php echo $map_thumb; ?>)">
<div class="blackgradient gradient"><?php echo $map; ?></div>
</td>
<td><?php echo $type; ?></td>
<!--<td><?php echo $mod; ?></td>-->
<td><?php echo $country; ?></td>
</tr>
<?php $odd = !$odd; ?>
<?php } ?>
</tbody></table>
<?php } ?>
尝试添加
$serverlist=explode("'n", $serverlist);
在FOREACH行之前。
Thx,它应该是"'n"
。
对于尾部的空线,使用FOREACH附近的空线:
$serverlist=explode("'n", $serverlist); //ADDED
$odd = false;
foreach($serverlist as $line) {
$line = trim($line);
if(empty($line))continue; //ADDED