【问题标题】:Guzzle Post request with auth带有身份验证的 Guzzle Post 请求
【发布时间】:2017-06-01 03:19:06
【问题描述】:

我第一次使用 guzzle。谁能告诉我如何为这个请求编写 Guzzle 调用。

curl -X POST -u username:password -H "Content-Type: application/json" https://xyz/api/v1/accounts.json -d '{"user":{"username":"test","password":"xyz","first_name":"test","last_name":"test","email":"test@test.com","roles":["Administrator"]}}'

我在 curl 请求的 -u 中遇到问题。

I have written this code.
$response = $client->post($url, [
    'headers' => ['Content-type' => 'application/json'],
    'auth' => ['username:password'],
    'json' => [
        'username' => 'test'
    ],
]);
$results = $response->json();

我试过this但无法调用API

任何建议或帮助。

【问题讨论】:

    标签: php json curl guzzle


    【解决方案1】:

    根据docs 基本身份验证应使用包含两个元素(登录名和密码)的数组来指定:

    $client = new GuzzleHttp\Client();
    $url = "//xyz/api/v1/accounts.json";
            $response = $client->post($url, [
                'headers' => ['Content-type' => 'application/json'],
                'auth' => [
                    'test', 
                    'xyz'
                ],
                'json' => [
                    "username"=>"xyz",
                    "password"=>"xyz",
                    "first_name"=>"test",
                    "last_name"=>"test",
                    "email"=>"test@test.com",
                    "roles"=>"Administrator"
                ], 
            ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-01
      • 2020-11-27
      • 1970-01-01
      • 2015-09-21
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      相关资源
      最近更新 更多