【问题标题】:PHP Loop Function with different parameters具有不同参数的 PHP 循环函数
【发布时间】:2016-09-23 23:23:44
【问题描述】:

我有一个查询 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");,我想向它添加更多玩家(从数组或其他东西)所以我可以请求多个用户名的统计信息。

【问题讨论】:

    标签: php arrays function loops


    【解决方案1】:

    您只需要循环播放器并为每个播放器执行 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 文件或其他文件中。

    【讨论】:

    • 感谢您抽出宝贵时间回答我的问题。我稍后会试试这个
    猜你喜欢
    • 2016-10-17
    • 2022-01-06
    • 1970-01-01
    • 2011-05-30
    • 2014-12-04
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多