如何解决Square PHP API目录图像上载-应用程序/八位字节流错误
我正在尝试将图像从s3存储桶上传到目录图像。以下是我所拥有的文档:
$imageData = new CatalogImage();
$imageData->setCaption('Some image name');
$imageObject = new Catalogobject(CatalogobjectType::IMAGE,'#TEStimage');
$imageObject->setimageData($imageData);
// Update catalog object in the api
$requestId = (string) Str::uuid();
$imageUploadBody = new CreateCatalogImageRequest($requestId,$imageObject);
$imageUploadBody->setobjectId($externalId); // $externalId is a valid CatalogItem Id
$imageUploadBody->setimage($imageObject);
// Save local copy of image (in prod this is coming from my s3 bucket)
$filePath = sys_get_temp_dir() . '/filename.jpeg';
$fileUrl = 'https://www.aworldofdifference.co.nz/pics/45,3,1,6,0/20151002142909/test-jpg.jpeg';
file_put_contents($filePath,file_get_contents($fileUrl));
// Configure object url
$imageFile = FileWrapper::createFromPath($filePath);
$apiResponse = $this->catalogApi->createCatalogImage($imageUploadBody,$imageFile);
但是,即使所提供文件的文件类型为.jpeg(即受支持的类型),我在上传后仍会从Square收到以下错误消息。
array:1 [
0 => Square\Models\Error {#1613
-category: "INVALID_REQUEST_ERROR"
-code: "INVALID_CONTENT_TYPE"
-detail: "Only [image/jpeg image/pjpeg image/png image/x-png image/gif] content type allowed. but got application/octet-stream"
-field: null
}
]
在理想的世界中,我只想将我的存储桶中的公共图片URL提供给上载的目录,但实际上,FileWrapper::createFromPath()
正在使用CurlFile()
,我的理解是不支持远程文件上传。 https://github.com/square/square-php-sdk/blob/master/src/Utils/FileWrapper.php#L63
我已经对此进行了一段时间的黑客攻击,但没有一个好的解决方案来展示它,因此任何建议将不胜感激。谢谢!
更新/回答
联系Square支持人员后,解决方法是显式提供哑剧类型作为FileWrapper::createFromPath
转向的第二个参数:
$imageFile = FileWrapper::createFromPath($filePath);
进入
$imageFile = FileWrapper::createFromPath($filePath,'image/jpeg');
Square不需要此字段,但其SDK CurlFile
中的基础函数默认为application/octet-stream
的mime类型,这就是为什么我得到错误的原因。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。