Laravel 外部补丁 API

Laravel patch external API

我正在从外部 API 来源获取数据。

我用它来检索我的订单

$data = Http::get(
    $this->URL .'orders?language=nl_NL&limit=100&format=json&status_changed_start_date='
       . $last_poll
       . '&ordering=asc&partner_token='.$this->PARTNERKEY.'&token='.$this->MERCHANTTOKEN.''
)->json();

按照合作伙伴网站上的指南,我应该使用这种格式来更新特定记录。

curl -X PATCH $this->URL ."orders/2?language=nl_NL&format=json&override_minimum=false&override_stock=false&mutate_stock=true&partner_token='.$this->PARTNERKEY.'&token='.$this->MERCHANTTOKEN.'" \
     -H  "accept: application/json" \
     -H  "Content-Type: application/json" \
     -d "{\"status\":2,\"archived\":true,\"comments\":{\"internal\":\"Owner's commentary\"}}"

我如何在 Laravel 控制器中实现它?

使用 HTTP Client 提供的 Laravel:

use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;

class MyController
{
    public function update (Request $request)
    {
        // Maybe use from $request?
        $id = 2;

        Http::patch($this->url . "/orders/" . $id, [
            'version' => 1,
            'order_number' => 2,
            'language' => 'nl_NL',
            'format' => 'json',
            'override_minimum' => false,
            // .. put here the other parameters
        ]);
    }   
}

嗨,这是我的最终代码。

这似乎符合我的需要

$client = new Client([
                // Base URI is used with relative requests
                'base_uri' => $this->URL,
            ]);
              
            $response = $client->request('PATCH', 'orders/' . $item['number'] .'?language=nl_NL&format=json&override_minimum=false&override_stock=false&mutate_stock=true&partner_token='.$this->PARTNERKEY.'&token='.$this->MERCHANTTOKEN, [
                'json' => [
                    'status' => 2,
                    'archived' => true,
                    'comments' => ['internal' => $comment]],
            ]);