如何使用 Curl 从 PHP ContactForm7 发送包含文件的数据表单

how to send a Data Form including file, from PHP ContactForm7 with Curl

在我的网站上,我使用 ContactForm7 向用户询问数据和文件。除了由 contactForm 自动发送电子邮件外,我还有一个 PHP 函数可以检索表单数据,并将它们发送到我的节点服务器进行一些分析。 我的问题是将带有 POST 的文件发送到我的服务器。 我可以检索文件的数据,但我不知道如何将它们传递给 curl POST send.

这是我的 ContactForm 模块

    <!-- WP CONTACT FORM -->
    <div>
            <div>
                <label for="user_mail">Your email</label>
                [email* user_mail]
            </div>
    
    
            <div>
                <label for="file_invoice">Your file .xml</label>
                 [file file_invoice limit:1mb filetypes:xml|p7m]
            </div>
    
            
            [submit class:button class:default "SEND"]
    
    </div>

这是functions.php

中的PHP代码
/*==============================
  FUNCTIONS.PHP CONTACT-FORM HOOK 
================================== */

add_action( 'wpcf7_before_send_mail', 'action_wpcf7_add_text_to_mail_body' );

function action_wpcf7_add_text_to_mail_body($contact_form)
{
  $submission = WPCF7_Submission::get_instance();
  $data  = $submission->get_posted_data();
  $files = $submission->uploaded_files();

  /* get file data */
  $file_invoice = $files['file_invoice'][0];
  $file_name = basename($file_invoice);
  $file_content = file_get_contents($file_invoice);

  /* retrieve text fields */
  $user_mail     = $data['user_mail']; 

  /* put them in an array */
    $fields = array('user_mail' => $user_mail);  
   /* HOW I INSERT THE FILE TO BE SENT? */

  $curl = curl_init();
  $curlParams = array(
            CURLOPT_RETURNTRANSFER  => 1,
            CURLOPT_URL             => 'https://....', // my node server
            CURLOPT_POST            => 1,
            CURLOPT_POSTFIELDS      => $post_data               
           );

    curl_setopt_array($curl, $curlParams);
    $resp = curl_exec($curl);

}

这是文件 vars 的内容。 我应该如何使用它们?:

$files = ( [file_invoice] => Array
           ([0] => "/customers/0/c/3/cashinvoice.it/httpd.www/wp-content/uploads/wpcf7_uploads/1229655618/ffffff.xml")
         );

$file_name = "ffffff.xml";

$file_content = "<?xml version="1.0" encoding="utf-8"?> ......."; /* xml content */

这是我的 Node Express 服务器


var express         = require('express');
var routes          = express.Router(); 
var multiparty           = require('connect-multiparty');
var multipartyMiddleware = multiparty();

routes.post('/my_route', multipartyMiddleware, function(req, res)
{
  console.log(req.files); // should contains the file!!!!!
  ...

我应该如何将 $file_invoice、$file_name、$file_content 与 Curl 一起使用?

我最终在 functions.php

中完成了这项工作
$submission = WPCF7_Submission::get_instance();
$data = $submission->get_posted_data();
$files = $submission->uploaded_files();
$file_invoice = $files['file_invoice'][0];

$cFile = "";
if (function_exists('curl_file_create')) 
   { // php 5.5+
          $cFile = curl_file_create($file_invoice);
   } 
else { $cFile = '@' . realpath($file_invoice); }

    $fields = array(
                        'user_mail'         => $user_mail,
                        'user_phone'        => $user_phone,
                        'file'              => $cFile
                    ); 

 $curl = curl_init();
 $curlParams = array(
            CURLOPT_RETURNTRANSFER  => 1,
            CURLOPT_URL             => 'https://....',
            CURLOPT_POST            => 1,
            CURLOPT_POSTFIELDS      => $fields
                   );

curl_setopt_array($curl, $curlParams);
$resp = curl_exec($curl);

$responseStatusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);