我如何将apikey和其他键传递到guzzle 6.3中的标头?

问题描述 投票:0回答:1

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

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

Api网址:app3.salesmanago.pl/api/contact/upsert

JSON DATA:
{
  "clientId":"w2ncrw06k7ny45umsssc",
  "apiKey":"ssssj2q8qp4fbp9qf2b8p49fz",
  "requestTime":1327056031488,
  "sha":"ba0ddddddb543dcaf5ca82b09e33264fedb509cfb4806c",
  "async" : true,
  "owner" : "[email protected]",
  "contact" : { 
        "email" : "[email protected]",
        "name" : "Test",
        "address":{
            "streetAddress":"Brzyczynska 123",
      }
    }
}

我得到以下成功结果

{
    "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']),
            ]);

        $current_timestamp = Carbon::now()->timestamp;
        $client = new Client();
        $request = $client->post('app3.salesmanago.pl/api/contact/upsert', [
            \GuzzleHttp\RequestOptions::JSON => [        
            'headers' => [
                'Accept' => 'application/json, application/json',
                'Content-Type' => 'application/json',
                'clientId' => 'dd2ncrw06k7ny45umce',
                'apiKey' => 'ddjdd2q8qp4fbp9qf2b8p49fdzd',
                'sha' => ' wba0b543dcaf5ca82b09e33264fedb4509cfb4806ec',
                "requestTime" => $current_timestamp,
                "owner" => "[email protected]",
            ],
            'form_params' => [
                'name' => $data['name'],
                'email' => $data['email'],
            ]
          ]
        ]);

        $response = $request->getBody();
        $r = json_decode($response);
        dd($r);
        return $user;
    }

当我运行我的应用程序并发送表单数据时,我使用与邮递员中相同的数据来获取此信息

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

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

有人可以告诉我我在做什么错吗?

php laravel laravel-5 guzzle guzzle6
1个回答
0
投票

仅供参考,不知道您使用的是哪种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" => "[email protected]",
])->post('app3.salesmanago.pl/api/contact/upsert', [
    'name' => $data['name'],
    'email' => $data['email'],
]);

if($response->successful()){
 dd($response->json())
}else{
  // handle yo errors
}
© www.soinside.com 2019 - 2024. All rights reserved.