Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uploading attachments #32

Closed
amochohan opened this issue Jun 17, 2015 · 10 comments
Closed

Uploading attachments #32

amochohan opened this issue Jun 17, 2015 · 10 comments

Comments

@amochohan
Copy link
Contributor

Given I have an existing invoice, how would I add an attachment to that invoice?

There is no attachment model, and looking at the Save() method on the Application class, there's no way for the file to be saved that I can determine.

@amochohan
Copy link
Contributor Author

My issue isn't relating to downloading PDFs, or other existing invoice attachments. I'm referring more to the ability to POST a brand new attachment to the Attachments endpoint as per http://developer.xero.com/documentation/api/attachments/

@calcinai
Copy link
Owner

Yep - I'm about to push a fix for both.

@calcinai
Copy link
Owner

You can use it like this:

$attachment = \XeroPHP\Models\Accounting\Attachment::createFromLocalFile('image.jpg');
$invoice = $xero->loadByGUID('Accounting\\Invoice', '[GUID]');
$invoice->addAttachment($attachment);

and fetch them like:

$invoice = $xero->loadByGUID('Accounting\\Invoice', '[GUID]');
$attachments = $invoice->getAttachments();
...
$attachment->getContent(); //Will contain the binary content of the file.

Let me know if you fun into any issues.

@amochohan
Copy link
Contributor Author

Just checked out the latest version, and having an issue with the file that gets created within Xero.

Using the exact same code that you've posted above, I can see the file gets created in Xero as can see it is listed along with the invoice that I've associated it to.

However, when I try to view the attachment (via the code above, or even directly within Xero) I get either a blank white document, or 'Failed to load PDF document'.

I've tried three different PDF documents, each from different sources.

Edit I've just tried with a jpg too, with no joy.

@calcinai
Copy link
Owner

Hey,

I've just been doing some testing too, and I'm getting the same thing. The ->getContent() method seems to contain the correct data, as if I set the content-type header in my test script and echo it, it shows. I'm just about to do a diff and see where the discrepancy is.

@amochohan
Copy link
Contributor Author

Whilst trying to debug what may be going on, I noticed the '?IncludeOnline=true' flag:

You can set an attachment to be included with the invoice when viewed online (through Xero). This functionality is available for accounts receivable invoices and accounts receivable credit notes. To enable an attachment to be viewed with the online invoice add the parameter IncludeOnline=true to the end of your request.

However, when appending that to the endpoint url the request throws an Exception Failed to validate signature.

The addAttachment() method sends the request twice, was this intentional?

@calcinai
Copy link
Owner

No, (neither is the print_r in there!). No idea how that slipped through, I think it's to do with the content-encoding - it's uploading it as UTF-8, when it's binary.

@calcinai
Copy link
Owner

However, when appending that to the endpoint url the request throws an Exception Failed to validate signature.

You need to use $request->addParameter() as the query string params are signed in the OAuth token.

@calcinai
Copy link
Owner

I've tried now using the file handle directly as CURLOPT_INFILE with no luck either. Will keep digging. Aside from the obvious (print_r & double send) the only problem seems to be the content encoding curl uses to send the file..

@amochohan
Copy link
Contributor Author

How are you setting the headers? Within Request, in the send() method I've tried adding:

$this->setHeader('Content-type', 'multipart/form-data');

before the $header_array variable is built, but this results in Undefined index: message in the response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants