1

This may sound DUPLICATE, but I tried all the answers with this same question.

I am using this API, https://smartystreets.com/docs/cloud/us-street-api

My problem is, the output of their INVALID ADDRESS is

[ ]

It is also stated in their documentation.

Now, this is my code but it's not working.

$url = 'SOME_API_HERE'  ;

  $url = str_replace(" ", '%20', $url);
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $result = curl_exec($ch);
  curl_close($ch);      

 $json =  json_decode($result, true);
  if (!empty($json))
  {
    return json_decode($result, true);
  }
  else {
    return 'invalid address';
  }
Vahn Marty
  • 1,428
  • 3
  • 14
  • 28
  • I approved the DUPLICATE mark, because I did not json_decode() it in the first place before using it in if-place. – Vahn Marty Jan 13 '17 at 18:38

1 Answers1

2

Convert JSON to an array first and then check if this array is empty or not:

$data = json_decode($result, true);
return empty($data) ? 'invalid address' : $data;
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279