PHP根据大于/小于JSON输出结果

标签 php json api

我试图让这个脚本根据大于/小于脚本输出结果。当我运行这个脚本时,它会输出文本文件中的第一行。关于我遗漏的任何建议?

<?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
            }
        }
    }
}

最佳答案

好的有两个基本问题。

  1. fopen 调用需要移出循环。
  2. 文件调用有一个烦人的习惯,就是保留尾随的换行符。当你建立你的 url 时,你应该使用 trim($usernumber) 来摆脱它。

这是关于这两件事的更新。

<?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

关于PHP根据大于/小于JSON输出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16780353/

相关文章:

php - 将数组值插入 MySQL

javascript - 解析并构造一个复杂的json

sql - PostgreSQL 在 JSONB 数据中以文本形式查找和替换

javascript - 如何使用 jquery 显示返回的 JSON 数组

php - 使用 PhpActiveRecord 检查 NULL 值

php - fsockopen 返回服务器错误 (0)

api - 从 API 搜索结果中获取推文总数?

api - Moodle 用户注册 API

php - 无法访问子文件夹 | PHP框架

Pandas 数据帧和系列 - IB TWS 历史数据