cURL failed to send array

0

Well, I have a small problem sending cURL data, when I send the data to the API it returns me that there is one of the fields that is not an array: {"errors":["items deveria ser um Array"]}

Code:

$user = 'xxxxxxxxxxxxxxxxxxxxxxxx';
$pass = '';
$data = array(
        "email" => "[email protected]",
        "due_date" => "2018-10-08",
        "items" => array(
                "description"=>"carro",
                "quantity"=> 1,
                "price_cents"=>150000
                ),
        "payer"=>array(
                "cpf_cnpj"=>"644.620.920-74",
                "name"=>"Jeison",
                "phone_prefix"=>"31",
                "phone"=>"991872520",
                "address"=>array(
                        "zip_code"=>"30520240",
                        "number"=>"109",
                        "street"=>"blenda"
                        )
                 )        
        );
$data_string=json_encode($data);
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'https://api.iugu.com/v1/invoices/' );
curl_setopt( $ch, CURLOPT_HEADER, 1 );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . base64_encode( $user . ':' . $pass ) ) );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$return=curl_exec( $ch );
curl_close( $ch );
header("Content-Type: text/json; charset=utf8");'
    
asked by anonymous 02.10.2018 / 15:02

1 answer

1

According to the manual , the problem occurs because you are sending just one item API. The correct thing would be to send an array of items , as follows:

//demais itens omitidos
$data = array(
    "items" => array(
        array(
            "description"=>"carro",
            "quantity"=> 1,
            "price_cents"=>150000
        )
    )
);

Or simplified:

//demais itens omitidos
$data = array(
    "items" => [
        [
            "description"=>"carro",
            "quantity"=> 1,
            "price_cents"=>150000
        ]
    ]
);

What happens is this: When an associative array is converted to JSON , it is converted as an object and not as an array. Therefore, even the value of items being an array, its interpretation is like an object.

When adding the item object inside a new array, without informing index, by default, it will use the index zero (0), which will make it an indexed array. Indexed arrays, in turn, are maintained as arrays in JSON .

Note: The array has been simplified to demonstrate only the problem

Update

There is one more question in your code. You are converting array to JSON , but you are sending the array to API :

$data_string = json_encode($data);

In the code below, it should be $data_string :

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

Send the data type and size as well in the header:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Basic '. base64_encode($user.':'.$pass),                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: '.strlen($data_string)
));
    
02.10.2018 / 15:12