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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?