検索するページの数に関して、作業している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のページをループする場合は、いくつかのアイデアを得る必要があります。申し訳ありませんが、私はもっと助けになることはできません – bbruman