Get XML Data into Magento via the Magento API

Recently I needed to write some code to get a dropshipper’s xml product data into Magento to update my products price, quantity available, and stock status. This is how I went about it.

NOTE: Before you get started, you need to make sure you have created an API user in Magento. Here is a good step-by-step tutorial for doing that.

I decided that because of the potentially massive amount of data in my dropshipper’s XML feed file, that I should save the file to my server before attempting to process any data. I set up a cron job to use CURL to fetch the file. First I created a file called ‘importproducts.php’ and added the following code:

importproducts.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//my dropshipper's feed URL - replace with your dropshipper's feed
$myFeed = 'http://www.mydropshipperssite.com/feedurl.xml';
 
// specify the name of the file you want to save
$myFilename = 'product_feed.xml';
 
 
	if ($myFeed != '') {
	//  Initialize the cURL session
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $myFeed);
	//Create a new file
	$fp = fopen($myFilename, 'w');
	// Save to file
	curl_setopt($ch, CURLOPT_FILE, $fp);
	// Execute the cURL session
	curl_exec ($ch);
	//Close cURL session and file
	curl_close ($ch);
	fclose($fp);
 
	echo "Feed Imported Successfully!";
 
 
		//check the file size - if its 0 then my dropshipper didn't provide any updates, so don't let me know
		if (filesize($myFilename) > 0) {
 
			// send email to me when executed
			$to = "youremail@yourdomain.com";
			$subject = "Feed Imported";
			$body = "Hi,\n\nThe latest product data for YOURSITE has been downloaded on ";
			$body .= date('l jS \of F Y h:i:s A')."\n\n File Size: ".filesize($myFilename) . " bytes";
 
				if (mail($to, $subject, $body)) {
				  echo("<p>Message successfully sent!</p>");
					 } else {
				 echo("<p>Message delivery failed...</p>");
				}
 
		}
 
 
	}

Now that we have this script written, we need to process the data. Before that, let me show you an example of the xml data I am working with in the xml file we just saved with the import script. Here is sample xml:

product_feed.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" ?>
<products>
<product sku="15569" mfg_part_number="405970-1" upc="082324027055">
<inventory quantity="13"/>
<price your_price="143.18" list="159.99"/>
<description short="A product description"/>
</product>
 
<product sku="16128" mfg_part_number="406350-1" upc="082324029301">
<inventory quantity="0"/>
<price your_price="128.18" list="149.99"/>
<description short="Another product description"/>
</product>
</products>

Now we can talk about the next file, the one that takes the XML data and uses it to update products in Magento via the API. We’ll name this file ‘processupdates.php’.

processupdates.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//include the Magento API file - replace with your server path to the file
require_once('/some/path/to/public_html/app/Mage.php');
 
// ***** Configuration ******
$myDomain = 'http://www.yourdomain.com';
$myAPILogin = 'username';
$myAPIKey = 'password';
 
//calculate the price markup - useful if you have a standard markup you apply
$myMarkUp = .30;
$defaultMarkup = .30;
 
// get my saved file name
$myFilename = 'product_feed.xml';
 
//load up local xml file for processing
$feed_xml = simplexml_load_file($myFilename);
 
// Begin SOAP Requests
$client = new SoapClient($myDomain.'/api/?wsdl');
$session = $client->login($myAPILogin, $myAPIKey);
 
$updatedProducts = "";
 
//some counters - counting loops this way lets me see and set where the count increments
$x = 0;
 
//some filter date to pass to the API - add more to filter your results further - see Magento API docs
$filterData = array('type'=>'simple');
 
//get all my database products into an array
$products = $client->call($session, 'catalog_product.list', array($filterData));
 
 
//loop through my product array
foreach ($products as $product) {
 
echo "Starting product loop...<br/><br/>";
 
 
//get my database product sku - for cleaner reference in the code
$mysku = $product['sku']
 
//search directly in the product sku attribute in the xml for my sku
$res = $feed_xml->xpath("//product[@sku='$mysku']");
 
 
// if we find one, lets process it
if(!empty($res)) {
 
	//matched - make updates
	echo "Matched: ".$mysku;
 
	//price updates
	if ($res[0]->price['list'] != '') {
		$newprice = $res[0]->price['your_price'] + (($res[0]->price['list'] - $res[0]->price['your_price'])*$myMarkUp);
	} else {
		$newprice = $res[0]->price['your_price'] + ($res[0]->price['your_price']*$defaultMarkup);
	}
 
	//build my array to pass to the magento API
	$fieldPriceData = array('cost'=>$res[0]->price['your_price']*1, 'price'=>$newprice, 'upc'=>$res[0]->product['upc']);
	//update magento with price data
	$client->call($session, 'catalog_product.update', array($product['sku'], $fieldPriceData));
 
	//qty and stock updates
	if (($res[0]->inventory['quantity']*1) != 0) {
		$fieldQtyData = array('qty'=>$res[0]->inventory['quantity']*1, 'is_in_stock'=>1);
		echo "In Stock<br/>";
	} else {
		$fieldQtyData = array('qty'=>$res[0]->inventory['quantity']*1, 'is_in_stock'=>0);
		echo "Out of Stock<br/>";
	}
 
	//update magento with quantity and stock data
	$client->call($session, 'product_stock.update', array($product['sku'], $fieldQtyData));
 
	//record the updated product for emailing later
	$updatedProducts .= "SKU: ".$mysku." - ".$res[0]->description['short']."\nPrice: ".$newprice."\nCost: ".$res[0]->price['your_price']."\nQty: ".$res[0]->inventory['quantity']."\n\n";
 
	//increment my counter
	$x = $x + 1; 
 
} else {
 
echo "no match<br/><br/>";
 
}
 
 
 
// send mail to me
$to = "you@yourdomain.com";
$subject = "Feed Processed";
$body = "Hi,\n\nThe latest data for ".$myDomain." has been processed on ".date('l jS \of F Y h:i:s A')."\n\n";
$body .= "Products Updated: ".$x."\n\n".$updatedProducts;
if (mail($to, $subject, $body)) {
  echo("<p>Message successfully sent!</p>");
	 } else {
 echo("<p>Message delivery failed...</p>");
}

Now all you need to do is set up the cron jobs to call your import file at one interval, and your process file at another interval. Note that this works for me because I have a limited number of database products. If you have thousands of products you might want to have your site updating products by category, for example. If anyone has any better ideas on how to do any of this, please let me know. I always like to learn new stuff!

Download Files: Magento API XML Import - 15.82 KB - 1577 Downloads

45 thoughts on “Get XML Data into Magento via the Magento API

  1. Sprice

    Thanks this is just what I need. So far I’ve only done the import but it works perfectly.

    Keep up the good work!

  2. Daniel

    I got some problems getting this to work… I can easily fetch the xml file from my supplier, but I cant seem to process the data. I have added a api / rule and put in the info, i also updated the file with all info, but when i try to call the file in the browser, i just get a blank screen.

    Can you help?

    I my xml file, the SKU it is not called product sku, but instead Product_Code,
    So is it right that I need to use following code :
    $res = $feed_xml->xpath(“Product_Code=’$mysku'”);

  3. Daniel

    Keep getting an PHP Parse error: syntax error, unexpected T_VARIABLE in /var/www/domain.com/public_html/processupdates.php on line 62

    Also when i use the code
    $res = $feed_xml->xpath(“//product[@sku=’$mysku’]”);

    Any hints?

  4. Brian Wilhelm Post author

    Hi Daniel,

    W3C have a pretty good primer on Xpath here.

    I’d say its just a matter of using the right syntax to access the value you are looking for.

    In regards to the T_VARIABLE error, that usually happens when you have left out a closing curly bracket in your code.

  5. Paul

    Hi There,

    Thank you for sharing this information, much appreciated.

    My feed is like this please can you help?

    19/01/2010 23:27:54
    2.0

    N0507
    example 1
    6
    In Stock

    Please can you tell me where to replace these files and how to setup the cron job.

    Many Thanks

    Paul

  6. Brian Wilhelm Post author

    Hi Paul,

    Unfortunately I can’t make much out of feed example you posted, so if you can repost the XML that would be helpful.

    As far as where to put the files, I think you can put them anywhere so long at the path to the mage.php file is correct on line 2 of the processupdates.php, but to be safe I would put them in the root folder of your magento install.

    In regards to Cron Jobs, you can read more about them here.

  7. Joe

    Hey, thanks so much for posting this info! It’s taken me almost 24 hours of searching to figure out how to do this with Magento. So difficult that in fact I was starting to look at other e-commerce solutions (yes, the shame!). I’m going to try this solution out (and customize it a bit maybe) and will report back (eventually! :P).

    Thanks again!

  8. Bill

    Does this update product options “attributes or custom options such as file or text” and can it be modified to? and what kind of product amount or size or products would you limit this too?

  9. Brian Wilhelm Post author

    Hey Bill, I would think that you could add attributes to the update, so long as those methods are available through the Magento API. This is essentially looping through a data file and your database, so with a ton of products it could take a while. That’s why I would maybe suggest using it on a category by category basis if you have a lot of products. You could probably set up a variable as a counter that represents your number of categories, and then when the script finishes one category, it moves on to the next automatically until it has gone through all the categories you have.

  10. GregC

    Thanks Brian.
    I’ve been playing with this for hours and keep getting different errors with the files. If I change one thing I get a different error. Some examples so far’

    PHP Warning: simplexml_load_file(): I/O warning : failed to load external entity “product_feed.xml” in **/html/processupdates.php on line 34
    Starting product loop…PHP Fatal error: Call to a member function xpath() on a non-object in **/html/processupdates.php on line 62

    Another thing I’m confused about is in your .xml file the sku is named <product sku="15569"
    My .xml file it's named <product cwr_sku="15569"

    Which part do I need to rename in the processupdates.php file?

    I'm also running this without using CURL. Is that a problem?

  11. GregC

    Another error when leaving the original code in place. Error is around

    } else {
    echo “no match”;
    }

    PHP Parse error: syntax error, unexpected T_ELSE in ***/html/processupdates.php on line 101

  12. GregC

    Well I’m not having anything but issues with the code. I got it to run with an .xml that only had sku,qty in it. The code proceeded to change prices on every sku I had to $0? Not fun now I had to go change them back.
    It also does not process correctly, as it processed skus with qty of >1 as 0-out of stock?

  13. Matthew Galvin

    Nice work! I guess it could use a little more OOP styling, but nice solid code anyway. I did have to add a curly brace to the end to close your loop, but I assumed that was just a typo.
    Easy to get up and running and updating my stock – much easier than the previous CSV import.

  14. GregC

    Matthew,

    Could you post your modified code here (or another place). I’m curious as to how yours works fine and when I run it doesn’t work at all, except for first file to get the feed.
    Matter of fact the last time I tried it 2 days ago, it went haywire and sent me 10,638 emails?

  15. Ron Seigel

    Awesome work.

    I’m having an issues though.

    I can connect to soap but I’m having trouble with the XML file.

    Here’s a snippet of the XML:

    ————————————–

    2010-12-14T19:07:39.000

    69

    ……..
    ——————————-

    It seems line 46 is the problem:

    $res = $feed_xml->xpath(“//product[@sku=’$mysku’]”);

    I’ve tried:

    $res = $feed_xml->xpath(“//ProductCode[@sku=’$mysku’]”);

    but that doesn’t seem to get it.

    Any ideas?

  16. GregC

    I don’t think the code is going to work for meas written.
    My feed outputs like this >>

    See how the is not enclosed in separate brackets? The code seems to be written to take what is inside the name brackets, which mine is not.

    However, the example feed posted by Brian is like mine, and I’ve tried changing the
    $res = $feed_xml->xpath(“//product[@sku=’$mysku’]”); around, it still doesn’t seem to get the info.

  17. Chris M

    This looks like exactly what I need for my site but I have been struggling all day to get it to work the error that I am getting is:

    PHP Fatal error: Uncaught SoapFault exception: [Sender] Invalid XML in /home/onoineco/public_html/processupdates.php:50
    Stack trace:
    #0 /home/***/public_html/processupdates.php(50): SoapClient->__call(‘call’, Array)
    #1 /home/***/public_html/processupdates.php(50): SoapClient->call(‘4941238c963cfcf…’, ‘catalog_product…’, Array)
    #2 {main}
    thrown in /home/***/public_html/processupdates.php on line 50

    Any help that you can give would be greatly appreciated.

  18. Lucian

    @Chris! check for missing brackets and “;” on your code .

    Hello Brian, your code is just updating the existing products in magento by using the catalog_product.update methode.

    I’m looking for implementing the methode catalog_product.create to import/create the products from my .xml feed and after that doing updates using your code.

    Thanks!

  19. Thomas

    Hi Brian,

    Interesting reading – but I am a little far from succeeding in getting the feed to work. Could I perhaps hyre you for a job on the exact same matter as you describe above?

    All the best.

    Thomas

  20. pepperanne

    Thank you for sharing this script with us Brian,
    I tested your code and I would like to ask you if it’s tested for configurable products. Each configurable product consists of several simple products. Each simple product has it’s own stock. Updating the stock quantities of these simple products has no effect on live eshop Although stock quantities seem to change in backend, in frontend products are always available, even if the stock quantity is 0.I’m using magento 1.4.2. Could you please explain to me if/how this script handles configurable products?

  21. Brian

    Hey guys sorry for the long delay with no response. As you can see its been about a year and a half since I wrote this post, so I would imagine that some things might have changed on the magento side that possibly makes this code not work properly.

    This code is only set up to work with simple products in magento, although I’m sure it could be made to work with configurable products, maybe with an extra loop that considers the iterations of a product that may need updating. Of course your feed would have to have that data in it as well.

  22. A

    If you are available, I would like to hire you to setup the code for an automatically updating (and initially importing)) a product list from a drop shipper with an xml feed.

  23. Sanne

    I totally don’t get it… I’m trying to import all products from my dropshipper, he uses XML or CSV files, now I tried these files from you, but where do the products go which are in the XML-file?
    This is what I did:

    Changed and uploaded the files you provided, made an xml file of your example (to test it)
    and then went to mywebsite.com/importproducts.php it says it’s imported all fine, but I can’t seem to find the products in my catalogue? What am I missing? Please can you help me? I’m trying for weeks now to get the products from my supplier into my webshop… 🙁

  24. Dave

    @Sanne I think you confuse the purpose of the script, which is to update items you ALREADY have in your catalog with price and stock information from your drop shipper’s xml feed. This doesn’t do an import of new items into magento.

    People in general, if you don’t know basic php, or the difference between xml files and cvs, then this script is probably over your head and you can’t expect Brian to teach you. Hire a programmer. Brian has done a great job of posting this piece of code, even if you just use it as a framework for something else.

  25. Paul

    Hi Brian

    Really pleased I found you on google! I wondered if I could ask for some advice? I am developing a new website and am new to Magento. I am hoping to create a product feed from my supplier to update product stocks/ info etc on a daily basis. They have provided me with both XML and CSV files which they say is updated daily. My issue would be how I get this into some kind of feed format for Magento and also how (if possible) to ensure that all tabs/ columns match from the supplier files to Magento format, in order for the feed to work?

    Any help or advice very much appreciated.

    Kind Regards

    Paul

  26. Blair Geddes

    Any one any ideas how the below would be written as code?

    Using “simplexml_load_file” function to get the products data from XML feed and stored it in the array “$importedproductarray”

    Have tried the below for importing xml file in:

    <?php
    // The file finished_products.xml contains an XML document with a root element
    // and at least an element /[root]/title.

    if (file_exists('finished_products.xml')) {
    $xml = simplexml_load_file('finished_products.xml');

    print_r($xml);
    } else {
    exit('Failed to open finished_products.xml.');
    }

    $importedproductarray = 'finished_products.xml';

    foreach ($importedproductarray as $product)

    But not quite working. Loads the xml in browser part fine, but not passing array 🙁

  27. amLIgo

    I was starting with your solution to import products to Magento, and then I had stumbled upon file2cart service. it imports data to the online store automatically, thus I had just uploaded XML file and the data was imported without my participation. I was surprised how easy it was

  28. Kurtis

    Hi all,
    If you have some technical skills you can use such helpful scripts as default Magento does not support this functionality. However, if you need to adjust code according to your needs, you have to be very careful.
    If you’re not experienced with coding I’d like to recommend to use extensions. Actually, my advice is to do updates and synchronizing the information with your supplier via Automated Product Import and Store Manager. The latest version of Store Manager handles import of the file in .XML format, as well as file of csv format and it does not require special technical skills. It can upload the information from external file and for that you won’t need any coding skills. In case you get the file from your supplier, the procedure can be automated done on scheduled basis.

  29. Shayla

    You share interesting things here. I think that your page can go viral easily, but you must give it initial boost
    and i know how to do it, just type in google for – wcnu traffic increase

  30. Johan

    What a fantastic guide. Managed to pull the data feed from my supplier after a few hiccups. Eureka!!! BUT Unfortunately I’m stuck with the processupdate.php cron job failing:

    Status: 500 Internal Server Error
    X-Powered-By: PHP/5.3.28
    Content-type: text/html

    Any ideas?

    Any feedback would be much appreciated…

    email me: djguessrsa[at]gmail[dot][com]

    Thank you kindly.

    Johan

  31. Tech

    Hi,
    This is my xml file http://affiliate.strawberrynet.com/affiliate/cgi/directListXML.aspx?siteID=neoretail&langID=1&currency=INR

    But I cannot understand several things-
    1) what will be this?
    // ***** Configuration ******
    $myDomain = ‘http://www.yourdomain.com’;
    $myAPILogin = ‘username’;
    $myAPIKey = ‘password’;

    Also I want to update stock and price only.What will be the $res = $feed_xml->xpath(“//product[@sku=’$mysku’]”);

    How to do this through cron job?
    Thanks

  32. BrowSEO Full version Cracked

    Thɑnks a lot for sharing tһіs with alⅼ people yoou гeally recognise what you are talking aрproximately!
    Bookmarked. Kindly ɑlso seek advice from my web site =).
    Ꮃe wiⅼl have а hyperlink alternate arrangement ɑmong սs

Leave a Reply

Your email address will not be published. Required fields are marked *