没有输出形式URL与CURL
问题描述:
嗨,大家好我使用我的代码从url获取文件数据我测试了curl函数的工作,但我不知道我的代码有什么问题,因为我没有任何输出请求可以有人告诉我该怎么解决,我想我有问题,本节:没有输出形式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
袅袅不返回数组。它将该响应作为STRING返回。你不能foreach()一个字符串。 – 2014-10-30 14:36:27
是否正确以及如何解决该问题? – navida 2014-10-30 14:39:50