Unable to get the API to work via PHP cURL in V5

Hi , I am trying to bill a client with the following curl command from terminal(linux) and it works like a charm ;

curl -X POST https://crm.xyz/api/v1/invoices -H “Content-Type:application/json” -d ‘{“client_id”:“the client id that i copied from list client curl”, “send_email”: false, “due_date”: “2022-05-06”, “date”: “2022-05-06”, “line_items”:[{“product_key”: “CHIS-V2 On-Cloud Users”, “notes”:“Test”, “cost”:3 “quantity”:3]}’ -H “X-API-Token: Svh…”

BUT when I try to do that via my PHP script it does not work, the only msg I see of var_dump is: false
The PHP code that I am using :

$data = array(
“client_id” => “rlNbW6Jayg”,
“send_email” => “true”,
“date” => “$datetoday”,
“due_date” => “$datetoday”,
“due_date” => “$datetoday”,
line_items => array(
“product_key” => “CHIS-V2 On-Cloud Users”,
“cost” => “$rate”,
“quantity” => “$qtty”
),
);

$payload = json_encode($data);
echo “\n$payload\n”;
$curl = curl_init();
$opts = [
CURLOPT_URL => ‘https://crm.xyz/api/v1/invoices’,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => “POST”,
CURLOPT_FOLLOWLOCATION, true,
CURLOPT_SSL_VERIFYPEER, false,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => [
‘Content-Type: application/json’,
‘Content-Type: multipart/form-data’,
‘X-Api-Token: Svh3…’,
‘X-Requested-With: XMLHttpRequest’,
],
CURLOPT_POSTFIELDS => $payload ,
];

curl_setopt_array($curl, $opts);
$response = curl_exec($curl);
$info = curl_getinfo($curl);
curl_error($curl);
curl_close($curl);
echo "
Info: " . curl_getinfo($curl);
echo $response;
echo “
”;
var_dump($response);

The respone i get of var_dump :

/var/www/test.php:119:boolean false

Please help me solve this, new at PHP and APIs,
Regards,
Ahsan Muhammad.

Just in case you wonder , all the variables are working fine as i can see them resolve on the echo.

@AhsanMuhammad

Have you tried to use our php SDK? this will do all the heavy lifting for you so you don’t have to worry about curl commands from within your script:

For instance, this is all you need to do to create an invoice, you can also attach actions when creating the invoice, so if you wish to create the invoice and mark it as paid…

use InvoiceNinja\Sdk\InvoiceNinja;
   
$ninja = new InvoiceNinja("your_token");

    $invoice = $ninja->invoices->create(['client_id'=> 'CLIENT_HASHED_ID'], ['mark_paid' => true]);

No I did not, but I would really like to know whats wrong with the code I am trying with.

If you run the command from your command line you should at the very least get a return error message.

You may want to start by using either curl from the command line or a tool like postman, so that you can easiily inspect the payloads…

Again, it may be worth the tiny bit of extra pain to use the SDK so you don’t have to worry about anything except the data you wish to send up.

curl -X POST 'http://ninja.test:8000/api/v1/invoices' \
-H "X-API-TOKEN:company-token-test" \
-H "Content-Type:application/json" \
-d '{"client_id":"olejZqWejN","line_items":[{"product_key":"ITEM","notes":null,"cost":10.0,"quantity":1.0,"tax_name1":"","tax_rate1":0.0,"tax_name2":"","tax_rate2":0.0,"type_id":"","custom_value1":"","custom_value2":"","discount":0.0,"isChanged":false,"updated_at":0,"archived_at":0,"is_deleted":false,"id":"-600"}]}' \
-H "X-Requested-With: XMLHttpRequest";

from my terminal, everything works fine,
cURL via PHP is the issue.
and now your SDK is also not working, why is there limited or no documentation available?
also, a step-by-step guide on API, PHP-SDK, etc is sincerely missing.

trying with the SDK i am getting :

PHP Fatal error: Uncaught Error: Class ‘InvoiceNinja\Sdk\InvoiceNinja’ not found

my code :

<?php

require DIR.’/vendor/autoload.php’;
use InvoiceNinja\Sdk\InvoiceNinja;

$ninja = new InvoiceNinja(“Svh…”);
$ninja->setUrl(‘https://crm.xyz’);
$invoices = $ninja->invoices->all();

ini_set(‘xdebug.var_display_max_depth’, 20000);
ini_set(‘xdebug.var_display_max_children’, 25006);
ini_set(‘xdebug.var_display_max_data’, 100024);
$rate = “03”;
$qtty = “03”;
$datetoday = date(“Y-m-d”);

$client = $ninja->invoices->create([
‘client_id’ => ‘rlNbW6Jayg’,
‘send_email’ => ‘true’,
‘date’ => ‘$datetoday’,
‘due_date’ => ‘$datetoday’,
‘due_date’ => ‘$datetoday’,
line_items => [
‘product_key’ => ‘CHIS-V2 On-Cloud Users’,
‘notes’ => ‘Test’,
‘cost’ => ‘$rate’,
‘quantity’ => ‘$qtty’]]
);

The problem here isn’t the SDK, but the way you are loading, the error message is clear.

Depending on your project structure, you may need to use from the root with

use \InvoiceNinja\Sdk\InvoiceNinja;

If you inspect the tests directory, there will be lots of examples of the SDK in use.

due to the lack of time I had and the fact that there is not much documentation available for beginners like me (sorry if am wrong), I accomplished the task by using bash as curl executer and PHP script to pass variables to the bash script for curl execution as a terminal command.

In the mean while i will keep experimenting the SDK also and will update you guys!

Thank you for your support,

Regards,
Ahsan Muhammad.

@AhsanMuhammad For a customer of ours we once wrote an API wrapper for Invoice Ninja. We wrote a blog article about handling an API (such as Invoice Ninja’s API) using PHP and libcurl:

Maybe this helps you for your implementation.

2 Likes