On my website, I use ContactForm7 to ask data and a file to users. 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. My problem is sending the file with a POST to my server. I can retrieve the data of the file, but I don't know how to pass them to the curl POST send.
This is my ContactForm module
<!-- 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
/*==============================
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. 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
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?
I ended up doing this in 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);