【发布时间】:2015-07-05 01:47:54
【问题描述】:
所以下面的代码中发生的事情是我在这个链接上从游戏中获取高分数据:
http://hiscore.runescape.com/index_lite.ws?player=
在网址末尾使用正确的用户名(例如“n0tch”)时一切正常。但是当用户名不存在时,它会显示游戏的网站。我需要的是一种检查页面上是否存在字符串的方法。
我得到的错误:
为了帮助您更好地理解,我会尝试在您的浏览器中输入这些网址:
现有和工作用户名:
http://hiscore.runescape.com/index_lite.ws?player=n0tch
不存在 & 在 PHP 中调用时抛出错误:
http://hiscore.runescape.com/index_lite.ws?player=asdfse123d
我需要在 foreach 循环周围添加某种错误检查语句。目前我正在尝试获取站点的响应代码,但每次都是 200。
代码:
<?php
$rsn=$_GET["rsn"];
$stats=array("Overall","Attack","Defence","Strength","Hitpoints","Ranged","Prayer","Magic","Cooking","Woodcutting","Fletching","Fishing","Firemaking","Crafting","Smithing","Mining","Herblore","Agility","Thieving","Slayer","Farming","Runecrafting","Hunter","Construction","Summoning","Dungeoneering","Duel Tournament","Bounty Hunters","Bounty Hunters Rougue","Fist Of Guthix","Mobilising Armies");
$url="http://hiscore.runescape.com/index_lite.ws?player=".$rsn;
$a="0";
echo "<pre>Showing Stats for: ".$rsn."\r\n";
echo "<center>
<table>
<tr>
<th>Skill</th>
<th>Rank</th>
<th>Level</th>
<th>Experience</th>
</tr>";
// var_dump($http_response_header);
var_dump(http_response_code());
if (http_response_code(200)) {
$data=explode(chr(10),file_get_contents($url));
foreach ($data as $value) {
if ($a<26) {
$value=str_replace(",","</td><td>",$value);
$pic = "<img class='img-resize' src='./img/skills/".$stats[$a].".png'>";
echo "<tr><td id='firstRow'>".$pic." ".$stats[$a].":</td> ".str_replace("-1 -1","Not Ranked",str_replace("-1 -1 -1","Not Ranked","<td>".$value."</td></tr>"))."\r\n";
}
$a++;
}
echo "</table></center>";
echo "Done";
} else {
echo "didn't work";
}
?>
【问题讨论】:
-
@Devon 我不明白给出的答案。 varDump 我将如何设置它?我还是 php 新手,只是想学习
-
试试 var_dump 看看有哪些变量可用。 PHP 手册会派上用场。
-
我建议您检查 file_get_contents 的返回值并采取相应措施。如果服务器返回 404 消息,file_get_contents 将返回 false 并触发警告,否则返回页面内容。
-
@Devon 所以我使用了它,它在我的屏幕上输出了一些东西:prntscr.com/6ygr2u 但正如我之前所说,我对此有点陌生,所以我真的不知道该怎么做点。
标签: php