2016-09-23 3 views
0

API를 쿼리하고 응답을 배열로 출력하는 함수가 있습니다. 이 함수를 한 번 실행하면 배열 출력을 반향시킬 수 있습니다.다른 매개 변수가있는 PHP 루프 함수

하지만 문제는 내가이 함수를 한 번 호출하여 출력 할 수 있다는 것입니다. 하지만 이러한 함수 매개 변수를 통해 반복하고 여러 사용자 이름을 호출하고 싶습니다. 예 :에서 fetchCharacterDescriptions("Senaxx", "2"); 그리고 난이 더 많은 선수를 추가하고 싶습니다 (:

<?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; 
    } 

?> 

그래서 내 함수 호출은 다음과 같습니다

<?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입니다 배열 또는 무언가) 그래서 나는 여러 사용자 이름에 대한 통계를 요청할 수 있습니다.

답변

0

플레이어를 반복하면 각자에 대해 fetchCharacterDescriptions를 수행하면됩니다.

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

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

fetchCharacterDescriptions()를 호출 할 때마다 두 번의 메일 요청이 실행되므로 웹 페이지가 느리게로드됩니다. 또한 API가 다운 된 경우 사이트가 효과적입니다 (또는 적어도 비어 있음).

데이터를 미리 가져 와서 (특정 인터벌에서) 데이터베이스/csv 파일이나 다른 것으로 저장하는 것이 좋습니다.

+0

내 질문에 답변 해 주셔서 감사합니다. 나중에 다시 해 볼게. – Senaxx

관련 문제