如何在 guzzle 6.3 中将 apikey 和其他密钥传递给 header?

How do I pass apikey and other keys to header in guzzle 6.3?

我有一个简单的注册表,用户可以在我的应用程序中注册,现在我想将提交的数据发送到另一个服务。

首先,我使用邮递员面板中的原始选项测试我的请求,如下所示。

Api url : app3.salesmanago.pl/api/contact/upsert

JSON DATA:
{
  "clientId":"w2ncrw06k7ny45umsssc",
  "apiKey":"ssssj2q8qp4fbp9qf2b8p49fz",
  "requestTime":1327056031488,
  "sha":"ba0ddddddb543dcaf5ca82b09e33264fedb509cfb4806c",
  "async" : true,
  "owner" : "adam@rce.com",
  "contact" : { 
        "email" : "test-1@konri.com",
        "name" : "Test",
        "address":{
            "streetAddress":"Brzyczynska 123",
      }
    }
}

UPDATE I get the following success result

{
    "success": true,
    "message": [],
    "contactId": "b52910be-9d22-4830-82d5-c9dc788888ba",
    "externalId": null
}

现在在 laravel

中使用 guuzle htpp 请求
protected function create(array $data)
{
    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        ]);

        $client = new client();
        $current_timestamp = Carbon::now()->timestamp;
        try {
            $request = $client->post('app3.salesmanago.pl/api/contact/upsert', [
                \GuzzleHttp\RequestOptions::HEADERS      => array(
                    'debug'        => true,
                    'Accept'       => 'application/json',
                    'Content-Type' => 'application/json',
                    'clientId'     => 's255hncrw06k7ny45umc',
                    'apiKey'       => 'sj2q8rt5qp4fbp9qf2b8p49fz',
                    'sha'          => 'ba0br45543dcaf5ca82b09e33264fedb509cfb4806c',
                    'requestTime'  =>  $current_timestamp,
                    'owner'        => 'adwamtrw@fere.com',
                    'http_error'   => true
                ),

                \GuzzleHttp\RequestOptions::JSON => [
                    'form_params' => [
                        'name'  => $data['name'],
                        'email' => $data['email'],
                    ],
                ],
            ]);  
        }
        catch (GuzzleHttp\Exception\ClientException $e) {
            $response = $e->getResponse();
            $responseBodyAsString = $response->getBody()->getContents();
        }
        $status = $request->getStatusCode();
        $response = $request->getBody();
        $r = json_decode($response);
        dd($r);
        dd($status, $r );
    return $user;
}

当我 运行 我的应用程序并发送表单数据时,我使用与邮递员相同的数据得到这个

{#306 ▼
  +"success": false
  +"message": array:1 [▼
    0 => "Not authenticated"
  ]
  +"contactId": null
  +"externalId": null
}

我的 API 密钥和其他 header 数据似乎没有按要求传递给 header,

有人能告诉我我做错了什么吗?

仅供参考,不确定 Laravel 您使用的是什么,但现在 The Laravel HTTP client 让这一切变得容易多了。

$response = Http::withHeaders([
                'Accept' => 'application/json, application/json',
                'Content-Type' => 'application/json',
                'clientId' => 'dd2ncrw06k7ny45umce',
                'apiKey' => 'ddjdd2q8qp4fbp9qf2b8p49fdzd',
                'sha' => ' wba0b543dcaf5ca82b09e33264fedb4509cfb4806ec',
                "requestTime" => $current_timestamp,
                "owner" => "testemail@wp.com",
])->post('app3.salesmanago.pl/api/contact/upsert', [
    'name' => $data['name'],
    'email' => $data['email'],
]);

if($response->successful()){
 dd($response->json())
}else{
  // handle yo errors
}

也许是这样的。请注意,根据 API,一些值应作为 headers 传递(接受,并且 Content-Type - 通常用作 headers,顺便说一句 -),而其他值作为body。这是 clientId 和 apiKey 等身份验证值的情况。

我手边没有安装 guzzle 6,但您可以尝试修改代码以包含该数据,而不是在请求的 headers 部分中,而是在 body:

$request = $client->post('app3.salesmanago.pl/api/contact/upsert', [
        \GuzzleHttp\RequestOptions::HEADERS      => array(
            'debug'        => true,
            'Accept'       => 'application/json',
            'Content-Type' => 'application/json',
        ),

        \GuzzleHttp\RequestOptions::JSON => [
            'form_params' => [
                'name'  => $data['name'],
                'email' => $data['email'],
                'clientId'     => 's255hncrw06k7ny45umc',
                'apiKey'       => 'sj2q8rt5qp4fbp9qf2b8p49fz',
                'sha'          => 'ba0br45543dcaf5ca82b09e33264fedb509cfb4806c',
                'requestTime'  =>  $current_timestamp,
                'owner'        => 'adwamtrw@fere.com',
                'http_error'   => true
            ],
        ],
    ]);

我不确定 RequestOptions::JSON 下的 'form_params',但是也许你可以将值直接放在 RequestOptions::JSON.