How to receive JSON POST with PHP

PHP English

Receiving POST JSON data with PHP can be useful in many places and may be a solution to your problems. For example, you want to POST your data to a PHP file with Python and add it to your MySQL database, at this moment we need to capture and parse the POST data with JSON. So how do we get JSON data POSTed with PHP?

How to get JSON data POSTed with PHP?

Let's open our working file and parse the POST Json data in two ways: array and object.

<?php

// Retrieves Raw JSON Data.
$json = file_get_contents('php://input');

// Converts All Data Received via POST to Object.
$object = json_decode($json);

// Converts All Data Arriving via POST to an Array.
$array = json_decode(file_get_contents('php://input'), true);

// Object Usage.
echo $object->title;

// Array Usage.
echo $array["title"];

?>

JSON ile verilerini nasıl POST ederiz?

I will show this part so that we can test our code snippet above.

Let's go to reqbin.com and repeat the image below.

{
  "title" : "Başlık"
}

After pressing the Send button, we will easily receive the Status 200 (OK) message as shown in the picture below and the "Title" value of the "title" field we wrote above.

Thank you for listening to me.


Yorumlar (0)

    Bu yazıya henüz bir yorum yapılmamış! İlk yorum yapan sen ol!