Search
Close this search box.
Search
Close this search box.

How to import a JSON in WP all Import through an external API with authentication

In order to do so, first add in your functions.php the following code:

				
					function custom_file_download($url, $type = 'xml'){

    $ch = curl_init();
	$data = array('email' => 'demo', 'password' => 'd3m0Acc0unT');
	curl_setopt($ch, CURLOPT_URL, $url.'?'.http_build_query($data));
	$user = 'demo';
	$pass = 'd3m0Acc0unT';
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
            exit('Error:' . curl_error($ch));
    }
    curl_close ($ch);

	$response = json_decode($result, true);
	$result = json_encode( $response['Data']);

    $uploads = wp_upload_dir();
    $filename = $uploads['basedir'] . '/' . strtok(basename($url), "?") . '.' . $type;
//echo $filename;
    if (file_exists($filename)){
            @unlink($filename);
    }
    file_put_contents($filename, $result);
    return str_replace($uploads['basedir'], $uploads['baseurl'], $filename);
}
				
			

Now go to WP All Import > New import.

Click Downlad a file.

Click From URL.

In the field, write something like the following:

				
					[custom_file_download("https://www.website-with-api.com/api/apilink","json")]
				
			

Click New items and select WooCommerce Products.

Click Continue to Step 2.

That’s it! You will now get the products. The products are written in a file in your server in the following path:

				
					wp-content/uploads/myjson