1

Getting "undefined index wars" on command and it was working before. But now when I run the command on a moved server it just errors out. I don't know if I'm missing an obvious answer or what.

    public function __construct(int $numWars = 500)
    {
        $client = new PWClient();
        $json = $client->getPage("http://game.com/api/wars/{$numWars}/?key=".env("API_KEY"));
        $decoded = \json_decode($json, true);
        $this->result = Collection::make($decoded["wars"]);
    }
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
HeyItsAlex
  • 13
  • 3
  • 1
    Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – atymic Jul 23 '19 at 04:31
  • Possible duplicate of [Undefined index Laravel](https://stackoverflow.com/questions/48924794/undefined-index-laravel) – Uddyan Semwal Jul 23 '19 at 04:54

2 Answers2

1

You are getting that error because the key wars doesn't exist on the response.

Most likely, the API you are querying has changed it's response format. You should check the documentation for it, and update your code.

atymic
  • 3,093
  • 1
  • 13
  • 26
0

Your $json is missing something:

Try this:

public function __construct(int $numWars = 500)
{
   $client = new PWClient();

   $key = env("API_KEY");
   $json = $client->getPage("http://game.com/api/wars/{$numWars}/?key={$key}");

   $decoded = json_decode($json, true);

   if(!empty($decoded['wars'])){
       $this->result = Collection::make($decoded["wars"]);
   }else{
       dump('decode variable does not have wars key or is empty:');
       dd($decoded);
   }

}
Ezequiel Fernandez
  • 954
  • 11
  • 18