php - Woocommerce Api:带有图像的商店类别获取错误错误请求 400
问题描述
我正在使用带有 woocommerce api 的 Laravel 8。我正在使用 Woocomerce api 的文档创建产品类别。
https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-product-category
我正在发送上传这样的图片的请求,
$categoryPhotoUrl = null;
if (request()->hasFile('images')) {
$file = $request->file('images') ;
$fileName = $file->getClientOriginalName() ;
$destinationPath = public_path().'/assets/images/categories' ;
$file->move($destinationPath,$fileName);
$categoryPhotoUrl = asset('/assets/images/categories').'/'.$fileName;
}
$request->request->add(['image' => ['src' => $categoryPhotoUrl]]);
CategoryService::created($request->except('_token', 'images'));
在服务类中我使用逻辑,
public static function created($data)
{
return (new WoocommerceClient())->postRecord('products/categories',$data);
}
和 WoocommerceClient 包含此逻辑,
$response = Http::withToken($this->authToken)->withHeaders([
"Content-Type" => "image/jpg"
])->post($this->baseUrl.$endpoint, $data);
dd($response);
如果我发送没有图像的数据它工作正常但如果我存储图像然后发送 url 我收到错误错误请求 400。
Illuminate\Http\Client\Response {#279 ▼
#response: GuzzleHttp\Psr7\Response {#314 ▼
-reasonPhrase: "Bad Request"
-statusCode: 400
-headers: array:20 [▶]
-headerNames: array:20 [▶]
-protocol: "1.1"
-stream: GuzzleHttp\Psr7\Stream {#313 ▶}
}
我该如何解决。谢谢。
解决方案
通过大量搜索,我将能够修复它。我已经使用 fetch 在 wordpress 数据库上上传图像。然后我得到它包含源网址的对象,然后我使用 woocommerce 发送它
推荐阅读
- ios - 带有端口映射的 docker-compose 中的 iOS postgres
- elasticsearch - 在 Kibana 中升级 Xpack 许可证
- shellexecute - 使用 Shellexecute 在后台静默打开应用程序
- java - 需要一些关于 requireNonNull 的说明
- react-native - 即使 props 有索引,也无法访问 props.index
- javascript - 如何删除位于圆圈边界之外的标记?
- tfs - TFS 无法识别导入的数据库
- spring - 在 Twitter 克隆应用程序中关注用户的 Spring Rest Api 设计?
- typescript - 为什么 TypeScript 有一个结构化的类型系统?
- java - 如何在 e clipse 中配置生成的 Sources