创建新产品时 Prestashop Web 服务中没有图像条目

No image entry in Prestashop Web Service when creating new product

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

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

PS:我正在使用 PS v1.6

试试这个功能,使用 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');

我终于找到了一种向没有图像的产品添加图像的方法(在 /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;
}

我希望这能帮助那些和我遇到同样问题的人。