繁体   English   中英

WooCommerce 3.5.4 和 WordPress 5.0.3 REST API:图片上传中断(woocommerce_product_invalid_image_id)

[英]WooCommerce 3.5.4 and WordPress 5.0.3 REST API: Image upload broken (woocommerce_product_invalid_image_id)

我正在使用 REST API 的 v2。此代码在 WordPress 和 WooCommerce 的旧版本上运行良好。我无法将图像上传到产品。

我升级后的第一个错误是:

array (
  'code' => 'woocommerce_product_image_upload_error',
  'message' => 'Invalid image: Sorry, this file type is not permitted for security reasons.',
  'data' => 
  array (
    'status' => 400,
  ),

通过在wp-config.php以下内容添加到文件底部来解决:

define('ALLOW_UNFILTERED_UPLOADS', true);

我无法弄清楚的第二个错误。 该图像不会上传并在上传的位置留下重影图像。

代码

<?php
require __DIR__ . '/vendor/autoload.php';

use Automattic\WooCommerce\Client;

$woocommerce = new Client(
    'http://localhost/wordpress', 
    'ck_44b92c00ea35e6cc59c89c29051bf67c22e0df3a', 
    'cs_dd833592a1ef7a00a82c1711fd455db2e4c5bd15',
    [
        'wp_api' => true,
        'version' => 'wc/v2',
    ]
);

$data['create'][] = array(
    'name' => 'TEST',
    'regular_price' => '4.50',
    'description' => 'TEST DESC',
    'type' => 'simple',
    'images' => array(
        array(
            'alt' => '',
            'name' => '',
            'src' => 'http://demo2.phppointofsale.com/PHP-Point-Of-Sale-Prev/index.php/app_files/view/1',
            'position' => 0,
        ),
    )
);


$response = $woocommerce->post('products/batch',$data);
$headers = $woocommerce->http->getResponse()->getHeaders();
var_dump($headers);
var_dump($response);

响应数据

array(13) {
  ["Date"]=>
  string(29) "Thu, 24 Jan 2019 18:22:16 GMT"
  ["Server"]=>
  string(6) "Apache"
  ["X-Powered-By"]=>
  string(9) "PHP/7.2.1"
  ["X-Robots-Tag"]=>
  string(7) "noindex"
  ["Link"]=>
  string(63) "<http://localhost/wordpress/wp-json/>; rel="https://api.w.org/""
  ["X-Content-Type-Options"]=>
  string(7) "nosniff"
  ["Access-Control-Expose-Headers"]=>
  string(27) "X-WP-Total, X-WP-TotalPages"
  ["Access-Control-Allow-Headers"]=>
  string(27) "Authorization, Content-Type"
  ["Expires"]=>
  string(29) "Wed, 11 Jan 1984 05:00:00 GMT"
  ["Cache-Control"]=>
  string(36) "no-cache, must-revalidate, max-age=0"
  ["Allow"]=>
  string(16) "POST, PUT, PATCH"
  ["Content-Length"]=>
  string(3) "139"
  ["Content-Type"]=>
  string(31) "application/json; charset=UTF-8"
}
array(1) {
  ["create"]=>
  array(1) {
    [0]=>
    array(2) {
      ["id"]=>
      int(0)
      ["error"]=>
      array(3) {
        ["code"]=>
        string(36) "woocommerce_product_invalid_image_id"
        ["message"]=>
        string(27) "#82 is an invalid image ID."
        ["data"]=>
        array(1) {
          ["status"]=>
          int(400)
        }
      }
    }
  }
}

证明https://via.placeholder.com/350x150是一张图片

cmuench@cmuench:~$ curl -I "https://via.placeholder.com/350x150";
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Mon, 28 Jan 2019 14:07:22 GMT
Content-Type: image/png
Content-Length: 1253
Last-Modified: Sun, 06 Jan 2019 22:00:10 GMT
Connection: keep-alive
ETag: "5c327a6a-4e5"
Expires: Mon, 04 Feb 2019 14:07:22 GMT
Cache-Control: max-age=604800
X-Cache: L1
Accept-Ranges: bytes

http://demo2.phppointofsale.com/PHP-Point-Of-Sale-Prev/index.php/app_files/view/1

来自实际文件的标头(不是演示示例)。 与演示示例相同的错误

    header("Cache-Control: max-age=2592000");
    header('Expires: '.gmdate('D, d M Y H:i:s', strtotime('+1 month')).' GMT');
    header('Pragma: cache');
    header('Content-Disposition: inline; filename="'.$file_name.'"');
    header("Content-type: ".get_mime_by_extension($file->file_name));

即使远程服务器返回“Content-Type: image/png”,Wordpress 的服务器端检索功能也没有获取文件名,因为您的休息请求中缺少名称,远程服务器响应中没有文件名,导致内部 wp_attachment_is_image( ) 测试失败。 尝试使用具有正确文件扩展名的文件名设置其余请求。

参考woocommerce源码: https ://github.com/woocommerce/woocommerce/blob/00a93ae8f0b200b4def4aea4462fec9d1d5ea96c/includes/api/v2/class-wc-rest-products-v2-controller.php

和 Wordpress 代码: https ://core.trac.wordpress.org/browser/tags/5.0.3/src/wp-includes/post.php

只需设置一个类似的环境,您的代码就可以工作,问题在于您的图像链接,curl 似乎将 url 响应作为 html 而不是图像( https://via.placeholder.com/350x150 )。 来自文档的“src”是一个包含图像 URL 的字符串,不幸的是,您提供的字符串不是图像 url。您只需直接指向图像文件,下面的代码对我来说很好。

<?php
require __DIR__ . '/vendor/autoload.php';

use Automattic\WooCommerce\Client;

$woocommerce = new Client(
    'http://localhost/wordpress', 
    'ck_a3ec02fcd547837c384e43ee6989200cca8f6178', 
    'cs_f60e9ad5c93c9e3bd4adaabd4bd323edddb58f7b',
    [
        'wp_api' => true,
        'version' => 'wc/v2',
    ]
);

$data['create'][] = array(
    'name' => 'TEST',
    'regular_price' => '4.50',
    'description' => 'TEST DESC',
    'type' => 'simple',
    'images' => array(
        array(
            'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg',

        ),
    )
);


$response = $woocommerce->post('products/batch',$data);
$headers = $woocommerce->http->getResponse()->getHeaders();
var_dump($headers);
var_dump($response);

因为 url 的名称中不包含文件扩展名,所以 Wordpress 将其视为没有扩展名的文件,因此无法通过wp_check_filetype_and_ext测试。

您可以添加过滤器以在 url 中不存在的地方添加正确的文件扩展名

add_filter('wp_handle_sideload_prefilter', 'add_extension_if_none_exists');

function add_extension_if_none_exists($file){
    if ( pathinfo( $file['name'], PATHINFO_EXTENSION ) ) {
        return $file;
    }
    $real_mime = wp_get_image_mime( $file['tmp_name'] );
    $mime_to_ext = apply_filters(
        'getimagesize_mimes_to_exts',
        array(
            'image/jpeg' => 'jpg',
            'image/png'  => 'png',
            'image/gif'  => 'gif',
            'image/bmp'  => 'bmp',
            'image/tiff' => 'tif',
            'image/webp' => 'webp',
        )
    );
    if ( ! empty( $mime_to_ext[ $real_mime ] ) ) {
        $file['name'] .= '.' . $mime_to_ext[ $real_mime ];
    }
    return $file;
}

也许这是您正在使用的版本上 WooCommerce 上的错误。 如果是这种情况:如果您使用外部 URL 上传新图片,则应包含 id 字段并将其设置为“0”

"images": [
        {
            "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg",
            "id": "0"
        }
    ]

如果您使用的是 WordPress 媒体中的图像,则应设置图像的 ID 以及 WP 上的链接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM