Wait!

Lifetime licenses are only available for a short time, don't miss out!

Any File Type

No need to reorganize your data, just upload your file to WP All Import and it'll sort it out.

Complete Customization

Granular control over every single piece of data. Think your import is too complicated? Think again.

Import Huge Datasets

It doesn't matter how big your site is or how much data you need to import, WP All Import can handle it. 

Multilingual Imports

Importing data to multilingual sites has never been easier, with support from the WPML team.

Scheduled Imports

Run imports automatically on a schedule. Subscribe to our point and click scheduling service, or set up cron jobs.

Developer Friendly

Write and test PHP right inside WP All Import when setting up your import, with tons of snippets to get you started.
@glitzformals
"This plugin is a game changer for my situation. I’m stunned at how fast and effortless the import process is."
@adatfalo
"I have never experienced such a quick and helpful support experience with any plugin, and by the way, the knowledge of the support colleagues is professional!"
@jessedw44
"Even if you just use this plugin once, it will save you time. If you work on several sites its invaluable. Constantly developed, and continuously supported. Great job."
90-Day Guarantee
Not 100% happy? Let us know and we’ll promptly send you a refund.
Free Updates For Life
Pay once for unlimited installs, even client sites. No renewal fees.
Fast, World Class Support
Over 9 years experience with WordPress imports and exports.
Now is your last chance to buy a lifetime license before we switch to annual pricing. Existing licenses will be unaffected.
Read More

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:

Order Data API Export Example

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.

Lifetime support. Lifetime updates. Pay once.

Packages
Standalone
Import
Pro Package
$249
one-time
  • Import Pro
Import Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
  • Types
  • Export Pro
Export Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
Add to Cart
90 Day Money Back Guarantee
Import + Export Pro Package
$299
one-time
  • Import Pro
Import Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
  • Types
  • Export Pro
Export Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
Add to Cart
90 Day Money Back Guarantee
WooCommerce Import Package
$149
one-time
  • Import Pro
Import Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
  • Types
  • Export Pro
Export Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
Add to Cart
90 Day Money Back Guarantee
Import Standalone
$99
one-time
  • Import Pro
Import Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
  • Types
  • Export Pro
Export Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
Add to Cart
90 Day Money Back Guarantee
Import + Export Standalone
$169
one-time
  • Import Pro
Import Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
  • Types
  • Export Pro
Export Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
Add to Cart
90 Day Money Back Guarantee
Export Standalone
$99
one-time
  • Import Pro
Import Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
  • Types
  • Export Pro
Export Add-Ons
  • WooCo
  • ACF
  • Gravity Forms
  • Users
Add to Cart
90 Day Money Back Guarantee
cross