0

I'm trying to check if the following is empty or not.

{"players":""}

I have a function that gets that from an api/site and.. well, heres the code.

function getPlayers($server) {
    // Fetches content from url and json_decodes it
    $playersList = getUrl('http://api.iamphoenix.me/list/?server_ip=' . $server);
    // Attempting to check if it's empty.
    if ($playersList != "") {
        // convert list of comma-separated names into array
        $players = explode(',', $playersList->players);
        foreach ($players as $player) {
            echo '<img title="'.$player.'" src="https://minotar.net/avatar/'.$player.'/32">';
        }
    } else {
        return 'empty';
    }
}

However, using !=, empty(), or isset(), I still get an empty string, example:

https://minotar.net/avatar//32

Where it should be..

https://minotar.net/avatar/Notch/32

When it's empty, I'd like it to just return 'empty'.

I'm not sure what I'm doing wrong. Any ideas?

devs
  • 541
  • 2
  • 13
  • 27
  • Have you tried `if (!empty($player)) {` within the `foreach`? – Dave Chen Dec 04 '13 at 05:11
  • I think the function getUrl returns an object of data not array of data. – HTTP Dec 04 '13 at 05:25
  • possible duplicate of [How to check that an object is empty in PHP](http://stackoverflow.com/questions/9412126/how-to-check-that-an-object-is-empty-in-php) – kenorb Mar 04 '15 at 22:18

5 Answers5

1

In pure php you can check the url segments like

$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', $_SERVER['REQUEST_URI_PATH']);

if($segments[2] == '') { 
}
//or
if(empty($segments[2])) { 
}

//or do your condition

if you are using codeigniter you might say

if(empty($this->uri->segment(2)))

But be sure you loaded the url helper

Hope I understand your question!

HTTP
  • 1,674
  • 3
  • 17
  • 22
0

try this

if (isset($playersList) && is_array($playersList) && !empty($playersList)) {

    // convert list of comma-separated names into array
    $players = explode(',', $playersList->players);
    foreach ($players as $player) {
        echo '<img title="'.$player.'" src="https://minotar.net/avatar/'.$player.'/32">';
    }
} else {
    return 'empty';
}
Punitha
  • 152
  • 1
  • 12
0

You should do this;

print_r($playersList);

just after you set it to see what it actually is. My guess is that you are not getting what you suspect from the getURL call.

Nick M
  • 1,647
  • 13
  • 17
0

Add one more equals sign to take type comparison into account as well

if ($playerList !== '')
Rejinderi
  • 11,694
  • 2
  • 31
  • 40
0

Since you were able to have some output, see my changes in the codes.

function getPlayers($server) {
    // Fetches content from url and json_decodes it
    $playersList = getUrl('http://api.iamphoenix.me/list/?server_ip=' . $server);
    // Attempting to check if it's empty.
    if ($playersList != "") {
        // convert list of comma-separated names into array
        $players = explode(',', $playersList->players);
        // check conversion
        if(is_array($players) && !empty($players){
            foreach ($players as $player) {
                echo '<img title="'.$player.'" src="https://minotar.net/avatar/'.$player.'/32">';
            }
        } else {
            return 'empty';
        }
    } else {
        return 'empty';
    }
}
neythz
  • 84
  • 4