Jump to content

Curl: Download invoices automatically


Recommended Posts

I'm trying to setup a way to download invoices to a directory as soon as they're generated (and there's no functionality in the API for this), but I'm just stuck. I haven't figured out a way to log in as an admin yet.

I need to authenticate as an admin to have access to all invoices, and then call dl.php to get the specific invoice.

Has anyone had any success with this before?

Link to comment
Share on other sites

2 hours ago, DennisHermannsen said:

We managed to get it working using curl with COOKIEJAR option set, though.

I managed to get it too. It seems that I can curl() any WHMCS as admin but there's something strange that is going on with dl.php. At the moment I think I'm seeing the source code of the PDF on screen (see below - I removed streams). The download should start automatically 🤔 See you later.

%PDF-1.7
6 0 obj
<< /Type /Page /Parent 1 0 R /LastModified (D:20200416230906+02'00') /Resources 2 0 R /MediaBox [0.000000 0.000000 595.276000 841.890000] /CropBox [0.000000 0.000000 595.276000 841.890000] /BleedBox [0.000000 0.000000 595.276000 841.890000] /TrimBox [0.000000 0.000000 595.276000 841.890000] /ArtBox [0.000000 0.000000 595.276000 841.890000] /Contents 7 0 R /Rotate 0 /Group << /Type /Group /S /Transparency /CS /DeviceRGB >> /Annots [ 5 0 R ] /PZ 1 >>
endobj
7 0 obj
<> stream
endstream
endobj
1 0 obj
<< /Type /Pages /Kids [ 6 0 R ] /Count 1 >>
endobj
3 0 obj
<>
endobj
4 0 obj
<>
endobj
8 0 obj
<> /Length 2532 >> stream
endstream
endobj
2 0 obj
<< /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /Font << /F1 3 0 R /F2 4 0 R >> /XObject << /I0 8 0 R /I1 9 0 R >> >>
endobj
5 0 obj
<> /H /I>>
endobj
10 0 obj
<< /Author (��MyCompany Srl) /Creator (��WHMCS) /Producer (��TCPDF 6.2.17 \(http://www.tcpdf.org\)) /CreationDate (D:20200416230906+02'00') /ModDate (D:20200416230906+02'00') /Trapped /False >>
endobj
11 0 obj
<< /Type /Metadata /Subtype /XML /Length 4242 >> stream
endstream
endobj
12 0 obj
<< /Type /Catalog /Version /1.7 /Pages 1 0 R /Names << >> /ViewerPreferences << /Direction /L2R >> /PageLayout /SinglePage /PageMode /UseNone /OpenAction [6 0 R /FitH null] /Metadata 11 0 R /Lang (��en) >>
endobj
xref
0 13
0000000000 65535 f 
0000002648 00000 n 
0000013229 00000 n 
0000002707 00000 n 
0000002813 00000 n 
0000013363 00000 n 
0000000015 00000 n 
0000000483 00000 n 
0000002924 00000 n 
0000005701 00000 n 
0000013579 00000 n 
0000013842 00000 n 
0000018167 00000 n 
trailer
<< /Size 13 /Root 12 0 R /Info 10 0 R /ID [ <940028b904aa9eb0ed56ccf7e798b580> <940028b904aa9eb0ed56ccf7e798b580> ] >>
startxref
18391
%%EOF

 

Link to comment
Share on other sites

Nah my bad, I was printing it instead of echo(). Anyway this works for me:

<?php

$WHMCSURL = 'http://example.com'; // WHMCS URL (no trailing slash)
$adminPath = '/admin'; // Admin Path (no trailing slash)
$username = 'admin'; // WHMCS Username
$password = '******'; // WHMCS Password (it should work also with chars like `@/\ since I use decode)
$invoiceID = '1908'; // The ID of the invoice (tblinvoices.id) you want to download

// Login
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $WHMCSURL . $adminPath . '/dologin.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=' . $username . '&password=' . htmlspecialchars_decode($password));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/this/is/not/important/tmp');
$output = curl_exec($ch);

// Download PDF
curl_setopt($ch, CURLOPT_URL, $WHMCSURL . '/dl.php?' . http_build_query(array('type' => 'i', 'id' => $invoiceID)));
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
$output = curl_exec($ch);

header('Cache-Control: public');
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . $invoiceID . '.pdf"');
header('Content-Length: '.strlen($output));
echo $output;

Edit: I forgot to insert invoiceID variable. Anyway nice idea, I'm going to include it somewhere in my scripts 😋

Edited by Kian
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated