php WooCommerce 3.5.4和WordPress 5.0.3的REST API:图像上传中断(woocommerce_产品_无效_图像_ID)

nfs0ujit  于 2023-01-12  发布在  PHP
关注(0)|答案(4)|浏览(129)

我使用的是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));
zlwx9yxi

zlwx9yxi1#

即使远程服务器返回“内容类型:image/png”,WordPress的服务器端检索功能没有获得文件名,因为在您的rest请求中缺少名称,并且在远程服务器响应中没有文件名,这导致内部wp_attachment_is_image()测试失败。请尝试使用具有正确文件扩展名的文件名设置rest请求。
参考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

ca1c2owp

ca1c2owp2#

只要设置一个类似的环境,你的代码工作,问题在于你的图像链接,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);
nbysray5

nbysray53#

由于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;
}
v8wbuo2f

v8wbuo2f4#

可能是WooCommerce上您正在使用的版本有一个bug。如果是这样:如果您使用外部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上的链接。

相关问题