curl_exec is not able to identify json string and crashes the api [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



curl_exec is not able to identify json string and crashes the api [duplicate]



This question already has an answer here:



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.


if($response['status'])



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





is the response a string? You might need to json_decode it
– Hugo
Aug 12 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 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 at 14:01



else


crashing


sleep(2);




2 Answers
2



You have to decode this response with json_decode:


json_decode



$response = json_decode($response, true);


$response = json_decode($response, true);



Now you can use $response as array


$response



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.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard