I am getting api response in plain text format. So operation with response data I need to encode the response into JSON format. Then I can easily grab the desire response data and use it.
Request CODE(Sample) ::
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://api.arshohag.me/test",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "login=testapp&key=152456&md5=chasdg4as432&action=test",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Response CODE(Sample) ::
id=2566546
authentication_key=74448975
error_code=0
error_txt=Test Ok
I want to encode The response is in JSON format like this -
{
"id" : "2566546",
"authentication_key" : "74448975",
"error_code" : "0",
"error_txt" : "Test Ok"
}
and also grab the data like this -
$id=array["id"];
echo $id;