Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

henryeti's avatar

Is there a way to attach file to Xero Invoice

I have this in my code which is meant to upload jpg image to my invoice.

$file_name = "set-chains-glyph_78370-1551.jpg";
$remoteFileUrl = 'https://example.com/path-to-remote-file.jpg'; 

// Download the remote file using file_get_contents
$remoteFileContents = file_get_contents($remoteFileUrl);


$base64FileContents = base64_encode($remoteFileContents); 

$attachmentData = [
    [
        'FileName' => $file_name,
        'MimeType' => 'image/jpeg', // Update MIME type if necessary
        'Content' => $base64FileContents,
    ]
];

// Construct the correct API endpoint for attaching the file to an invoice
$url = 'https://api.xero.com/api.xro/2.0/Invoices/' . $invoiceId . '/Attachments/' . $file_name;

$Fileheaders = [
    'Authorization' => 'Bearer ' . $accessToken,
    'Xero-Tenant-Id' => $tenantId,
    'Content-Type' => 'application/octet-stream', 
'application/octet-stream' for binary file uploads
];

$newClient = new OAuth2\Client(null, [
    'access_token' => $accessToken
]);

$attachmentResponse = $newClient->fetch($url, json_encode($attachmentData), 'POST', $Fileheaders);

The thing is that the image is uploaded but when i try viewing it the image looks broken and is not shown. I am not sure what i am doing wrong could someone kindly help with this.

0 likes
0 replies

Please or to participate in this conversation.