【问题标题】:How to add product image via the woocommerce REST API?如何通过 woocommerce REST API 添加产品图片?
【发布时间】:2015-06-21 05:13:18
【问题描述】:

我正在使用 Woocommerce REST API,需要将产品添加到商店。

它以前工作过。现在我有这个错误:

stdClass Object ( [errors] => Array ( 
  [0] => stdClass Object ( [code] => 
  woocommerce_api_invalid_remote_product_image 
  [message] => Error getting remote image 
  https://www.google.lt/images/srpr/logo11w.png ) ) )

这是通过 WooCommerce REST API 添加产品的文档 http://woothemes.github.io/woocommerce-rest-api-docs/#create-a-product

这是我的代码:

$dataArray = array(
            'title' => 'xxxxxxxxxx',
            'description' => 'description1',
            'price' => '69',
            'sku' => 'sku2',
            'tags' => 'tag1, tag2, tag3',
            'color' => array('red', 'blue'),
            'size' => array('S', 'M'),
            'image' => 'https://www.google.lt/images/srpr/logo11w.png'
            );

public function addProduct($data)
{
    $wc_api = $this->_getClient();


    $newProductData = array(
        'product' => array(
            'title' => $data['title'],
            'type' => 'variable',
            'regular_price' => $data['price'],
            'description' => $data['description'],
            'sku' => $data['sku'],
            'tags' => [ $data['tags'] ],
            'images' => [ array('src' => $data['image'], 'position' => '0') ],
            'virtual' => true
        )
    );

    return $wc_api->create_product($newProductData);
}

我正在使用此客户端调用 REST API

https://github.com/kloon/WooCommerce-REST-API-Client-Library

编辑: 如果我从托管 woocommerce 的 wordpress 获得图像,那么一切都很好。但是,如果我使用来自其他网站的链接,则会出现错误。

【问题讨论】:

  • 外部图像将使用wp_remote_get() 获取。你有安装 cURL 吗?还是有什么东西阻止了外部请求?
  • “卷曲已启用”。很酷,但配置正确吗?你测试过外部请求吗?
  • 如果您可以调试服务器端,请查看第 1700 行附近的 class-wc-api-products.php 内部发生了什么。这就是产生错误的原因。您可能遇到 SSL 错误。您是否尝试过使用http 链接而不是感谢https?看到这个stackoverflow.com/questions/26461966/…
  • 我很确定 image['position'] 也应该是一个整数,尽管这似乎不会引发错误。

标签: php api curl woocommerce


【解决方案1】:

我遇到了类似的问题,cURL 在 WordPress 内部生成的错误导致 woocommerce_api_invalid_remote_product_image{"errors":{"http_request_failed":["SSLRead() return error -9806"]},"error_data":[]},这意味着,根据 https://stackoverflow.com/a/26538127/266531 中的 Asaph,

php 是使用使用 Apple 的 Secure 的 cURL 版本编译的 Yosemite 下的传输和 URL 请求的目标没有 支持 SSLv3(可能由于 POODLE 而被禁用 漏洞)。

我的猜测是您在使用 SSL over cURL 时遇到了错误。

您是否尝试过使用http 链接而不是感谢https

如果您可以调试服务器端,请查看第 1700 行附近的 class-wc-api-products.php 内部发生的情况。这就是产生错误的原因。您可能遇到 SSL 错误。

如果是同一类型的 SSL 问题,那么您可能的解决方案是

  1. 使用不安全的图片链接(http 而不是https),或者
  2. 按照 Asaph 在https://stackoverflow.com/a/26538127/266531 的回答中使用 OpenSSL 而不是 SecureSSL 安装 PHP 的步骤

【讨论】:

    【解决方案2】:

    在 Woocommerce REST API 库中,您还可以将选项设置为不验证 SSL。

    $options = array(
        'debug'           => true,
        'return_as_array' => false,
        'validate_url'    => false,
        'timeout'         => 60,
        'ssl_verify'      => false,
    );
    

    【讨论】:

      猜你喜欢
      • 2022-12-15
      • 1970-01-01
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      相关资源
      最近更新 更多