使用 Magento API 获取产品 [英] Using Magento API to get Products

查看:45
本文介绍了使用 Magento API 获取产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Magento API 从另一个域中获取某个类别的产品的产品数据.我已经进行了 API 调用等...我目前用来获取产品数据的代码如下所示:

I'm using the Magento API to get product data for products from a certain category from another domain. I have made the API call etc... The code I'm currently using to get the product data looks like this:

$productList = $client->call($session, 'catalog_category.assignedProducts', 7);

foreach ($productList as $product){
    $theProduct = array();
    $theProduct['info'] = $client->call($session, 'catalog_product.info', $product['sku']);
    $allProducts[] = $theProduct;
}

代码运行良好,但运行速度极慢.当我将图像调用添加到循环中时,页面加载大约需要 50 秒,这适用于只有 5 个产品的站点.我想知道的是:

The code works fine, but it goes extremely slow. When I add the image call to the loop it takes about 50 seconds for the page to load, and that's for a site with only 5 products. What I want to know is the following:

  1. 上面的代码是否正确,只是 Magento 的 API 脚本很慢?
  2. 上面的代码不是我需要的最佳方式吗?
  3. 会不会有其他因素让这一切变得如此缓慢?

任何帮助将不胜感激.至少如果我知道我使用的是正确的代码,我可以查看其他途径.

Any help would be much appreciated. At least if I know I'm using the code right I can look at other avenues.

提前致谢!

================= 编辑 ==================

使用 Matthias Zeis 建议的多重调用,数据可以更快地到达.这是我使用的代码:

Using multicall suggested by Matthias Zeis, the data arrives much quicker. Here's the code I used:

$apicalls = array();
$i = 0;
$productList = $client->call($session, 'catalog_category.assignedProducts', 7);

foreach ($productList as $product){
$apicalls[$i] = array('catalog_product.info', $product['product_id']);
$i++;
}

$list = $client->multiCall($session, $apicalls);

这现在比以前更快了!我发现的下一个问题是 catalog_product_attribute_media.list 调用即使产品都设置了图像,但似乎并没有以同样的方式工作.

This now works much quicker than before! The next issue I've found is that the catalog_product_attribute_media.list call doesn't seem to work in the same way, even though the products all have images set.

我在 var_dump 中得到的错误是:

The error I'm getting in the var_dump is:

产品图片库中不存在请求的图片.

有人知道为什么现在会发生这种情况吗?再次提前致谢.

Anybody know why this may now be happening? Thanks again in advance.

推荐答案

1.上面的代码是否正确,只是Magento的API脚本很慢?

您的代码是正确的,但脚本很慢,因为 (a) SOAP API 不是非常快,并且 (b) 您正在为每个产品单独调用.

Your code is correct, but the script is slow because (a) the SOAP API is not blazingly fast and (b) you are doing seperate calls for every single product.

2.上面的代码不是我需要的最佳方式吗?

如果您使用 SOAP v1 API 或 XML-RPC,您可以测试 multiCall.首先,调用 catalog_category.assignedProducts 来获取产品 ID.收集产品 ID 并执行 multiCall 调用.这应该可以大大缩短等待时间.

If you use the SOAP v1 API or XML-RPC, you can test multiCall. At first, call catalog_category.assignedProducts to fetch the product ids. Collect the product ids and execute a multiCall call. That should cut the waiting time down quite a bit.

不幸的是,Magento 没有提供一个很好的开箱即用的解决方案来交付您需要的数据.我建议您实现自己的自定义 API 调用.

Unfortunately, Magento doesn't provide a nice solution out of the box to deliver the data like you need it. I recommend that you implement your own custom API call.

使用产品集合模型:

$collection = Mage::getModel('catalog/product')->getCollection();

这将为您提供一个 Mage_Catalog_Model_Resource_Product_Collection 对象,该对象可用于过滤、排序、分页……您的产品列表.迭代集合并构建一个包含您需要的数据的数组.您还可以在构建数据数组时直接为您的产品生成缩略图:

This will get you a Mage_Catalog_Model_Resource_Product_Collection object which can be used to filter, sort, paginate, ... your product list. Iterate over the collection and build an array containing the data you need. You also can generate thumbnails for your products directly while building the data array:

foreach ($products as $product) {
    $data[$product->getSku()] = array(
        /* the attributes no need ... */
        'small_image'   => Mage::helper('catalog/image')->init($product, 'image')
                                ->constrainOnly(true)
                                ->keepAspectRatio(true)
                                ->keepFrame(false)
                                ->resize(100,150)
                                ->__toString(),
        /* some more attributes ... */
    );
}

这应该会给您带来相当大的性能提升.

This should give you quite a performance improvement.

当然,这只是冰山一角.如果此解决方案对您来说不够快,请避免使用 SOAP 并通过构建您自己的 API 来绕过 Magento 堆栈的一部分.这不一定是一个复杂的解决方案:它可以是一个带有 HTTP 基本身份验证的简单 PHP 脚本,用于解析过滤条件等的 URL,包括 app/Mage.php 并调用 Mage::app() 来初始化 Magento框架.好处是您可以轻松使用 Magento 类,但您不必经历整个路由过程.

But of course this only is the tip of the iceberg. If this solution is not fast enough for you, avoid SOAP and bypass a part of the Magento stack by building your own API. This doesn't have to be a complex solution: it could be a simple PHP script with HTTP Basic Authentication which parses the URL for filter criteria etc., includes app/Mage.php and calls Mage::app() to initialise the Magento framework. The benefit is that you have the comfort of using Magento classes but you don't have to go through the whole routing process.

不要忘记,您可以缓存结果,因为我可以想象您将向其他域上的相当多的访问者展示相同的产品.即使缓存几分钟也可能对您的服务器有所帮助.

Not to forget, you may cache the results because I could imagine that you will show the same products to quite a few visitors on the other domain. Even caching for a few minutes may help your server.

3.会不会有其他因素导致它变得如此缓慢?

可能有一些原因导致您的服务器上的调用那么慢 - 但如果不知道您的数据量、您的服务器硬件和您所做的定制,即使是最好的猜测也不会'不会那么好.

There may be some reasons why the calls are that slow on your server - but without knowing the volume of your data, your server hardware and the customisations you have done, even a best guess won't be that good.

这篇关于使用 Magento API 获取产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆