Skip to content
Advertisement

send post json with php (curl)

i done search tutorial for send post json with curl .. but for this value i cant find in here.. and my question how to convert to array post json in value if like this, and this my value post json

   {
      "payment_type": "bca_klikpay",
      "transaction_details": {
        "order_id": "orderid-01",
        "gross_amount": 11000
      },
      "item_details": [
        {
          "id": "1",
          "price": 11000,
          "quantity": 1,
          "name": "Mobil "
        }
      ],
      "customer_details":{
        "first_name": "John",
        "last_name": "Baker",
        "email": "john.baker@email.com",
        "phone": "08123456789"
      },
      "bca_klikpay": {
        "description": "Pembelian Barang"
      }
    }

i done try to php array like this but still error

$item = array('id' => 'id1', 'price' => 11000, 'quantity' => 1 , 'name' => 'Mobil');

$data2 =array('payment_type' => 'bca_klikpay', 
'transaction_details' => array('order_id' => 'orderid-01', 'gross_amount' => 11000),
'item_details' => array([$item]),
'customer_details'=> array('first_name' => 'john',
'last_name' => 'baker', 'email' => 'john.baker@email.com', 'phone' => 08123456789),
'bca_klikpay' => array('description' => 'Pembelian Barang'));

maybe someone can help me.. and sory for my bad english

thanks

Advertisement

Answer

There is the following error – the phone number has to be string, not number, so it would look like this 'phone' => '08123456789', because numbers cannot begin with 0.

Beside this, there is the following issue – You should not set item_details like this, but rather 'item_details' => [$item]. You do not need 2 nested arrays, just one. (array() is equal to []. They are practically the same in your use case. So you are doing something like array(array($item)), which is wrong)

What else, you have to do a $json = json_encode($data2); at the end and it will return what you want it to.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement