A
A
Andrei St2020-05-08 01:35:07
PHP
Andrei St, 2020-05-08 01:35:07

Why am I not getting a JSON response in the config when I send a GET request using PHP?

The task scheduler sends a request to get ip:port and receives the online_p and max data from JSON and then writes it to the config ...

Cron gives such an error and does not write anything, can someone tell me what's wrong and where did I make a mistake? I've been suffering for half a day, sorry for my broken php language...

Notice: Undefined index: play.mcraft.pw:19132 in /var/www/u0888407/data/www/test.mcraft.pw/cron.php on line 40

CRON
$serversData = [];
foreach ($config['servers'] as $server) {
    $serversData[] = $server['ip'] . '&port=' . $server['port'];
}
$serversData = implode(',', $serversData);

// Если у вас MCPE сервер, замените ниже mc на mcpe
$url = 'https://localhost/?ip=' . $serversData;

$response = @file_get_contents($url);
if ($response) {
    $response = json_decode($response, true);

    foreach ($config['servers'] as &$server) {
        $rData = $response[$server['ip']. '&port=' .$server['port']];
        if ($rData['status'] === true) {
            $server['onlinePlayers'] = $rData['players']['online_p'];
            $server['totalPlayers']  = $rData['players']['max'];
        } else {
            $server['onlinePlayers'] = 0;
            $server['totalPlayers']  = 0;
        }
    }

    savecfg();
}


CONFIG

'servers' => 
  array (
    0 => 
    array (
      'name' => 'TEST',
      'ip' => 'localhost',
      'port' => '19132',
      'password' => '123365',
      'totalPlayers' => 0,  << max 
      'onlinePlayers' => 0,  << online_p
      'showMonitoring' => true,
    ),
  ),


API SCRIPT To get the number of players
<?php
/*

   * MCPEServerPingAPI written by GenesisMiuss

   * Version: 1.0.0

   * Website: https://miuss.org

   * GitHub: https://github.com/miuss/mcapi-pe

*/

header('Content-type:text/json');

$servers = $_GET['ip'];
$port = $_GET['port'];

require_once 'ApiQuery.php';

$json = array(
    'status' => 'success',
    'online' => false,
    'servers' => array(
        'ip' => $servers,
        'port' => $port
    ),
    'motd' => null,
    'version' => array(
        'version' => null,
        'software' => null,
        'plugins' => null
    ),
    'players' => array(
        'online_p' => 0,
        'max' => 0,
    )
);

if ($Info = $Query->GetInfo()) {
    if($Info['GameName']=='MINECRAFTPE'){
        $json = array(
            'status' => 'success',
            'online' => true,
                
            'servers' => array(
                'ip' => $servers,
                'port' => $port
            ),

            'motd' => $Info['HostName'],

            'version' => array(
                'version' => $Info['Version'],
                'software' => $Info['Software'],
                'plugins' => $Info['Plugins']
            ),

            'players' => array(
                'online_p' => $Info['Players'],
                'max' => $Info['MaxPlayers']
            )
        );
    }
}

echo json_encode($json, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
?>


OLD KRON
$serversData = [];
foreach ($config['servers'] as $server) {
    $serversData[] = $server['host'] . '&port=' . $server['port'];
}
$serversData = implode(',', $serversData);

// старый внешний сервис но он уже не рабочий, поэтому было принято решение использовать другой скрипт
$url = 'https://use.gameapis.net/mcpe/query/players/' . $serversData;

$response = @file_get_contents($url);
if ($response) {
    $response = json_decode($response, true);

    foreach ($config['servers'] as &$server) {
        $rData = $response[$server['host']. ':' .$server['port']];
        if ($rData['status'] === true) {
            $server['onlinePlayers'] = $rData['players']['online'];
            $server['totalPlayers']  = $rData['players']['max'];
        } else {
            $server['onlinePlayers'] = 0;
            $server['totalPlayers']  = 0;
        }
    }

    savecfg();
}


Tobish in order: Cron performs the task then checks which server to choose where the true function is enabled, then it selects the same data from the API using json and writes it to the config for the number of online players, but the cron does not do this, that is, an error pops up

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
felony13twelve, 2020-05-08
@felony13twelve

Use the library from xPaw

A
AUser0, 2020-05-08
@AUser0

You would at least check for the existence of data, would ...

foreach ($config['servers'] as &$server) {
        if (!isset($response[$server['host']. ':' .$server['port']])) continue;
        $rData = $response[$server['host']. ':' .$server['port']];
        if (isset($rData['status']) AND $rData['status'] === true) {
            $server['onlinePlayers'] = $rData['players']['online'];
            $server['totalPlayers']  = $rData['players']['max'];
        } else {
            $server['onlinePlayers'] = 0;
            $server['totalPlayers']  = 0;
        }
    }

F
FanatPHP, 2020-05-08
@FanatPHP

If you open your eyes and read the error, then the old cron works for you, not the new one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question