2015-05-14 74 views
0

我在這個階段是新手,對於openID庫也是如此。我想爲Steam用戶創建一個網站,因此我需要顯示他們的廣告資源。我已經下載了大部分東西寫的例子,但我仍然需要其他的東西。作爲一項培訓,我採取了一些與庫存不同的方式,但是相似。沒有成功,所以我希望你能幫助我。帶JSON解碼和蒸汽API的PHP

這就是我在嘗試解碼其他數據時所看到的例子。

{ 
"response": { 
    "players": [ 
     { 
      "steamid": "76hidden", 
      "communityvisibilitystate": 3, 
      "profilestate": 1 
     } 
    ] 

} 

}

和代碼的PHP看起來像

$url2 = file_get_contents("url"); 
$content = json_decode($url2, true); 
$_SESSION['steam_steamid'] = $content['response']['players'][0]['steamid']; 
$steamprofile['steamid'] = $_SESSION['steam_steamid']; 

而上述的這一個運作良好。你能解釋一下爲什麼「蒸汽」和「球員」之間有0?此外,它看起來像會話名稱很重要,我說得對嗎?我正在做一些測試,當我更改名稱會話時,它不起作用。

因此,這裏就是我的 JSON代碼工作:

{ 
    "friendslist": { 
     "friends": [ 
      { 
       "steamid": "765hidden", 
       "relationship": "friend", 
       "friend_since": 1428495026 
      }, 
      { 
       "steamid": "764hidden", 
       "relationship": "friend", 
       "friend_since": 1355599210 
      }, 
      { 
       "steamid": "764hidden", 
       "relationship": "friend", 
       "friend_since": 1423504205 
      }, 
      much more friends 
     ] 

    } 
} 

所以我希望得到steamid和我不知道如何得到它。我一直試圖讓關係,steamID上面已經使用:

$url3 = file_get_contents("url2"); 
$content2 = json_decode($url3, true); 
$_SESSION['steam_relationship'] = $content2['friendslist']['friends'][0]['relationship']; 
$steamfriends['friend'] = $_SESSION['steam_relationship']; 

當然回聲$ steamfriend [「朋友」]不能正常工作。我看到那些製作這些PHP文件的人已經知道會話名稱是什麼(如果他們需要正確工作)。任何想法,我可以在哪裏找到它?

我感到非常慚愧,我無法自己想象它。

問候。

回答

1

第一JSON

{ 
    "response": { 
    "players": [ 
     { 
     "communityvisibilitystate": 3, 
     "profilestate": 1, 
     "steamid": "76hidden" 
     } 
    ] 
    } 
} 

轉化爲這個PHP數組

$content = array(
    "response" => array(
     "players" => array(
      array(
       "communityvisibilitystate" => 3, 
       "profilestate" => 1, 
       "steamid" => "76hidden"    
      ) 
     ) 
    ) 
) 

球員是數組(哈希數組)的數組,所以訪問您需要使用索引號的第一個球員($content["response"]["players"][0]["steamid"]),即使玩家陣列中只有一名玩家。

從第二陣列獲取所有steamid S,你只需要運行一個簡單的foreach:

$friendIds = array(); 
foreach ($content2["friendslists"]["friends"] as $friend) { 
    $friendIds[] = $friend["steamid"]; 
} 
# array("764hidden", "764hidden", "764hidden") 
var_export($friendIds); 
+0

這是相同的像「內容」的東西。沒有什麼更多了。我無法想象如何從朋友名單中得到蒸汽。第一個是一些程序員做的例子,我不能做同樣的事情,但有其他變量。這就是所有 泰勒博士 - 讓蒸汽朋友之一,就像我蒸汽的「球員」是一個,所以只有一個蒸汽。現在有幾個(因爲有很多朋友)。 – user3898922

+0

@ user3898922如果我說得對,你想讓所有玩家都對陣列做出迴應嗎? –

+0

是的,你是對的。 – user3898922

0

當JSON創建他們使用類似的方法:

foreach ($record as $response){ 
    $content['response']['players'][] = $response; 
} 

$ player可能是一個具有多個值的數組,或者不是。

它只是使創建JSON更容易。

首先使JSON數組:

$array = json_decode($json,true) 

然後得到這樣的:

session_start(); 
$_SESSION['relationship'] = $array['friendslist']['friends'][0]['relationship']; 
$_SESSION['steamid'] = $array['friendslist']['friends'][0]['steamid']; 
$_SESSION['steam_relationship'] = $array['friendslist']['friends'][0]['steam_relationship']; 

典型的方式讓所有的玩家信息:

foreach ($array['friendslist']['friends'] as $key => $value){ 

    $steamid = $value['steamid']; 
    $relationship = $value['relationship']; 
    $friend_since = $value['friend_since']; 

     // do what need to be done here. 

} 
+0

你能解釋一下關於會話的事嗎?當我將其從$ session_steamid更改爲$ sessios_steam並像使用它(關於echo和其他東西)那樣使用它時,它不起作用。所以會話名稱很重要,對嗎? – user3898922

+0

我一直這樣做很長一段時間,從來沒有一個很好的理由使用SESSION變量。更好的辦法是問怎麼做你想要做的事情與會話 – Misunderstood

+0

你需要做一個'session_start()'首先保存變量。 – Misunderstood