繁体   English   中英

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"); 并且我想为此添加更多的玩家(从数组之类的东西),所以我可以请求多个用户名的统计信息。

您只需要在播放器上循环播放每个播放器的fetchCharacterDescriptions。

$players = array(
    "Senaxx" => "2",
    "SomeoneElse" => "2",
);

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

请记住,由于每次调用fetchCharacterDescriptions()都会执行2个curl请求,因此您的网页加载缓慢。 另外-如果API关闭,则您的网站也会有效(或至少为空白)。

您最好事先(以一定间隔)获取数据并将其存储到数据库/ csv文件或其他内容中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM