PHP基于大于/小于JSON的结果输出结果

时间:2013-05-27 20:45:37

标签: 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
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

好的,有两个基本问题。

  1. fopen调用需要移出循环。
  2. 文件调用有一个令人讨厌的习惯,就是保留尾随换行符。当你构建你的网址时,你应该使用trim($usernumber)来摆脱它。
  3. 这是一个包含这两件事的更新。

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

答案 1 :(得分:2)

我用CURL做了这个,它也有效。

代码:

$lines = array('76561197992831594','76561197992707820','76561197992146126');
    $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 = curl_download($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
                     file_put_contents("final." . $timestamp . ".txt", $usernumber);
                } //closes if $playtime

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

卷曲下载功能:

function curl_download($Url){

// is cURL installed yet?
if (!function_exists('curl_init')){
    die('Sorry cURL is not installed!');
}

// OK cool - then let's create a new cURL resource handle
$ch = curl_init();

// Now set some options (most are optional)

// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);

// Set a referer
curl_setopt($ch, CURLOPT_REFERER, "http://www.google.pl");

// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla Firefox/1.0");

// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);

// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

// Download the given URL, and return output
$output = curl_exec($ch);

// Close the cURL resource, and free system resources
curl_close($ch);

return $output;

<强>输出:

array(1) { [0]=> object(stdClass)#102 (6) { ["steamid"]=> string(17) "76561197992831594" ["success"]=> int(1) ["backpack_value"]=> float(47.97) ["backpack_update"]=> int(1369683750) ["name"]=> string(8) "WesFox13" ["notifications"]=> int(0) } } array(1) { [0]=> object(stdClass)#106 (6) { ["steamid"]=> string(17) "76561197992707820" ["success"]=> int(1) ["backpack_value"]=> float(59.78) ["backpack_update"]=> int(1369689171) ["name"]=> string(10) "Alexsutton" ["notifications"]=> int(0) } } array(1) { [0]=> object(stdClass)#98 (6) { ["steamid"]=> string(17) "76561197992146126" ["success"]=> int(1) ["backpack_value"]=> float(36181.59) ["backpack_update"]=> int(1369689000) ["name"]=> string(25) ":HIT: Bobo the Monkey Boy" ["notifications"]=> int(0) } }

我建议你在想要从网上下载东西时使用CURL。此外,使用 file_get_contents() file_put_contents()语法很短,并且它们也一样,并且更易于使用。