dtebrq0245 2015-11-20 21:31
浏览 216
已采纳

以json格式发送带curl的多维数组

I am trying to post data using curl to a api, it has to be in JSON. Now I have an multidimensional array that i need to pass along with the post. But i keep getting the same error all over, and i can not figure out why.

I've tried every possible way I could imagine.

This is the example that i need to send to the API:

POST /orders/ HTTP/1.1
Authorization: Basic aHVudGVyMjo=
Content-Type: application/json
{
    "currency": "EUR",
    "amount": 99,
    "return_url": "http://www.example.com/",
    "transactions": [
      {
       "payment_method": "ideal",
       "payment_method_details": {
           "issuer_id": "INGBNL2A"
        }
      }
     ]
}

So my array I made like this:

$post_fields = array();
$post_fields["currency"] = "EUR";
$post_fields["amount"] = 99;
$post_fields["return_url"] = "http://website_url.nl/return_page/";
$post_fields["transactions"]["payment_method"] = "ideal";
$post_fields["transactions"]["payment_method_details"]["issuer_id"] = "INGBNL2A";

Then the follinw i do is converting the array to a JSON string by this code:

$data_string = json_encode($post_fields);

So far is everything OK, but then i am going to post the data to the API by using the following code:

$url = "https://api.kassacompleet.nl/v1/orders/";

$curl_header = array();
$curl_header[] = "Authorization: Basic ".base64_encode("$auth_code:");
$curl_header[] = "Content-type: application/json";

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 180);
curl_setopt($ch, CURLOPT_TIMEOUT, 180);

$output = curl_exec($ch);

curl_close($ch);

print_r($output);

And this always results in an error that looks as the following:

{ "error": { "status": 400, "type": "", "value": "{u'payment_method': u'ideal', u'payment_method_details': {u'issuer_id': u'INGBNL2A'}} is not of type u'array'" } }

Could someone tell me where it comes from and what I am doing wrong? Is it the format of the array or what is it?

  • 写回答

2条回答 默认 最新

  • dongye1934 2015-11-20 21:44
    关注

    From checking the API documentaion It looks like transactions should be an array.

    $post_fields = array();
    $post_fields["currency"] = "EUR";
    $post_fields["amount"] = 99;
    $post_fields["return_url"] = "http://website_url.nl/return_page/";
    $post_fields["transactions"][0]["payment_method"] = "ideal";
    $post_fields["transactions"][1]["payment_method_details"]["issuer_id"] = "INGBNL2A";
    
    echo  '<pre>'.json_encode($post_fields).'</pre>';
    
    /* outputs...
    {
     "currency":"EUR",
     "amount":99,
     "return_url":"http:\/\/website_url.nl\/return_page\/",
     "transactions":[
     {
     "payment_method":"ideal",
     "payment_method_details":{
        "issuer_id":"INGBNL2A"
        }
      }
      ]
    }
    */

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加