Reputation: 13
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"]);
}
Upvotes: 1
Views: 1027
Reputation: 1074
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);
}
}
Upvotes: 0
Reputation: 3128
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.
Upvotes: 1