且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Magento API 获取产品

更新时间:2023-11-30 11:19:04

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.