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"]);
    }
2

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually looking deeper, this was the issue.
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);
   }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.