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';
  }
1
  • I approved the DUPLICATE mark, because I did not json_decode() it in the first place before using it in if-place. Commented Jan 13, 2017 at 18:38

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.