I was recently tasked with writing a script to export the data from a woocommerce database, rearrange it, and email it to a warehousing partner automatically. The first part was pretty easy – writing the script, rearranging the sample data, and emailing it automatically when the script was run.
The second part was a bit more complex. The client had the GNU licensed CSV Customer/Order Export extension for Woocommerce. Unfortunately, it wasn’t working as expected. The client had already purchased a support license, but I hadn’t heard back from the support techs yet and was running far too close to deadline for my own comfort. The extension is set up to export to FTP or HTTP POST automatically and I decided to integrate that and accept the file it exports in the post instead of just using SQL to dig into the complex database structures and grab the data (more on that in another post). There was only one problem, it doesn’t actually submit a file with the HTTP POST option.
The Solution
Since I solved the problem, I figured I would share the solution for any other coder having the same issue. I’ve also asked support staff to update the documentation, as knowing one simple fact would have saved me HOURS of work.
The HTTP POST option from CSV Customer Order Export sends a stream of CSV formatted data, but not an actual CSV file. I had built a script that was capable of taking a submitted file and then running code on it to reorganize it, grab the correct data, upload into an SQL database and then resubmit in a format the warehouse was capable of using. It didn’t work and all CSV Export said was “Test Successful”.
Everything worked with other file uploads and remote connections from servers that I used to test my script, but it wasn’t working from the HTTP POST option in CSV Export. I couldn’t find any documentation that related to what kind of data HTTP POST exported, so I had to keep guessing at its nature and how to capture it. I finally, through trial and error and capturing raw data in the POST section, found that it sends a stream of data with commas and delimiters that has to be saved. Here is the simple code I used to capture that stream and save it as a CSV.
<?php /*Data Save and store*/ /* PUT data comes in on the stream */ $putdata = fopen("php://input", "r"); /* Open a file for writing */ $fp = fopen("tempfile.csv", "w"); /* Read the data 1 KB at a time and write to the file */ while ($data = fread($putdata, 1024)) fwrite($fp, $data); /* Close the streams */ fclose($fp); fclose($putdata); ?>
And that gives you a CSV file that you can manipulate and work with. You could use curl to upload it to another server from there, work code on the file afterwards, set up functions to send an email with the file attached, or load it into an array and rearrange it.
Since it is already hard enough being a coder and having to think in a half math, half logic, and half arcane spellcasting to code solutions, I thought this would help anyone trying to work with the HTTP POST Option out of CSV Customer Order Export.
– John