2016-10-12 3 views
0

그래서 기본적으로이 API의 모든 단일 페이지의 AveragePrice의 합계를 얻으려고합니다. 지금 당장은 첫 페이지 만 얻었습니다. 나는 시도한 것들이 무한 루프로 뭉개져 버린 것입니다. 1 페이지 작업에 대한 내 코드를 Heres.PHP이 API 페이지를 어떻게 반복합니까?

저는 어떻게해서 페이지를 반복하고 모든 페이지를 합칠 수 있는지 확실하지 않습니다.

<?php  
    function getRap($userId){ 
     $url = sprintf("https://www.roblox.com/Trade/InventoryHandler.ashx?userId=" . $userId . "&filter=0&page=1&itemsPerPage=14"); 
     $results = file_get_contents($url); 
     $json = json_decode($results, true); 

     $data = $json['data']['InventoryItems'];      
     $rap = 0; 

     foreach($data as $var) { 
      $rap += $var['AveragePrice']; 
     } 

     echo $rap; 
    } 

    $userId = 1; 
    getRap($userId); 
?> 
+0

변경 항목이 동일한 페이지에 모두있다? – bbruman

+0

api가 깨지기를 지원하지 않습니다. –

+0

오케이. 솔직히 나는 정말로 확신하지 못한다. 당신이 google '페이지를 통해 루프 php api'하면 몇 가지 아이디어를 얻어야한다. 죄송합니다 더 많은 도움을 드릴 수가 없습니다 – bbruman

답변

0

찾고있는 페이지 수와 관련하여 작업중인 API를 살펴보면 더 좋은 답변을 얻을 수 있습니다. 최대 페이지를 칠 때까지 반복하고 싶습니다. 요청 결과에 존재하지 않는 페이지 (예 : 결과가 더 이상 없음)를 요청했음을 알려주는 값이 있어야합니다. total number of results to search for을 얻을 수 있다면 그걸로 for 루프를 할 수 있습니다. 이 항목의 총 수를 얻을 수없는 경우 상태를 실패하면서

//Change the function to accept the page number as a variable 
function getRap($userId, $i){ 
      $url = sprintf("https://www.roblox.com/Trade/InventoryHandler.ashx?userId=" . $userId . "&filter=0&page=" . $i . "&itemsPerPage=14"); 

//work out how many pages it takes to include your total items 
// ceil rounds a value up to next integer. 
// ceil(20/14) = ceil(1.42..) == 2 ; It will return 2 and you will look for two pages 
$limit = ceil($totalItems/$itemsPerPage); 

// Then loop through calling the function passing the page number up to your limit. 
for ($i = 0; $i < $limit; $i++) { 
    getRap($userId, $i); 
} 

, 당신은 루프

// look for a fail state inside your getRap() 
function getRap($userId, $i) { 
    if ($result = error) { //you will have to figure out what it returns on a fail 
     $tooMany = TRUE; 
    } 
} 

for ($i = 0; $tooMany !== TRUE ; $i++) { 
    getRap($userId, $i); 
} 

편집

발생하지 않은 수 :는 내부의 실패 상태를 찾고, 내 대답을 검토하여 함수가 형편 없습니다 (이 경우 변수의 범위 때문에 작동하지 않습니다). 변수를 앞뒤로 전달할 수 있지만 그 부분은 나에게 맡깁니다.

총계를 얻으려면 함수가 결과 (echo $rap)를 인쇄하지 않지만 나중에 사용할 수 있도록 반환하십시오. 그래서 URL에 페이지 당

전체 예를

<?php  
function getRap($userId, $i){ 
    $url = sprintf("https://www.roblox.com/Trade/InventoryHandler.ashx?userId=" . $userId . "&filter=0&page=" . $i . "&itemsPerPage=25"); 
    $results = file_get_contents($url); 
    $json = json_decode($results, true); 
    if ($json['msg'] == "Inventory retreived!") { 
     $data = $json['data']['InventoryItems'];      
     $rap = 0; 

     foreach($data as $var) { 
      $rap += $var['AveragePrice']; 
     } 

     return $rap; 
    } else { 
     return FALSE; 
    } 
} 

$total = 0; 
$userId = 1; 
for ($i = 0; $i < 1000 /*arbitrary limit to prevent permanent loop*/ ; $i++) { 
    $result = getRap($userId, $i); 
    if ($result == FALSE) { 
     $pages = $i; 
     break; 
    } else { 
     $total += getRap($userId, $i); 
    } 
} 
echo "Total value of $total, across $pages pages"; 
?> 
+0

각 페이지의 결과를 추가하는 것이 좋지 않은 각 페이지를 인쇄하는 것만 큼 어떨까요? –

+0

@RyanWilliams 결과에서 모든 AveragePrice의 평균을 구하고 싶습니까? – Luke

+0

@RyanWilliams 모든 평균 가격의'total'을 원한다면'$ rap + = $ var [ 'AveragePrice'];가되어야합니다. – Luke

관련 문제