首页 > 解决方案 > 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 ▶}
   }

我该如何解决。谢谢。

标签: phpwordpresslaravelwoocommerce

解决方案


通过大量搜索,我将能够修复它。我已经使用 fetch 在 wordpress 数据库上上传图像。然后我得到它包含源网址的对象,然后我使用 woocommerce 发送它


推荐阅读