1

public function verifyotp(Request $request) { $mobile=$request->mobilenumber;

$otp=$request->otp;


$client = new \GuzzleHttp\Client();
$tem='https://www.smsalert.co.in/api/mverify.json?apikey=#&mobileno=';
$cod='&code=';
$msg=($tem.$mobile.$cod.$otp);
$res=$client->request('POST', $msg);
(string) $res->getBody();


if($res->getBody(['description']=== "['desc']:Code does not match.")
{
    echo 'Code Not Matched';
}

elseif(($res->getBody(['description'] === "['desc']:Code Matched successfully."))
{
    echo  'Code Matched;
}

}

I have get response from api.I got Two response

one is like this

{ "status": "success", "description": { "desc": "Code does not match." } }

Second like this

{ "status": "success", "description": { "desc": "Code matched successfully." } } In This response i want to read "desc" on my controller

How to check desc using if condition ?

Selva
  • 33
  • 3
  • 1
    Please add more information about your question. Information such as the API you use and a code snippet of what you tried will be useful :) – Harshana Oct 18 '21 at 06:01
  • Does this answer your question? [How to extract and access data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php) – Don't Panic Oct 18 '21 at 09:36

1 Answers1

1

You can use json_decode function like this :

$result = json_decode($response);
echo $result->description->desc;

see this for detail

Done...

Amir Kaftari
  • 1,305
  • 14
  • 13
  • json_decode(): Argument #1 ($json) must be of type string, GuzzleHttp\Client given THis is the error I got. – Selva Oct 18 '21 at 06:38
  • json is a kind of string. if you're response is a json type , you can parse this with json_decode. – Amir Kaftari Oct 18 '21 at 07:17