Skip to content
Advertisement

How to modify post response in php

How to modify this response

$url = "https://example.com";
$data = "{"phone_number":"18868768"};
$len = strlen($data);
$headers = array();
$otp = request($url, $data, $headers);

the response is

{"status":0,"msg":"not Found"}

I want to modify to this :

{"status":1,"msg":"Found"}

Advertisement

Answer

I’ll assume you’re saving your response to a $response variable. You need to convert the JSON to an array to manipulate it, then convert it back to JSON. So to change it you would do:

$response = json_decode($response, true);
$response['status'] = 1;
$response['msg'] = 'Found';
$response = json_encode($response);

That being said, you really shouldn’t be encoding your initial JSON in string form. Do this instead:

$data = json_encode([
    'phone_number' => 18868768
]);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement