0

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
  • Typically you need to supply CURLOPT_POSTFIELDS a http query string or a php array that it will turn into a http query string. Commented Nov 20, 2015 at 21:41
  • @JonathanKuhn Are you trying to say that I do not need to send that as JSON ? Commented Nov 20, 2015 at 21:45

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this is working indeed, I got already an other answer now. But this is resulting the following: NoneType' object has no attribute 'endswith'"
0

I suggest that param "issuer_id" should be an array, in that case try

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

4 Comments

Thank you for your answer, but unfortunatly that doesn't work. I get the following response: { "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'" } }
If you read the error, it is saying the entire json string being passed is not an array. As in {u'payment_method': u'ideal', u'payment_method_details': {u'issuer_id': u'INGBNL2A'}} is not of type u'array'.
@JonathanKuhn Oke, I understand that, but how am I able to convert it to an array then ? Cause I tought it was already an array because it was an array before the JSON convert
$post_fields = array($post_fields); ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.