3

このスクリプトに、大なり/小なりスクリプトに基づいて結果を出力させようとしています。このスクリプトを実行すると、テキスト ファイルの最初の行が出力されます。私が欠けているものに関する提案はありますか?

<?php
$lines     = file('unique.txt'); // Reads the file with the list of user numbers
$timestamp = time(); // Defines time for below renaming

foreach ($lines as $usernumber) { // Loops line by line
    $link = 'http://backpack.tf/api/IGetUsers/v2/?&steamids=' . $usernumber . '&format=json';
    $json = file_get_contents($link); // Reads link (this ^)
    $data = json_decode($json); // Defines decode as json
    if (!empty($data)) {
        $profiles = array(); //init array so we can use $profiles[] later
        foreach ($data->response->players as $player) { // Loop thrugh all the players
            $player2 = $player->backpack_value;
            if ($player2 < 9999999) { // Check the backpack_value
                $profiles[] = $player; // Assign the required players to a new array

                var_dump($profiles); // Dump the array to browser for debugning
                $fh = fopen("final." . $timestamp . ".txt", 'a') or die("can't open file"); // Opens final.txt to write in
                fwrite($fh, $usernumber); // Writes the parsed results to final.txt
            } //closes if $playtime

        } //closes foreach $data
    } //closes if !empty
    else {
        echo $data;
    }
} //closes foreach $lines

?>

Unique.txt が含まれています

76561197992831594
76561197992707820
76561197992146126
76561197992694522
76561197992707820
76561197992831594

JSON の例

{
    "response": {
        "success": 1,
        "current_time": 1369685515,
        "players": {
            "0": {
                "steamid": "76561197992831594",
                "success": 1,
                "backpack_value": 47.97,
                "backpack_update": 1369683750,
                "name": "WesFox13",
                "notifications": 0
            }
        }
    }
}
4

2 に答える 2

2

わかりました 2 つの根本的な問題があります。

  1. fopen 呼び出しは、ループの外に移動する必要があります。
  2. ファイル呼び出しには、末尾の改行を保持するという厄介な習慣があります。URL を構築するときは、それを削除するために使用する必要があります trim($usernumber)

これら 2 つの項目を含む更新プログラムを次に示します。

<?php
$lines     = file('unique.txt'); // Reads the file with the list of user numbers
$timestamp = time(); // Defines time for below renaming

$fh = fopen("final." . $timestamp . ".txt", 'a') or die("can't open file"); // Opens final.txt to write in
foreach ($lines as $usernumber) { // Loops line by line
    $link = 'http://backpack.tf/api/IGetUsers/v2/?&steamids=' . trim($usernumber) . '&format=json';
    $json = file_get_contents($link); // Reads link (this ^)
    $data = json_decode($json); // Defines decode as json
    print_r($json);
    if (!empty($data)) {
        $profiles = array(); //init array so we can use $profiles[] later
        foreach ($data->response->players as $player) { // Loop thrugh all the players
            $player2 = $player->backpack_value;
            if ($player2 < 9999999) { // Check the backpack_value
                $profiles[] = $player; // Assign the required players to a new array

                var_dump($profiles); // Dump the array to browser for debugning
                fwrite($fh, $usernumber); // Writes the parsed results to final.txt
            } //closes if $playtime

        } //closes foreach $data
    } //closes if !empty
    else {
        echo $data;
    }
} //closes foreach $lines
于 2013-05-27T21:05:43.407 に答える