0

我有一个数组:

Array ( [0] => Ahri [1] => Akali [2] => Alistar [3] => Amumu [4] => Anivia [5] =>  Annie [6] => Ashe [7] => Blitzcrank [8] => Brand [9] => Caitlyn [10] => Cassiopeia [11] => Cho'Gath [12] => Corki [13] => Dr. Mundo [14] => Evelynn [15] => Ezreal [16] => Fiddlesticks [17] => Fiora [18] => Fizz [19] => Galio [20] => Gangplank [21] => Garen [22] => Gragas [23] => Graves [24] => Hecarim [25] => Heimerdinger [26] => Irelia [27] => Janna [28] => Jarvan IV [29] => Jax [30] => Karma [31] => Karthus [32] => Kassadin [33] => Katarina [34] => Kayle [35] => Kennen [36] => Kog'Maw [37] => LeBlanc [38] => Lee Sin [39] => Leona [40] => Lulu [41] => Lux [42] => Malphite [43] => Malzahar [44] => Maokai [45] => Master Yi [46] => Miss Fortune [47] => Mordekaiser [48] => Morgana [49] => Nasus [50] => Nautilus [51] => Nidalee [52] => Nocturne [53] => Nunu [54] => Olaf [55] => Orianna [56] => Pantheon [57] => Poppy [58] => Rammus [59] => Renekton [60] => Riven [61] => Rumble [62] => Ryze [63] => Sejuani [64] => Shaco [65] => Shen [66] => Shyvana [67] => Singed [68] => Sion [69] => Sivir [70] => Skarner [71] => Sona [72] => Soraka [73] => Swain [74] => Talon [75] => Taric [76] => Teemo [77] => Tristana [78] => Trundle [79] => Tryndamere [80] => Twisted Fate [81] => Twitch [82] => Udyr [83] => Urgot [84] => Varus [85] => Vayne [86] => Veigar [87] => Viktor [88] => Vladimir [89] => Volibear [90] => Warwick [91] => Wukong [92] => Xerath [93] => Xin Zhao [94] => Yorick [95] => Ziggs [96] => Zilean [97] => Lee Sin [98] => Orianna [99] => Yorick [100] => Leona [101] => Riven [102] => Lulu [103] => Hecarim [104] => Varus [105] => Malphite [106] => Morgana [107] => Ryze [108] => Annie [109] => Cho'Gath [110] => Taric [111] => Jax [112] => Teemo [113] => Warwick [114] => Poppy [115] => Garen [116] => Anivia [117] => Blitzcrank [118] => Corki [119] => Karthus [120] => Twitch [121] => Irelia [122] => Miss Fortune [123] => Galio [124] => Swain [125] => Kennen [126] => Malzahar ) 

我需要在以下函数中使用这些值中的每一个:

$input=file_get_contents("http://leagueoflegends.wikia.com/api.php?action=query&titles=$value&prop=revisions&rvprop=content&format=xml");

其中$value是数组中的一个值(例如Ahri)。

我怎样才能做到这一点?

编辑:我通过执行以下操作获得这些值:

$file = file_get_contents('http://leagueoflegends.wikia.com/api.php?action=query&titles=List_of_champions&prop=revisions&rvprop=content&format=xml');
preg_match_all('/{{ci\|([^}]*)}}/', $file, $matches);

(上面提到的数组在哪里$matches。)

4

1 回答 1

2

或者,您可以使用LeagueOfLegends API|的内置方式,并通过将每个标题与字符分隔来同时检索所有值。像这样:

$values = array("Ahri","Akali","Alistar");
$input  = "";

while($pack = array_splice($values,0,49)){
    $concatValues = implode("|",$values); // Will return "Ahri|Akali|Alistar";
    $url   = "http://leagueoflegends.wikia.com/api.php?action=query&titles=$concatValues&prop=revisions&rvprop=content&format=xml";
    $input .= file_get_contents($url);
}

对于代码的前三个元素,将返回以下内容,您可以一个一个地执行此操作,但 API 礼仪建议您执行最少数量的请求来获取所需的数据。

<api>
    <query>
    <pages>
        <page pageid="281432" ns="0" title="Ahri">
            <revisions>
                <rev xml:space="preserve">#REDIRECT [[Ahri the 9 Tails Fox]]</rev>
            </revisions>
        </page>
        <page pageid="5578" ns="0" title="Akali">
            <revisions>
                <rev xml:space="preserve">#REDIRECT [[Akali the Fist of Shadow]]</rev>
            </revisions>
        </page>
        <page pageid="2202" ns="0" title="Alistar">
            <revisions>
                <rev xml:space="preserve">#REDIRECT [[Alistar the Minotaur]]</rev>
            </revisions>
        </page>
        </pages>
    </query>
</api>

请注意,结果是缓存的,因此如果您想两次获取相同的数据,请确保使用相同的请求并且不要更改元素的顺序,因为新的顺序会导致新的 url 并再次获取结果从他们的服务器而不是提供 API 缓存版本。

更新

查看数组中的值,我看到一些包含引号的“标题”,因此会破坏 url。url encode您应该在使用之前对url 进行编码。

于 2012-05-17T18:23:10.940 回答