程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了通过 Shopware 6 API / VBA 通过 URL 添加图像大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决通过 Shopware 6 API / VBA 通过 URL 添加图像?

开发过程中遇到通过 Shopware 6 API / VBA 通过 URL 添加图像的问题如何解决?下面主要结合日常开发的经验,给出你关于通过 Shopware 6 API / VBA 通过 URL 添加图像的解决方法建议,希望对你解决通过 Shopware 6 API / VBA 通过 URL 添加图像有所启发或帮助;

我尝试使用 Shopware 6 API 使用带有 Winhttp.Winhttprequest.5.1 的 VBA 从现有 Access 数据库迁移东西到新商店。除媒体传输外,一切正常。

我的方法:

  1. 在 shopware 中的正确媒体文件夹中创建媒体对象
   POST /API/v3/media 

   payload: "{""mediaFolderID"":""" & folderID & """,""ID"":""ec90cae2cc84c37b6b7a0cb9fe5c4548""}"

工作正常!

  1. 通过 url 上传新图片 (testfile.png)
   POST /API/v3/_action/media/ec90cae2cc84c37b6b7a0cb9fe5c4548/upload?extension=png&filename=testfile

   payload: "{""url"":"" & MediaUrl & "." & MediaUrlExt & ""}"

   response 204 (no error),

创建了一个媒体文件,但文件的内容不是 PNG 图像而是负载字符串

VBA 代码:

Set objhttp = CreateObject("Winhttp.Winhttprequest.5.1")
        AuthURL = url & "/API/v2/_action/media/" & newmediaId & "/upload?extension=" & fileExtFromUrl(MediaUrl) & "&filename=" & filenameFromUrl(MediaUrl) & "&_response=true"
        objhttp.open "POST",AuthURL,false
        objhttp.setrequestheader "User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; windows NT 5.0)"
        objhttp.setrequestheader "Content-type","application/Json"
        objhttp.setrequestheader "Accept","application/Json"
        objhttp.setrequestheader "Authorization","Bearer " & Token
        DeBUG.Print authURL
        payload = "{""url"":""https://" & FTPUrl & ftpKategorIEOrdner & "/" & MediaUrl & """}"
        DeBUG.Print payload
        objhttp.send (payload)
        strResult = objhttp.responseText
        DeBUG.Print strResult
        DeBUG.Print "uploadMedia " & objhttp.Status; " - " + objhttp.StatusText

也许是创建缩略图有问题?开发日志:

[2021-05-18 15:13:22] messenger.INFO: Sending message Shopware\Core\Content\Media\message\Generatethumbnailsmessage with Enqueue\MessengerAdapter\QueueInteropTransport {"message":"[object] (Shopware\\Core\\Content\\Media\\message\\Generatethumbnailsmessage: {})","class":"Shopware\\Core\\Content\\Media\\message\\Generatethumbnailsmessage","sender":"Enqueue\\MessengerAdapter\\QueueInteropTransport"} []
[2021-05-18 15:13:23] php.INFO: User Deprecated: The "Symfony\Component\DeBUG\DeBUGClassLoader" class is deprecated since Symfony 4.4,use "Symfony\Component\ErrorHandler\DeBUGClassLoader" instead. {"exception":"[object] (ErrorException(code: 0): User Deprecated: The \"Symfony\\Component\\DeBUG\\DeBUGClassLoader\" class is deprecated since Symfony 4.4,use \"Symfony\\Component\\ErrorHandler\\DeBUGClassLoader\" instead. at ./vendor/symfony/deBUG/DeBUGClassLoader.php:16)"} []

怎么了?

解决方法

这应该有效 - 管理面板使用相同的端点通过 URL 上传图像和上传二进制数据。

我能发现的唯一区别是请求的内容类型。

很遗憾,您没有提到您要设置的内容类型,我认为这就是问题所在。

Only if the content type is application/json,从 URL 获取文件。

确保在第二个请求中设置标题

Content-Type: application/json

EDIT 根据 https://forum.shopware.com/t/bilder-upload-ueber-api-url/87850/4 中的 x-post 发现 VBA 发送

content-type: "application/json; Charset=UTF-8"

Shopware 核心的一个可能补丁是

124: if (strpos($contentType,'application/json') !== falsE))

大佬总结

以上是大佬教程为你收集整理的通过 Shopware 6 API / VBA 通过 URL 添加图像全部内容,希望文章能够帮你解决通过 Shopware 6 API / VBA 通过 URL 添加图像所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。