Trying to create new client with PHP Curl

When manually using curl from the cli, the new client is created with the submitted data:

curl -X POST "https://selfhosted.ninja/api/v1/clients" -H "Content-Type:application/json" -H "X-Ninja-Token: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" -d '{"name":"Client","contact":{"email":"test@example.com","first_name":"Claudio","last_name":"Kuenzler"}}'

But when I try to use the same using PHP curl, a new client is created but without the data:

$payload = "{'name' : 'Fixed Company'}";
$curl = curl_init();
$opts = [
        CURLOPT_URL => 'https://selfhosted.ninja/api/v1/clients',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POST => 1,  
        CURLOPT_HTTPHEADER  => [
                'Content-Type: application/json',
                'X-Ninja-Token: '. $token,
        ],
        CURLOPT_POSTFIELDS => [ $payload ],
];

curl_setopt_array($curl, $opts);
$response = curl_exec($curl);
$info = curl_getinfo($curl);
curl_error($curl);
curl_close($curl);

The request is successfully sent to Invoiceninja, a new client is created, but the name is empty instead of “Fixed Company”. I was also looking into possible problems with the data and used json_encode($payload) but this didn’t help either. Any idea?

Here’s an example using cURL and PHP from the SDK

I tried that, too. But it doesn’t work any I can’t figure out why. There’s no error coming from Invoiceninja.

PHP Code:

$data = array(
        "name" => "Infiniroot",
        "city" => "Bettwiesen",
);

$payload = json_encode($data);
echo "\n$payload\n";

$curl = curl_init();
$opts = [
        CURLOPT_URL => 'https://selfhosted.ninja/api/v1/clients',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POST => 1,
        CURLOPT_HTTPHEADER  => [
                'Content-Type: application/json',
                'X-Ninja-Token: '. $token,
        ],
        CURLOPT_POSTFIELDS => [ $payload ],
];

curl_setopt_array($curl, $opts);
$response = curl_exec($curl);
$info = curl_getinfo($curl);
curl_error($curl);
curl_close($curl);

echo $response;

When I launch this PHP code I get the following output. The first line is obviously the output from $payload.
The json output after this is the response from the Invoiceninja API.

{"name":"Infiniroot","city":"Bettwiesen"}
{
    "data": {
        "account_key": "mn59oqdszyfxhgjwwwzq5giaboj38ymt",
        "is_owner": true,
        "user_id": 1,
        "id": 23,
        "name": "",
        "display_name": "",
        "balance": 0,
        "paid_to_date": 0,
        "updated_at": 1603803736,
        "archived_at": null,
        "address1": "",
        "address2": "",
        "city": "",
        "state": "",
        "postal_code": "",
        "country_id": 0,
        "work_phone": "",
        "private_notes": "",
        "public_notes": "",
        "last_login": "",
        "website": "",
        "industry_id": 0,
        "size_id": 0,
        "is_deleted": false,
        "payment_terms": 0,
        "vat_number": "",
        "id_number": "",
        "language_id": 0,
        "currency_id": 0,
        "custom_value1": "",
        "custom_value2": "",
        "invoice_number_counter": 0,
        "quote_number_counter": 0,
        "task_rate": 0,
        "shipping_address1": "",
        "shipping_address2": "",
        "shipping_city": "",
        "shipping_state": "",
        "shipping_postal_code": "",
        "shipping_country_id": 0,
        "show_tasks_in_portal": false,
        "send_reminders": false,
        "credit_number_counter": 0,
        "custom_messages": "{}",
        "contacts": [
            {
                "account_key": "mn59oqdszyfxhgjwwwzq5giaboj38ymt",
                "is_owner": true,
                "id": 22,
                "first_name": "",
                "last_name": "",
                "email": "",
                "contact_key": "lmzzqninr0jhh3do6hu5geejb6hwu1mo",
                "updated_at": 1603803736,
                "archived_at": null,
                "is_primary": true,
                "phone": "",
                "last_login": "",
                "send_invoice": true,
                "custom_value1": "",
                "custom_value2": ""
            }
        ]
    }
}

I suggest comparing your code for differences, for example you’re wrapping the CURLOPT_POSTFIELDS in an array

1 Like

Yes, I’ve tried many variations.
I removed the array and it now seems to work, thanks! :grinning:

OK another question. How do I place the nested contact fields into the $data array?

I’m going to try a nested array in the data array, like this:

$data = array(
        "name" => "Infiniroot",
        "city" => "Bettwiesen",
        contact => array("first_name" => "Claudio"),
);

But maybe you can already tell me now that it won’t work :wink:

Edit: Wow, it actually works like this. :rofl: