简体   繁体   English

发送 json 和 php(卷曲)

[英]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 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我尝试过像这样的 php 数组,但仍然错误

$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谢谢

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.有以下错误 - 电话号码必须是字符串,而不是数字,所以它看起来像这样'phone' => '08123456789' ,因为数字不能以 0 开头。

Beside this, there is the following issue - You should not set item_details like this, but rather 'item_details' => [$item] .除此之外,还有以下问题 - 您不应该像这样设置 item_details,而应该设置'item_details' => [$item] You do not need 2 nested arrays, just one.您不需要 2 个嵌套的 arrays,只需一个。 ( 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) array()等于[] 。它们在您的用例中实际上是相同的。所以您正在做类似array(array($item))的事情,这是错误的)

What else, you have to do a $json = json_encode($data2);还有什么,你必须做一个$json = json_encode($data2); at the end and it will return what you want it to.最后,它会返回你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM