0

I can send a json request with this code. But How can I read json request in with php http://www.test.com/post . it is my doman

function try($method_name, $json_data)
{
    $ch = curl_init('http://www.test.com/post');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($json_data)));
    $result = curl_exec($ch);
    curl_close($ch);
    //echo "<pre>$result</pre>";
    $diziyap = json_decode($result);
    if ($diziyap->Success==1) {
        $sonuc = [1,''];
    } else {
        $sonuc = [0,json_decode(json_encode($diziyap->Errors), true)];
    }
    return $sonuc;
}

$sonuc = try('try', $json_data);
echo "<pre>";
echo $sonuc[0];

1 Answer 1

1

To read json with rest post request you need to use file_get_contents('php://input'). Use below code:

$json_data = file_get_contents('php://input');
$data = json_decode($json_data);
print_r($data);
Sign up to request clarification or add additional context in comments.

1 Comment

@nFnK If your problem is solved then you can assign as right answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.