2011-01-05 13 views
1

我有一个URL,其中包含产品的XML。将URL从URL导入/解析到Magento中

我想要做的是,如果可能的话,解析内容,然后将细节作为产品导入到Magento中。

这是可能的吗?如果是这样,那么我需要执行什么步骤来执行此操作?

感谢

编辑:

import_products.php

require_once('app/Mage.php'); 

class import_products extends Mage_Core_Model_Abstract 
{ 

public static function url_contents($url) 
{ 
    $crl = curl_init(); 
    $timeout = 5; 
    curl_setopt ($crl, CURLOPT_URL,$url); 
    curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); 
    $ret = curl_exec($crl); 
    curl_close($crl); 
    return $ret; 
} 

}

import.php

require_once('import_products.php'); 

$app = Mage::app('default'); 
$product = Mage::getSingleton('catalog/product'); 

$objDOM = new DOMDocument(); 
$objDOM->load("http://www.example.com/products.xml"); 

$note = $objDOM->getElementsByTagName("car"); 
// for each note tag, parse the document and get values for 
// tasks and details tag. 

foreach($note as $value) 
{ 
    $car_ids = $value->getElementsByTagName("Car_ID"); 
    $car_id = $car_ids->item(0)->nodeValue; 

    $makes = $value->getElementsByTagName("Make"); 
    $make = $makes->item(0)->nodeValue; 

    $models = $value->getElementsByTagName("Model"); 
    $model = $models->item(0)->nodeValue; 

    $descriptions = $value->getElementsByTagName("Description"); 
    $description = $descriptions->item(0)->nodeValue; 

    $short_descriptions = $value->getElementsByTagName("Specification"); 
    $short_description = $short_descriptions->item(0)->nodeValue; 

    $prices = $value->getElementsByTagName("Price"); 
    $price = $prices->item(0)->nodeValue; 

    $simple = 'simple'; 
    $product->setAttributeSetId(4); 
    $product->setSku($task); 
    $product->setName($detail); 
    $product->setTypeId($simple); 
    $product->setPrice($price); 
    $product->setValue($price); 
    $product->setDescription($description); 
    $product->setShortDescription($short_description); 
    $product->setWeight(100); 
    $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE); 
    $product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED); 
    $product->setTaxClassId(2); 
    $product->setStockData(array(
     'is_in_stock' => 1, 
     'qty' => 99999 
)); 

    try { 
    $product->save(); 
echo "Saved"; 
    } 
    catch (Exception $ex) { 
     echo "<pre>".$ex."</pre>"; 
    } 

    //echo "$car_id :: $make :: $model :: $description :: $short_description :: $price <br /><br />"; 
} 
+0

好吧,我已经看过使用高级配置文件:http://www.magentocommerce.com/boards/viewthread/2448/,但它似乎没有通过curl连接并将文件保存在/ var/import - 任何想法? – terrid25 2011-01-05 13:10:34

回答

3

,你可以得到在法师的参数n要和使用简单的XML把字符串转换成XML

$xmlstr = self::XML_DECLARATION . urldecode($this->getRequest()->getParam('paramName'); 
    try { 
     $xml = simplexml_load_string($xmlstr); 
    } 
    catch(Exception $ex) { 
     echo "Unable to process xml file because of the following error<br /><br /> $ex"; 
     exit; 
    } 

如果你试图让你可以设置在它下面的代码的PHP文件,以获得该URL的内容的文件

function get_url_contents($url){ 
$crl = curl_init(); 
$timeout = 5; 
curl_setopt ($crl, CURLOPT_URL,$url); 
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); 
$ret = curl_exec($crl); 
curl_close($crl); 
return $ret; 

}

您可以使用该功能,然后在将上述我们有

$xmlstr = self::XML_DECLARATION . urldecode($this->getRequest()->getParam('paramName'); 

你应该能够做到这一点

$xmlstr = self::XML_DECLARATION . get_url_contents('http://urlhere.com/xml'); 

那么你可以使用简单的XML和分配节点的产品阵列,在那里你可以拥有的产品集合,然后遍历这些和编程创建在Magento产品像这样的东西,显然会改变可能被硬编码的值,无论你需要什么。

$product = Mage::getSingleton('catalog/product'); 

    // Build the product 
    $product->setSku($productData['sku']); 
    $product->setAttributeSetId(26); 
    $product->setTypeId('simple'); 
    $product->setName($productData['description']); 
    $product->setCategoryIds(array(162)); 
    $product->setWebsiteIDs(array(1)); 
    $product->setDescription($productData['description']); 
    $product->setShortDescription($productData['description']); 
    $product->setPrice($productData['price']); # Set some price 
    $product->setWeight(4.0000); 

    $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE); 
    $product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED); 
    $product->setData('is_salable', '1'); 
    $product->setTaxClassId(2); # My default tax class 
    $product->setStockData(array(
      'is_in_stock' => 1, 
      'qty' => 99999 
    )); 

    try { 
     $product->save(); 
    } 
    catch (Exception $ex) { 
     //Handle the error 
    } 
+0

这段代码在哪里? – terrid25 2011-01-05 13:39:46

+0

你的网址在哪里,它是你建立的一个自定义模块吗?如果是这样,你可以把它放在你的控制器中。 – 2011-01-05 13:50:53

+0

该url指向一个外部XML文件 - http://www.example.com/products.xml,如果此导入可用作URI:http://www.mymagentosite.com/import_products,那将会很不错。 php – terrid25 2011-01-05 14:00:01