Real-Time Exports API Integration Example
Using our real-time export feature, WP All Export can generate an export every time a new record of the chosen post type is created on your site. Each record is exported one by one, in real-time, as they are created. The record data can be used programmatically with our API and custom code.
Once you've set up a real-time export, you'll also need to set up an action that will be triggered for each exported record.
This guide explains how to use our API to set up that action. The code will use our API to send an HTTP POST request every time a WooCommerce order completes on your site. That request will include a JSON payload with the order data.
Please note that this is only example code that will most likely require modifications. For example, you may need to encode the order payload differently, or you may want to log/store errors from the cURL request in a different fashion. This specific example works strictly for CSV exports.
Step 1: Use the pmxe_after_export Hook
To get started, we are going to use the pmxe_after_export hook, which is going to fire after any export execution. We define for which specific export ID the code will run, so it's only executed for our real-time export.
function send_order_data_post_request( $export_id, $exportObj ) {
// Run for export 3 only
if ( $export_id == '3' ) {
// Integration code here
}
}
add_action( 'pmxe_after_export', 'send_order_data_post_request', 10, 2 );
Step 2: Define URL to Send the Post Request
Now, we define the URL that we are going to use for the POST request.
//The url you wish to send the POST request to
$url = "http://www.example.com/tester.phtml";
Step 3: JSON Encode the Order Data you Want to Send
We proceed to JSON encode the order data. We use a default CSV export for this example.
First, we obtain the current CSV export file from $exportObj->options["current_filepath"]
. Then, we use the array_map function with the str_getcsv function as the callback to build an array that contains the exported data. This array is later encoded as a JSON in the $payload variable.
//JSON encode the order data that was exported
$csv = file_get_contents($exportObj->options["current_filepath"]);
$array = array_map("str_getcsv", explode("\n", $csv));
array_pop($array); //Used to remove an empty element at the end of the array.
$payload = json_encode($array);
Step 4: Open the Connection
This initializes the cURL session.
//Open the connection
$ch = curl_init();
Step 5: Set the URL, Enable POST Request, Add POST Data
Using the curl_setopt function, we define the URL to be fetched with CURLOPT_URL, we set CURLOPT_POST to true to make a regular HTTP POST request, and then attach the $payload to the request with CURLOPT_POSTFIELDS.
//Set the url to be fetched
curl_setopt($ch,CURLOPT_URL, $url);
//Enable regular HTTP POST request
curl_setopt($ch,CURLOPT_POST, true);
//Pass the order data payload
curl_setopt($ch,CURLOPT_POSTFIELDS, $payload);
Step 6: Execute HTTP Post Request via cURL
We execute the HTTP POST request via cURL using curl_exec.
//Make sure curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//Execute HTTP post request via cURL
$result = curl_exec($ch);
Step 7: Return Error if cURL Fails and Close the Connection
In case there are any errors, we catch them using the curl_errno function and return them.
Finally, we proceed to close the cURL session using curl_close.
// Return error if cURL fails.
if ( curl_errno( $ch ) ) {
exit( 'Error:' . curl_error( $ch ) );
}
//Close the connection
curl_close( $ch );
Full code example
This is what the full code would look like:
function send_order_data_post_request( $export_id, $exportObj ) {
// Run for export ID 3 only
if ( $export_id == '3' ) {
//The url you wish to send the POST request to
$url = "http://www.example.com/tester.phtml";
//JSON encode the order data that was exported
$csv = file_get_contents($exportObj->options["current_filepath"]);
$array = array_map("str_getcsv", explode("\n", $csv));
array_pop($array); //Used to remove an empty element at the end of the array.
$payload = json_encode($array);
//Open the connection
$ch = curl_init();
//Set the url to be fetched
curl_setopt($ch,CURLOPT_URL, $url);
//Enable regular HTTP POST request
curl_setopt($ch,CURLOPT_POST, true);
//Pass the order data payload
curl_setopt($ch,CURLOPT_POSTFIELDS, $payload);
//Make sure curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//Execute HTTP post request via cURL
$result = curl_exec($ch);
// Return error if cURL fails.
if ( curl_errno( $ch ) ) {
exit( 'Error:' . curl_error( $ch ) );
}
//Close the connection
curl_close( $ch );
}
}
add_action( 'pmxe_after_export', 'send_order_data_post_request', 10, 2 );
If your order export columns look like this:
The JSON-encoded payload with the order data would look like this:
[["Order ID","Order Key","Title","Order Total","Product Name","Quantity","Item Cost","Shipping Taxes","Coupons Used"],["297","wc_order_Z5Y3gvVG79Tec","Order - October 20, 2022 @ 04:21 AM","18.51","Blue Circle Dog Tag from Aibox","1","17.31","1.2",""]]
Related Docs
Learn how to set up and run real-time exports using WP All Export.
Review our API to learn about the available hooks.
Use WP All Export to generate an export file manually, or on a schedule.