【问题标题】:sending multidimensional array with curl in json format以json格式发送带有curl的多维数组
【发布时间】:2015-11-20 21:31:17
【问题描述】:

我正在尝试使用 curl 将数据发布到 api,它必须是 JSON。 现在我有一个多维数组,我需要将它与帖子一起传递。但是我总是遇到同样的错误,我不知道为什么。

我已经尝试了所有我能想到的方法。

这是我需要发送到 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"
        }
      }
     ]
}

所以我的数组是这样的:

$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";

然后我要做的就是通过以下代码将数组转换为 JSON 字符串:

$data_string = json_encode($post_fields);

到目前为止一切正常,但接下来我将使用以下代码将数据发布到 API:

$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);

这总是会导致如下所示的错误:

{ "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'" } }

谁能告诉我它来自哪里以及我做错了什么? 是数组的格式还是什么?

【问题讨论】:

  • 通常你需要提供CURLOPT_POSTFIELDS一个http查询字符串或一个php数组,它将变成一个http查询字符串。
  • @JonathanKuhn 你是想说我不需要将它作为 JSON 发送吗?

标签: php arrays json api curl


【解决方案1】:

通过检查API documentaion 看起来 transactions 应该是一个数组。

$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"
    }
  }
  ]
}
*/

【讨论】:

  • 谢谢你这确实有效,我现在已经得到了另一个答案。但这会导致以下结果:NoneType' 对象没有属性 'endswith'"
【解决方案2】:

我建议参数“issuer_id”应该是一个数组,在这种情况下试试

...
$post_fields["transactions"]["payment_method_details"]["issuer_id"] = array("INGBNL2A"); ...

【讨论】:

  • 感谢您的回答,但不幸的是这不起作用。我得到以下响应:{“错误”:{“状态”:400,“类型”:“”,“价值”:“{u'payment_method':u'ideal',u'payment_method_details':{u'issuer_id ': [u'INGBNL2A']}} 不是 u'array' 类型” } }
  • 如果您阅读了错误,则表示传递的整个 json 字符串不是数组。如{u'payment_method': u'ideal', u'payment_method_details': {u'issuer_id': u'INGBNL2A'}} is not of type u'array'
  • @JonathanKuhn Oke,我明白这一点,但我怎么能把它转换成数组呢?因为我认为它已经是一个数组,因为它在 JSON 转换之前是一个数组
  • $post_fields = array($post_fields); ?
猜你喜欢
  • 2015-01-31
  • 2016-07-03
  • 1970-01-01
  • 2016-12-12
  • 2014-09-05
  • 2016-05-22
  • 1970-01-01
  • 2016-08-25
  • 2018-06-13
相关资源
最近更新 更多