Php - Json -> Object value found, but an array is required
Ik probeer een koppeling te maken met MyParcel via JSON, maar krijg steeds de melding:
Object value found, but an array is required
Zo moet het worden:
Quote:
POST https://api.myparcel.nl/shipments HTTP/1.1
Content-Type:application/vnd.shipment+json;charset=utf-8 Authorization:basic **knip**
{
"data":{
"shipments":[
{
"recipient":{
"cc":"NL",
"city":"Hoofddorp",
"street":"Hoofdweg",
"number":"679",
"postal_code":"2131BC",
"person":"Mr. Parcel",
"phone":"0213030315",
"email":"[email protected]"
},
"options":{
"package_type":1,
"only_recipient":1,
"signature":1,
"return":1,
"insurance":{
"amount":50000,
"currency":"EUR"
},
"large_format":0
},
"carrier":1
},
{
"recipient":{
"cc":"NL",
"city":"Amsterdam",
"street":"Dorpstraat",
"number":"123",
"postal_code":"1020BC",
"person":"Mrs. Parcel",
"phone":"02012343546",
"email":"[email protected]"
},
"options":{
"package_type":1,
"only_recipient":0,
"signature":0,
"return":0
},
"carrier":1
}
]
}
}
Content-Type:application/vnd.shipment+json;charset=utf-8 Authorization:basic **knip**
{
"data":{
"shipments":[
{
"recipient":{
"cc":"NL",
"city":"Hoofddorp",
"street":"Hoofdweg",
"number":"679",
"postal_code":"2131BC",
"person":"Mr. Parcel",
"phone":"0213030315",
"email":"[email protected]"
},
"options":{
"package_type":1,
"only_recipient":1,
"signature":1,
"return":1,
"insurance":{
"amount":50000,
"currency":"EUR"
},
"large_format":0
},
"carrier":1
},
{
"recipient":{
"cc":"NL",
"city":"Amsterdam",
"street":"Dorpstraat",
"number":"123",
"postal_code":"1020BC",
"person":"Mrs. Parcel",
"phone":"02012343546",
"email":"[email protected]"
},
"options":{
"package_type":1,
"only_recipient":0,
"signature":0,
"return":0
},
"carrier":1
}
]
}
}
En dit probeer ik:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$responce['data'] = array(
"shipments" => array(
"recipient" => array(
"cc" => "NL",
"city" => "Hoofddorp",
"street" => "Hoofdweg",
"number" => "679",
"postal_code" => "2131BC",
"person" => "Mr. Parcel",
"phone" => "01234565789",
"email" => "[email protected]"),
"options" => array(
"package_type" => 1,
"only_recipient" => 1,
"signature" => 1,
"return" => 1,
"insurance" => array(
"amount" => 5000,
"currency" => "EUR"),
"large_format" => 1),
"carrier" => 1
)
);
$data_string = json_encode($responce);
"shipments" => array(
"recipient" => array(
"cc" => "NL",
"city" => "Hoofddorp",
"street" => "Hoofdweg",
"number" => "679",
"postal_code" => "2131BC",
"person" => "Mr. Parcel",
"phone" => "01234565789",
"email" => "[email protected]"),
"options" => array(
"package_type" => 1,
"only_recipient" => 1,
"signature" => 1,
"return" => 1,
"insurance" => array(
"amount" => 5000,
"currency" => "EUR"),
"large_format" => 1),
"carrier" => 1
)
);
$data_string = json_encode($responce);
Gewijzigd op 29/01/2017 11:09:32 door - Ariën -
PS: Het is response ;-)
{"data":{"shipments":{"recipient":{"cc":"NL","city":"Hoofddorp","street":"Hoofdweg","number":"679","postal_code":"2131BC","person":"Mr. Parcel","phone":"01234565789","email":"[email protected]"},"options":{"package_type":1,"only_recipient":1,"signature":1,"return":1,"insurance":{"amount":5000,"currency":"EUR"},"large_format":1},"carrier":1}}}
En de complete error:
object(stdClass)#1 (2) { ["errors"]=> array(1) { [0]=> object(stdClass)#2 (3) { ["code"]=> int(0) ["fields"]=> array(1) { [0]=> string(14) "data.shipments" } ["human"]=> array(1) { [0]=> string(59) "data.shipments Object value found, but an array is required" } } } ["message"]=> string(73) "Failed validation agains JSON-SCHEMA shipment/post_shipments_request.json" }
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
$response['data'] = array(
"shipments" => array(
array(
"recipient" => array(
"cc" => "NL",
"city" => "Hoofddorp",
"street" => "Hoofdweg",
"number" => "679",
"postal_code" => "2131BC",
"person" => "Mr. Parcel",
"phone" => "01234565789",
"email" => "[email protected]"),
"options" => array(
"package_type" => 1,
"only_recipient" => 1,
"signature" => 1,
"return" => 1,
"insurance" => array(
"amount" => 5000,
"currency" => "EUR"),
"large_format" => 1),
"carrier" => 1
)
)
);
$data_string = json_encode($response);
?>
$response['data'] = array(
"shipments" => array(
array(
"recipient" => array(
"cc" => "NL",
"city" => "Hoofddorp",
"street" => "Hoofdweg",
"number" => "679",
"postal_code" => "2131BC",
"person" => "Mr. Parcel",
"phone" => "01234565789",
"email" => "[email protected]"),
"options" => array(
"package_type" => 1,
"only_recipient" => 1,
"signature" => 1,
"return" => 1,
"insurance" => array(
"amount" => 5000,
"currency" => "EUR"),
"large_format" => 1),
"carrier" => 1
)
)
);
$data_string = json_encode($response);
?>
Ik miste de [ en ] bij Shipments
Klopt, en dat is omdat je een array niveau te weinig had.