简体   繁体   English

如何发送包含文件的数据表,从 PHP ContactForm7 到 Curl

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

On my website, I use ContactForm7 to ask data and a file to users.在我的网站上,我使用 ContactForm7 向用户询问数据和文件。 Besides the automatic email sent by contactForm, I have a PHP function that retrieve the form data, and send them to my Node server to make some analysis.除了由 contactForm 发送的自动 email 之外,我还有一个 PHP function 检索表单数据,并将它们发送到我的节点服务器进行一些分析。 My problem is sending the file with a POST to my server.我的问题是将带有 POST 的文件发送到我的服务器。 I can retrieve the data of the file, but I don't know how to pass them to the curl POST send.我可以检索文件的数据,但我不知道如何将它们传递给 curl POST 发送。

This is my ContactForm module这是我的 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>

This is the PHP code in functions.php这是函数中的 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);

}

This is the content of the file vars.这是文件 vars 的内容。 How Should I use them?我应该如何使用它们? : :

$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 */

And this is my Node Express server这是我的 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!!!!!
  ...

How should I use $file_invoice, $file_name, $file_content with Curl?我应该如何将 $file_invoice、$file_name、$file_content 与 Curl 一起使用?

I ended up doing this in functions.php我最终在函数中做了这个。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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM