带有不同参数的PHP循环函数


PHP Loop Function with different parameters

我有一个函数,它查询api并将响应输出为数组。我可以运行这个函数一次,然后我可以回显数组的输出。

但对我来说问题是,我可以调用这个函数一次并有输出。但我想循环遍历这些函数参数并为多个用户名调用它。例子:

<?php
    require("./include/function.php");
    $Player=fetchCharacterDescriptions("Senaxx", "2");
          echo "<tr>";
            echo "<th class='"col-md-3'">" . $Player[0]['username'] . "</th>";
            foreach ( $Player as $var ) 
                {
                echo "<th class='"col-md-3'">",$var['class']," ",$var['light'],"</th>";
                }
            echo "</tr>";
    echo "</thead>";
    echo "</table>";
?>

这个调用是function。php中的函数fetchCharacterDescriptions也就是

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$hash = array(
        '3159615086' => 'Glimmer',
        '1415355184' => 'Crucible Marks',
        '1415355173' => 'Vanguard Marks',
        '898834093'  => 'Exo',
        '3887404748' => 'Human',
        '2803282938' => 'Awoken',
        '3111576190' => 'Male',
        '2204441813' => 'Female',
        '671679327'  => 'Hunter',
        '3655393761' => 'Titan',
        '2271682572' => 'Warlock',
        '3871980777' => 'New Monarchy',
        '529303302'  => 'Cryptarch',
        '2161005788' => 'Iron Banner',
        '452808717'  => 'Queen',
        '3233510749' => 'Vanguard',
        '1357277120' => 'Crucible',
        '2778795080' => 'Dead Orbit',
        '1424722124' => 'Future War Cult',
        '2033897742' => 'Weekly Vanguard Marks',
        '2033897755' => 'Weekly Crucible Marks',
    );
function translate($x)
{
  global $hash;
  return array_key_exists($x, $hash) ? $hash[$x] : null;
}

//BungieURL
function callBungie($uri)
    {
    $apiKey = '145c4aff30864167ac4548c02c050679';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $uri);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'X-API-Key: ' . $apiKey
    ));
    if (!$result = json_decode(curl_exec($ch) , true))
        {
        $result = false;
        }
    curl_close($ch);
    return $result;
    }
//Request Player    
function fetchPlayer($username, $platform)
    {
    $result = false;
    $uri = 'http://www.bungie.net/Platform/Destiny/SearchDestinyPlayer/' . $platform . '/' . $username;
    $json = callBungie($uri);
    if (isset($json['Response'][0]['membershipId']))
        {
        $result = array(
            'membershipId' => $json['Response'][0]['membershipId'],
            'membershipType' => $platform
        );
        }
    return $result;
    }
//Request characters
function fetchCharacters($username, $platform)
    {
        $result = array();
        if($player = fetchPlayer($username, $platform)) {
            $uri = 'http://bungie.net/Platform/Destiny/'.$player['membershipType'].'/Account/'.$player['membershipId'].'?ignorecase=true';
            if( $json = callBungie($uri) ) {
                foreach ($json['Response']['data']['characters'] as $character) {
                    $result[] = $character;
                }
            }
        }
        return $result;
    }
//Request character descriptions
function fetchCharacterDescriptions($username, $platform)
    {
        $character_descriptions = array();
        if($characters = fetchCharacters($username, $platform)) {
            foreach ($characters as $character) {
                $class = translate($character['characterBase']['classHash']);
                $emblem = $character['emblemPath'];
                $backgroundpath = $character['emblemPath'];
                $level = $character['characterLevel'];
                $character_id = $character['characterBase']['characterId'];
                $light = $character['characterBase']['stats']['STAT_LIGHT']['value'];
                $username = $username;
                $character_descriptions[] = array(
                    'class'=> $class,
                    'emblem'=> $emblem,
                    'backgroundpath'=>$backgroundpath,
                    'character_id' => $character_id,
                    'characterlevel' => $level,
                    'light' => $light,
                    'username' => $username
                );
            }   
        return $character_descriptions;
        }
        return false;
    }
?>

所以我的函数调用是:fetchCharacterDescriptions("Senaxx", "2");,我想添加更多的球员到这(从数组或其他东西),所以我可以请求多个用户名的统计数据。

你只需要循环遍历玩家并为每个玩家执行fetchcharacterdescription。

$players = array(
    "Senaxx" => "2",
    "SomeoneElse" => "2",
);
foreach ($players as $playerName => $platformId) {
    $Player = fetchCharacterDescriptions($playerName, $platformId);
    // do your other stuff
}

请记住,你的网页将加载非常慢,因为每次调用fetchCharacterDescriptions()执行2个curl请求。此外,如果API关闭,您的网站也会有效地(或至少是空白的)。

你可能最好提前获取数据(在一定的间隔),并将其存储到数据库/csv文件或其他东西