Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 以json格式发送带有curl的多维数组_Php_Arrays_Json_Api_Curl - Fatal编程技术网

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

Php 以json格式发送带有curl的多维数组,php,arrays,json,api,curl,Php,Arrays,Json,Api,Curl,我试图使用curl将数据发布到api,它必须是JSON格式的。 现在我有了一个多维数组,我需要随帖子一起传递。但我总是犯同样的错误,我不知道为什么 我已经尝试了我能想象到的所有可能的方法 这是我需要发送到API的示例: POST /orders/ HTTP/1.1 Authorization: Basic aHVudGVyMjo= Content-Type: application/json { "currency": "EUR", "amount": 99, "retu

我试图使用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'" } }
有人能告诉我它从哪里来,我做错了什么吗? 它是数组的格式还是什么?

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


$post_字段[“交易”][“付款方法详细信息”][“发卡机构id”]=数组(“INGBNL2A”); 通过检查 看起来事务应该是一个数组

$post_fields=array();
$post_字段[“货币”]=“欧元”;
$post_字段[“金额”]=99;
$post_字段[“返回_url”]=”http://website_url.nl/return_page/";
$post_字段[“交易”][0][“付款方式”]=“理想”;
$post_字段[“交易”][1][“付款方法_详细信息”][“发卡机构_id”]=“INGBNL2A”;
回显“”。json_编码($post_字段)。“”;
/*输出。。。
{
“货币”:“欧元”,
“金额”:99,
“返回url”:“http:\/\/website\u url.nl\/return\u page\/”,
“交易”:[
{
“付款方式”:“理想”,
“付款方式详情”:{
“发卡机构id”:“INGBNL2A”
}
}
]
}

*/
谢谢您的回答,但不幸的是,这不起作用。我得到以下响应:{“error”:{“status”:400,“type”:“,“value”:“{u'payment_method”:u'ideal',u'payment_method_details':{u'issuer_id':[u'INGBNL2A']}}不是类型u'array'}如果你读到这个错误,它说传递的整个json字符串不是数组。正如在
{u'payment\u method':u'ideal',u'payment\u method\u details':{u'issuer\u id':u'INGBNL2A'}不是类型u'array'
@JonathanKuhn Oke中一样,我理解这一点,但我如何才能将其转换为数组呢?因为我认为它已经是一个数组了,因为它是JSON convert$post_fields=array($post_fields)?通常,您需要提供一个http查询字符串或一个php数组,该字符串或数组将转换为http查询字符串。@JonathanKuhn您是想说我不需要将其作为JSON发送吗?谢谢,这确实有效,我现在已经得到了另一个答案。但这会导致以下结果:非类型对象没有属性“endswith”