Get XML Data into Magento via the Magento API

Get XML Data into Magento via the Magento API

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 - 651 Downloads


Tags: , ,

I am a web entrepreneur, internet addict, e-commerce lover, and music fan. I currently work for a Internet Retailer 500 E-commerce site, managing email marketing, social media, and mobile marketing. In the past, I worked as an Internet Marketing Director, Design Director, Web Developer, and Product Designer.

33 Comments Leave yours

  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. 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. 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. Nathanial Meleo #

    yea nice Work

  8. 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!

  9. Thanks Joe, let me know how it works out for you.

  10. GregC #

    Does this require the use of Joomla before it will work?

  11. 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?

  12. No, this has nothing to do with Joomla Greg.

  13. 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.

  14. 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?

  15. 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

  16. 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?

  17. 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.

  18. 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?

  19. 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?

  20. Ron Seigel #

    Ahhhh….xml won’t display properly. trying again

    [code]

    2010-12-14T19:07:39.000

    69

    [/code]

  21. Ron Seigel #

    How can I post it here?

    The actual feed itself is at:

    http://www.trademarkcommerce.com/merchants/files/feeds/tminv_feed.xml

  22. Ron Seigel #

    Got it.

    Should have been:

    $res = $feed_xml->xpath(“//Product[ProductCode='$mysku']“);

  23. 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.

  24. 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.

  25. 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!

  26. 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

  27. 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?

  28. 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.

  29. 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.

  30. This is way better than a brick & mortar estaiblshnmet.

  31. doesn’t work for me… instead of adding lines to product_feed.xml …. it clears it out :(
    pls help

  32. 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… :-(

Leave a Reply