Curl failed to send array

Asked

Viewed 599 times

1

Well I have a small problem sending the data via Curl, when sending the data to the API 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");`

1 answer

1


According to the manual, the problem occurs because you are sending only one item to the API. The correct thing would be to send a item array, 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 within a new array, without entering index, by default it will use the zero index (0), which will make it an indexed array. Indexed arrays, in turn, are kept as arrays in JSON.

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

Update

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

$data_string = json_encode($data);

In the code below, it should be $data_string:

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

Send in header, also, data type and size:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Basic '. base64_encode($user.':'.$pass),                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: '.strlen($data_string)
));
  • I did here but the error continues, I did so:"items" => array( array( "Description"=>"car", "Quantity"=> 1, "price_cents"=>150000 ), array( "Description"=>"moto", "Quantity"=> 3, "price_cents"=>150000 ) ) converted to json and tbm failed.

  • @user9806474 I updated the answer with other points you should change

  • It worked, very sheltered guy!! saved me in the internship!!

  • @user9806474 mark the answer as correct and, if possible, +1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.