찾고있는 페이지 수와 관련하여 작업중인 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";
?>
변경 항목이 동일한 페이지에 모두있다? – bbruman
api가 깨지기를 지원하지 않습니다. –
오케이. 솔직히 나는 정말로 확신하지 못한다. 당신이 google '페이지를 통해 루프 php api'하면 몇 가지 아이디어를 얻어야한다. 죄송합니다 더 많은 도움을 드릴 수가 없습니다 – bbruman