【问题标题】:No image entry in Prestashop Web Service when creating new product创建新产品时 Prestashop Web 服务中没有图像条目
【发布时间】:2016-02-09 21:23:27
【问题描述】:

因此,正如我在标题中指定的那样,我正在使用 Prestashop 网络服务将产品添加到我的商店。有了它,一切都变得简单而美丽。我设法通过组合、类别等轻松地将新产品添加到商店。 添加图片时出现问题。

PS 的文档展示了如何将图像发送到 API,以便它们与产品链接。例如,如果我有一个 ID 为 150 的产品,那么使用带有 POST 的 CURL 将图像发送到 /api/images/products/150 会将该图像添加到产品 (see here)。 我的问题如下:当我使用 PS Web 服务 API 创建新产品时,它会返回包含有关新产品信息的 XML。假设我的产品 ID 是 151。当转到 /api/images/products 时,最后一个条目是 150。所以基本上添加新产品不会在 API 的图像/产品部分添加新条目,所以我无处可通过 CURL 发送图像。 直到现在我都无法在互联网上找到解决方案。有谁知道如何强制网络服务使用新产品 ID 创建 /images/products 条目,或者如何手动创建条目?任何帮助表示赞赏。如果需要更多详细信息,请写在 cmets 中,我会添加它们。

PS:我使用的是 PS v1.6

【问题讨论】:

  • 同样的问题,我不知道为什么

标签: php image curl prestashop prestashop-1.6


【解决方案1】:

我终于找到了一种将图像添加到没有图像的产品的方法(在/api/images/products/{ID} 中找不到条目)。 关键是创建一个新的 CurlFile,而不仅仅是将图像的二进制文件发送回 PS WS。您可以使用以下功能:

/**
* Function that creates a new Product Feature Value and returns its newly created id
* @param product_id = the id of the product for which the image should be uploaded
* @param image_name = the String containing the name of the downloaded image (which will be found in /img)
* @return the ID of the newly inserted image
*/
function addNewImage( $product_id, $image_name  ) {
    $url = PS_SHOP_PATH . '/api/images/products/' . $product_id;
    $image_path = 'img/'. $image_name;
    $key = PS_WS_AUTH_KEY;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:multipart/form-data','Expect:'));
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_USERPWD, $key.':');
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => new CurlFile($image_path)));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $result = curl_exec($ch);
    curl_close($ch);

    return $result;
}

我希望这对遇到和我一样问题的人有所帮助。

【讨论】:

    【解决方案2】:

    试试这个函数,使用 cURL:

    function addProductImage($ProductId, $ImageUrl){
        $url = $this->url.'api/images/products/'.$ProductId;
        /**
         * Uncomment the following line in order to update an existing image
         */
        //$url = 'http://myprestashop.com/api/images/products/1/2?ps_method=PUT';
    
    
        if(class_exists('CURLFile')) {
            $cfile = new CURLFile(realpath($ImageUrl));
            //$cfile->setPostFilename("image.jpg");
        }
        else
            $cfile = '@'.realpath($ImageUrl);
    
    
        $postFields = array(
            'image' => $cfile,
    
        );
    
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:multipart/form-data','Expect:'));
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        //curl_setopt($ch, CURLOPT_PUT, true); //edit
        curl_setopt($ch, CURLOPT_USERPWD, $this->key.':');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        curl_close($ch);
    
    
        return $result;
    
    }
    
    
    $this->addProductImage(1, '/path/image/test.jpg');
    

    【讨论】:

    • icy2k 谢谢你的回答。问题:这段代码与我之前链接的 PS 文档中的代码不做同样的事情吗?请参阅此处,在页面底部:doc.prestashop.com/display/PS15/Chapter+9+-+Image+management
    • 是的,但是尝试像这个例子一样添加 HTTPHEADER。我现在已经尝试过了,它对我有用。你能告诉我你的错误吗?
    • 我只是尝试添加标题,但还是一样。如果我转储结果,它只会打印bool(false)。另外,如果我将 curl 的错误输出写入文件,它看起来像这样:* Hostname was found in DNS cache * Trying IP... * Connected to domain.com (IP) port 80 (#0) * Server auth using Basic with user 'KEY' * Connection #0 to host domain.com left intact 这对于调试没有用处。例如,如果我手动将图像添加到新创建的产品中,我可以毫无问题地使用上面的代码...
    猜你喜欢
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 2014-01-17
    相关资源
    最近更新 更多