将 XML 从 URL 导入/解析到 Magento 中

发布于 2024-10-10 14:29:21 字数 2590 浏览 4 评论 0原文

我有一个 URL,其中包含产品的 XML。

我想做的是,如果可能的话,解析内容,然后将详细信息作为产品导入到 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 />";
}

I have a URL, which contains XML of products.

What I'm looking to do, is, if possible, parse the contents and then import the details as products into Magento.

Is this possible and If so, what would be the steps I need to do to carry this out?

Thanks

EDIT:

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 />";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满地尘埃落定 2024-10-17 14:29:21

你可以在Magento中获取参数并使用简单的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
    }

you could get the parameter in Magento and use simple xml to turn the string into 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;
  }

if it is a file you are trying to get you can setup a php file with the following code in it to get the contents of the 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;

}

You can use that function and then where in the above we had

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

You should be able to do this

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

Then you could use simple xml and assign the nodes to a products array, where you could have a collection of products and then loop through those and programmatically create the products in Magento with something like this, obviously change the values that might be hard coded for whatever you might need.

    $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
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文