How to create an invoice (and client) via php api?

the title basically says what my question is. I checked your code for sdk but I still don’t get how to use it to create a client and a bill (the client does not exist yet). I did also check an example form to manage this with curl. However, it makes no sense to me how to make it work together.
What I got now is a HTML <form> and a backend php file which takes the input from the form and a function to process it. For the last step the code is required to add a new client to invoice ninja and add a new invoice to this client.

Are there any other working examples of how to integrate invoice ninja in custom html/php?

Thanks for your answer(s) in advance.

Have you seen the examples here:

Ya, but this won’t work with php. :confused:

If you’re using PHP why not use the SDK?

That’s what I tried to say in my op. I don’t get how to use your sdk. Could you please explain further?

Have you tried using this code to create an invoice/client?

Ya, but how do I import it to my php script?

We support composer, you’d run composer require invoiceninja/sdk-php

Is there a file/archive which contains all files already “composed”?

Not that I’m aware of

I got it with composer.
From command line I can connect to my invoice ninja installation but now with your php-sdk I get the following error:
[Sun Jun 18 10:12:07.549302 2017] [:error] [pid 11104] [client] PHP Fatal error: Uncaught Error: Call to undefined function InvoiceNinja\Models\curl_init() in /var/www/html/vendor/invoiceninja/sdk-php/src/InvoiceNinja/Models/AbstractModel.php:133\nStack trace:\n#0 /var/www/html/vendor/invoiceninja/sdk-php/src/InvoiceNinja/Models/AbstractModel.php(62): InvoiceNinja\Models\AbstractModel::sendRequest(‘https://crm…’, ‘{“contacts”:[{"…’, ‘POST’)\n#1 /var/www/html/index.php(12): InvoiceNinja\Models\AbstractModel->save()\n#2 {main}\n thrown in /var/www/html/vendor/invoiceninja/sdk-php/src/InvoiceNinja/Models/AbstractModel.php on line 133

Never mind about my last post. Had to fix permissions after running composer. Now it’s working. Thanks.

Ok, now I got it. Works perfect with this API. Thanks.
Just one thing, how does it work with downloading an invoice… right now I get some weired symbols in my browser. How to generate a download (invoice.pdf)?

It’s the raw PDF, you can enable the user to download it as a file using these headers:

Thanks, that gives me a download, but there seem to be errors with the encoding. I tried with base64_decode($pdf); but it no success.

I’m not sure, you try using the link in the app instead.


I think it has something to do with this line:

Are you saying the /download link isn’t working?

No, not very good. It works but I just found that if I put the content from the string to a file (with fopen (…) and name this file invoice.pdf, the pdf works. However, this gives me an empty pdf file:

$invoice = Invoice::find(1);
$pdf = $invoice->download();

header('Content-Type: application/pdf');
header('Content-Length: ' . strlen($pdf));
header('Content-Disposition: attachment; filename="test.pdf"');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');

return $pdf;

Are you sure it should be return $pdf; not echo $pdf;