0

I'm trying to automate downloading a file (in this case it's a PDF invoice) on wordpress order completed hook.

I have first tried to download it using wp_remote_get which seemed simple, but without success (no file downloads):

function download_pdf_invoice__on_order_completed( $order_id, $order ) { wp_remote_get( "http://www.africau.edu/images/default/sample.pdf" ); } add_action( 'woocommerce_order_status_completed', 'download_pdf_invoice__on_order_completed', 20, 2 ); 

So far I have managed to make it work and download any file with cURL as long as the extension is in the URL, but I can't get it to work with my dynamic download URL, which is this test/demo URL: https://www.moloni.com/downloads/index.php?action=getDownload&h=b75b2d99c08c56480da0c5dff4900b4a&d=189279574&[email protected]&i=1&t=n

function action_woocommerce_admin_order_get_invoice_pdf($url){ //The resource that we want to download. $fileUrl = 'https://www.moloni.com/downloads/index.php?action=getDownload&h=b75b2d99c08c56480da0c5dff4900b4a&d=189279574&[email protected]&i=1&t=n'; //The path & filename to save to. $saveTo = '/myserver/public_html/wp-content/plugins/my-custom-functionality-master/logo.jpg'; //Open file handler. $fp = fopen($saveTo, 'w+'); //If $fp is FALSE, something went wrong. if($fp === false){ throw new Exception('Could not open: ' . $saveTo); } //Create a cURL handle. $ch = curl_init($fileUrl); //Pass our file handle to cURL. curl_setopt($ch, CURLOPT_FILE, $fp); //Timeout if the file doesn't download after 20 seconds. curl_setopt($ch, CURLOPT_TIMEOUT, 20); //Execute the request. curl_exec($ch); //Get the HTTP status code. $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); //Close the cURL handler. curl_close($ch); } add_action( 'woocommerce_order_status_completed', 'action_woocommerce_admin_order_get_invoice_pdf', 20, 2 ); 

However if I replace the $fileUrl with this sample PDF http://www.africau.edu/images/default/sample.pdf then it will work

I have considered implementing some sort of error / log to be able to see what errors could be caused by the code however I have not figured it out how to do this under these circumstances of hooking the download to the woocommerce order completed action

1
  • 1
    Why not simply use file_get_contents($fileUrl) instead of cUrl? Just don't forget to place somewhere that file content
    – Justinas
    CommentedFeb 25, 2021 at 19:03

1 Answer 1

1

Use file_get_contents to download file from URL.

$fileUrl = 'https://www.moloni.com'; $saveTo = ABSPATH . '/wp-content/plugins/my-custom-functionality-master/logo.jpg' file_put_contents( $saveTo, file_get_contents($fileUrl) ); 

1
  • I had tried file_get_contents before but it did not work, however your code is working great with any link even my dynamic download one is downloading successfully! Thanks JustinasCommentedFeb 25, 2021 at 20:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.