2011-02-08 80 views
0

我不知道如何從輸出中獲取所有的gamertags,然後將它們插入到mysql表中。插入遊戲歷史記錄頁面中的所有遊戲者標籤。

這是我到目前爲止,閱讀評論...謝謝!

<?php 
//gameID 
$gameid = "469547013"; 
//key to access api 
$apikey = "30cRxVA9J73esG388CzmOXUVRo5VjYhSfI2qBaqcMzs="; 

$url = "http://www.bungie.net/api/reach/reachapijson.svc/game/details/".$apikey."/".$gameid.""; 
$output = file_get_contents($url); 

$obj = json_decode($output); 

//output 
print_r($output); 

//json_decoded output 
print_r($obj); 

//Having a hard time getting a single gamertag 
print $obj->GameDetails->Players->PlayerDetail[0]->gamertag; 

//I guess there needs to be an array or arrays here 
$result = ????????????; 

while ($tag = $result) { 
//value 
$tag = $array['gamertag']; 

//insert each gamertag into table 
mysql_connect('localhost', '', '') or die('Error connecting to MySQL'); 
mysql_select_db(''); 
mysql_query("INSERT IGNORE INTO gamertags(gamertag)VALUES ('".$tag."')"); 

} 

?> 
+1

而不是暴露你的API密鑰給大家,怎麼樣粘貼一些示例數據? – drudge 2011-02-08 20:06:46

+0

輸出超過了一個職位的限制,我不想從網站鏈接..鑰匙將被改變... – AndrewFerrara 2011-02-08 20:11:50

回答

2

檢查了這一點:

$output = file_get_contents('http://www.bungie.net/api/reach/reachapijson.svc/game/details/30cRxVA9J73esG388CzmOXUVRo5VjYhSfI2qBaqcMzs=/469547013'); 
$output = json_decode($output); 
foreach($output->GameDetails->Players as $player) { 
    echo $player->PlayerDetail->gamertag . '<br />'; 
} 

編輯看看http://dbug.ospinto.com/。我通過解碼的json進入它,它告訴我如何得到我所需要的。

0

玩家是一個數組,所以你想是這樣的:通過Players陣列

print $obj->GameDetails->Players[0]->PlayerDetail->gamertag; 

,那麼你將要循環,並得到每個標籤。像這樣:

$ary = new Array(); 
foreach($obj->GameDetails->Players as $player) { 
    $ary[] = $player->PlayerDetail->gamertag; 
}