繁体   English   中英

Laravel 发送带有 guzzle http 请求的 post 请求

[英]Laravel sending post request with guzzle http request

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

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

Api 网址: 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",
      }
    }
}

我得到以下成功结果

{
    "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" => "testemail@wp.com",
            ],
            '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
}

有人能告诉我为什么在 Postman 中一切正常,但在 laravel 中却失败了吗?

我的代码有什么问题?

因为你在 json 选项中写了标题。 这是正确的

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

你可以像这样使用它

$client->request('POST', 'app3.salesmanago.pl/api/contact/upsert', [
    'form_params' => [
        'name'  => $data['name'],
        'email' => $data['email'],
    ],
    'headers' => [
        'Accept'       => 'application/json, application/json',
        'Content-Type' => 'application/json',
        'clientId'     => 'dd2ncrw06k7ny45umce',
        'apiKey'       => 'ddjdd2q8qp4fbp9qf2b8p49fdzd',
        'sha'          => ' wba0b543dcaf5ca82b09e33264fedb4509cfb4806ec',
        'requestTime'  => $current_timestamp,
        'owner'        => 'testemail@wp.com',
    ]
]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM