javascript
  • php
  • jquery
  • 2016-03-28 62 views 0 likes 
    0

    http://roblox.plus:2052/inventory?username=joxxi如何從此API的數組中獲得特定值?

    它顯示一個大的長陣列。

    第一部分是:

    {"id":107484852,"username":"Joxxi","bc":"BC","rap":643 
    

    我怎麼想不顧一切,但643

    我使用的API在PHP腳本是這樣的:現在

    echo "<td><object height='20' width='100' data='http://roblox.plus:2052/inventory?username=".$row['username']."'/></td>"; 
    

    它只是迴應陣列

    +0

    爲什麼你不使用'file_get_contents'? – madalinivascu

    +0

    你想在'''data'屬性中得到結果'643',還是隻想把js var或php var? – Sean

    +0

    不知道我不是最好的PHP。 –

    回答

    0

    嘗試file_get_contents

    $page = file_get_contents('http://roblox.plus:2052/inventory?username='.$row['username']); 
    echo json_decode($page,true)['rap']; 
    
    +0

    是我的連結權嗎? –

    +0

    $ page = file_get_contents(「http://roblox.plus:2052/inventory?username='」。$ row ['username']。「'」); –

    +0

    由於某種原因,它不顯示643它顯示0當回聲 –

    0

    簡單做

    $result = file_get_contents('http://roblox.plus:2052/inventory?username=joxxi'); 
    

    然後變換結果爲關聯數組

    $row = json_decode($result, true); 
    

    稍後,您可以重複但在此之前要確保行不是數組的集合如果這樣返回第一個

    if (count ($row)) { 
        $row = $row[0]; 
    } 
    echo $row['rap'] 
    

    希望它有幫助

    0

    我也注意到比較curl和file_get_contents的執行時間有所不同。您還可以使用下面的捲曲代碼來獲取所需的數據。

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_URL, 'http://roblox.plus:2052/inventory?username=joxxi'); 
    $resultCurl = curl_exec($ch); 
    $obj = json_decode($resultCurl); 
    echo $obj->rap;