从页面中提取数据并将其添加到PHP文件中

时间:2016-01-29 18:03:13

标签: php html json api

我正在尝试创建一个使用页面数据的PHP页面(例如:http://services.runescape.com/m=hiscore/index_lite.ws?player=DisplayName)。更改" DisplayName"用另一个名字来改变所显示数字的值。

这是我尝试从中提取数据的页面中显示的代码:

665249,1159,37778175 559762,75,1214453 1014682,45,67435 287248,91,6088249 391910,86,3747242 133650,99,13759525 599855,52,125572 264389,94,8032896 -1,1,-1 375977,82,2480819 676548,46,71298 346696,76,1430070 841961,50,105600 843793,41,42078 1178160,20,4481 1092128,32,17847 763021,18,3685 498978,51,121383 485421,53,145460 455992,59,263171 519344,32,17930 -1,1,-1 -1,1,-1 552229,40,37356 -1,1,-1 -1,1,-1 -1,1,-1 -1,0,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 -1,-1 

基本上,每个数字对应一个值。格式如下:

xxx,xxx,xxx xxx,xxx,xxx

第一个数字代表一个玩家在一个异能的能力中的位置,第二个数字代表相应能力的等级,第三个数字代表能力中累积的总经验。接下来,有一个空间意味着一种新的能力正在被描述和#34;格式相同(位置,级别,经验)。

因此可以描述为:

position,level,experience position,level,experience position,level,experience

我尝试过在网上找到的一些代码,并尝试对它们进行调整,但我没有设法让它们发挥作用。

您知道如何创建可以放入HTML模板的有效PHP代码吗?

可在此处找到有关数据的更好说明:http://services.runescape.com/m=rswiki/en/Hiscores_APIs

感谢您的帮助,

Eibe

编辑:

<?php
header('Content-type: application/json');
require_once 'Runescape_API.class.php';

$rsapi = new Runescape_API;
//Hiscore
if($ressource = $rsapi->getHiscore("Eibe")){
    $result["getHiscore"] = $ressource;
}
//Combat Level
if($ressource = $rsapi->getCombatLevel("Eibe")){
    $result["getCombatLevel"] = $ressource;
}
//Player Clan
if($ressource = $rsapi->getClanByPlayer("Eibe")){
    $result["getClanByPlayer"] = $ressource;
}
//Clan Members
if($ressource = $rsapi->getClanMembers("Loyals")){
    $result["getClanMembers"] = $ressource;
}
//Events
if($ressource = $rsapi->getEvents('archived')){
    $result["getEvents"] = $ressource;
}
//Events By Player
if($ressource = $rsapi->getEventsByPlayer('Eibe','archived')){
    $result["getEventsByPlayer"] = $ressource;
}
//Beast
if($ressource = $rsapi->getBeastById(49)){
    $result["getBeastById"] = $ressource;
}
//Search Beasts
if($ressource = $rsapi->searchBeasts('kuh',1)){
    $result["searchBeasts"] = $ressource;
}
//Beast Cataloque
if($ressource = $rsapi->getBeastCataloque('a',1)){
    $result["getBeastCataloque"] = $ressource;
}
//Area Names
if($ressource = $rsapi->getAreaNames()){
    $result["getAreaNames"] = $ressource;
}
//Beasts area
if($ressource = $rsapi->getBeastsByArea("Bank")){
    $result["getBeastsByArea"] = $ressource;
}
//Slayer names
if($ressource = $rsapi->getSlayerCatNames("Bank")){
    $result["getSlayerCatNames"] = $ressource;
}
//Beasts by slayer cat
if($ressource = $rsapi->getBeastsBySlayerCat(96)){
    $result["getBeastsBySlayerCat"] = $ressource;
}
//memberStatus, returns true|false and null by failure
$ressource = $rsapi->getMemberStatus("Glotzfrosch");
if($ressource !== NULL){
    $result["getMemberStatus"] = $ressource;
}/*
//Quests
if($ressource = $rsapi->getQuests("Glotzfrosch")){
    $result["getQuests"] = $ressource;
}*/
//recent player events
if($ressource = $rsapi->getRecentPlayerEvents("Drumgun")){
    $result["getRecentPlayerEvents"] = $ressource;
}
//item information
if($ressource = $rsapi->getItemInformation(444,"br")){
    $result["getItemInformation"] = $ressource;
}
//item price
if($ressource = $rsapi->getPriceInformation(444,"br")){
    $result["getPriceInformation"] = $ressource;
}
//item cataloque
if($ressource = $rsapi->getItemCataloque(2)){
    $result["getItemCataloque"] = $ressource;
}
//items by cataloque
if($ressource = $rsapi->getItemsByCataloque(12,"a",20)){
    $result["getItemsByCataloque"] = $ressource;
}
//player avatar urls
if($ressource = $rsapi->getPlayerAvatars("Drumgun")){
    $result["getPlayerAvatars"] = $ressource;
}
//news
if($ressource = $rsapi->getNews(2)){
    $result["getNews"] = $ressource;
}
//Hiscore for oldschool runescape
if($ressource = $rsapi->getHiscore07("xMorgan")){
    $result["getHiscore07"] = $ressource;
}

echo json_encode($result);
?>

使用此处http://pastebin.com/0Q2Qguux

中的RuneScape_API.class.php文件

注意:我甚至不确定这些文件是否符合我的要求。我是个乞丐。

1 个答案:

答案 0 :(得分:2)

首先获取url内容并相应地解析它。

例如:

$url = "http://services.runescape.com/m=hiscore/index_lite.ws?player=DisplayName";
$f = file_get_contents($url);
$items = explode(' ', $f);

在$ items中你将拥有所有玩家xxx,xxx,xxx。当然,这取决于文件中分隔玩家的空间。

您可以使用相同的explode()函数解析$ items中的每个元素 - 可能 - 以获取您要查找的信息。比如这样做:

foreach($items as $item){
    $player = explode(",", $item);
    // $player[0] - will contain the first number
    // $player[1] - will contain second
    // $player[3] - will contain third
}