0

My code is as shown below:

$response = curl_exec($curl);
echo $response;
$err = curl_error($curl);

curl_close($curl);

if ($response['status'] == 200) { 
  echo '<img style="text-align:center"; src="./check-successful.gif" width="100" /><br/><p>Please close the browser</p>';
    sleep(2);
} 
else {
    echo '<img style="text-align:center"; src="./icn-failed-1.gif" width="100" /><br/><p>Please close the browser</p>';
    sleep(2);
}

Here echo gives me this output:

{"status":"200","message":"Your test message"}

but somehow , it is not identifed by if($response['status']) and goes into else statement only.

Mrugesh
  • 4,381
  • 8
  • 42
  • 84
  • 2
    is the response a string? You might need to json_decode it – Hugo Aug 12 '18 at 13:54
  • So "crashes the api" is just hyperbole then? And what's the thought process behind blaiming curl_exec for not implicitly decoding? Was it documented as anything more but a request client anywhere? – mario Aug 12 '18 at 13:58
  • Falling into an `else` is not `crashing` anything ;) The above code is working correctly as written (just not the way you thought it would work hehe). Side note... why on earth are you using `sleep(2);` in there? Surely you do not want that... – IncredibleHat Aug 12 '18 at 14:01

2 Answers2

1

You have to decode this response with json_decode:

$response = json_decode($response, true);

Now you can use $response as array

Nikita Leshchev
  • 1,784
  • 2
  • 14
  • 26
1

you need to decode it first, check my code:

$response = curl_exec($curl);
echo $response;
$decodedResponse = json_decode($response, true);
$err = curl_error($curl);

curl_close($curl);

if ($decodedResponse['status'] == 200) { 
  echo '<img style="text-align:center"; src="./check-successful.gif" width="100" /><br/><p>Please close the browser</p>';
    sleep(2);
} 
else {
    echo '<img style="text-align:center"; src="./icn-failed-1.gif" width="100" /><br/><p>Please close the browser</p>';
    sleep(2);
}

everything should be just fine now.

Ray A
  • 1,283
  • 2
  • 10
  • 22