在 wordpress 中使用 PHP 从动态下载 URL 获取文件
Getting a file from a dynamic download URL with PHP in wordpress
我正在尝试在 wordpress order completed 挂钩上自动下载文件(在本例中为 PDF 发票)。
我首先尝试使用 wp_remote_get 下载它,这看起来很简单,但没有成功(没有文件下载):
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 );
到目前为止,只要扩展名在 URL 中,我已经设法让它工作并使用 cURL 下载任何文件,但我无法让它与我的一起工作动态下载URL,就是这个test/demoURL:
https://www.moloni.com/downloads/index.php?action=getDownload&h=b75b2d99c08c56480da0c5dff4900b4a&d=189279574&e=teste@moloni.com&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&e=teste@moloni.com&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 );
但是,如果我用这个示例 PDF http://www.africau.edu/images/default/sample.pdf 替换 $fileUrl,那么它将起作用
我考虑过实施某种错误/日志,以便能够查看代码可能导致的错误,但是我还没有想出在将下载挂接到 woocommerce 订单的这些情况下如何做到这一点完成的动作
使用 file_get_contents
从 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)
);
我正在尝试在 wordpress order completed 挂钩上自动下载文件(在本例中为 PDF 发票)。
我首先尝试使用 wp_remote_get 下载它,这看起来很简单,但没有成功(没有文件下载):
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 );
到目前为止,只要扩展名在 URL 中,我已经设法让它工作并使用 cURL 下载任何文件,但我无法让它与我的一起工作动态下载URL,就是这个test/demoURL: https://www.moloni.com/downloads/index.php?action=getDownload&h=b75b2d99c08c56480da0c5dff4900b4a&d=189279574&e=teste@moloni.com&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&e=teste@moloni.com&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 );
但是,如果我用这个示例 PDF http://www.africau.edu/images/default/sample.pdf 替换 $fileUrl,那么它将起作用
我考虑过实施某种错误/日志,以便能够查看代码可能导致的错误,但是我还没有想出在将下载挂接到 woocommerce 订单的这些情况下如何做到这一点完成的动作
使用 file_get_contents
从 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)
);