【问题标题】:Laravel sending post request with guzzle http requestLaravel 使用 guzzle http 请求发送 post 请求
【发布时间】:2020-07-14 11:54:27
【问题描述】:

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

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

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 中却失败了?

我的代码有什么问题?

【问题讨论】:

    标签: php json laravel guzzle guzzle6


    【解决方案1】:

    你可以像这样使用它

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

    【讨论】:

    • hej 你能解释一下你的代码是为了解决我的问题吗??
    【解决方案2】:

    因为您在 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",
        ],
    ]);
    

    【讨论】:

      猜你喜欢
      • 2018-03-21
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多