-2

Could someone help with the following i'm trying to parse out some results from an API at https://www.amee.com/api#/documentation, im not exactly a guru at this so would like some help.

I can retrieve all the data using var_dump in what looks like json but when trying to parse it out in html i get the below error

Notice
: Trying to get property 'Company' of non-object in
C:\xampp\htdocs\smbprotool\api\funcions.php
on line
90


Warning
: Invalid argument supplied for foreach() in
C:\xampp\htdocs\smbprotool\api\functions.php
on line
90

This is my code on my functions.php file

function get_company_detail_amee() {

  $url = 'https://www.amee.com//api/companies?min_annual_sales_local=1000000';

  $username = '******';
  $password = '******';

  $ch = curl_init($url);

  curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  $response = curl_exec($ch);
  $array = json_decode($response, true);

  foreach($array->Company as $p){

    echo "<h6> ". $p->amee_company_id ."</h6>";

  }

}

Any help is appreciated

EggNBacon
  • 42
  • 6
  • What does `$response` contain (something like `echo $response;`) – Nigel Ren Aug 22 '20 at 15:58
  • remove true from [json_decode()](https://www.php.net/manual/en/function.json-decode.php).. – Lawrence Cherone Aug 22 '20 at 16:03
  • @NigelRen - its the CURL request – EggNBacon Aug 22 '20 at 16:12
  • @LawrenceCherone - i removed that and am getting "Notice : Undefined property: stdClass::$Company" along with the for each invalid argument i stated originally – EggNBacon Aug 22 '20 at 16:13
  • please do `print_r($response);` its not what you think it is, obviously when I hit that url it shows `{"errors":["API authentication failed: no credentials provided"]}`.. but if your creds are what they are or wrong.. then your wont be getting `{Company:..}` back, you should implement handling errors from the api – Lawrence Cherone Aug 22 '20 at 16:15
  • @LawrenceCherone i used print_r and the response was received, i removed my credentials above for security the response was this [![amee-response.png](https://i.postimg.cc/K8BdkBqS/amee-response.png)](https://postimg.cc/4Y48j7RB) – EggNBacon Aug 22 '20 at 21:33
  • Does this answer your question? [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Mike Doe Aug 22 '20 at 22:01

1 Answers1

0

change:

$array = json_decode($response, true);
foreach($array->Company as $p){

to

$array = json_decode($response);
foreach($array->companies as $p){
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • Thank you very much, looks like that worked so looks like i had Company instead of companies and decode set to true in which this should not be. :) – EggNBacon Aug 22 '20 at 22:14