简体   繁体   English

Guzzle Post请求与auth

[英]Guzzle Post request with auth

First time I am using guzzle. 我第一次使用guzzle。 Can anybody let me know How can I write Guzzle call for this request. 任何人都可以告诉我如何为此请求编写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"]}}'

I am facing problem in -u of curl request. 我在卷曲请求的-u中遇到问题。

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

I have tried this but unable to call API 我试过这个但无法调用API

Any Suggestion or Help. 任何建议或帮助。

As per the docs basic auth should be specified using array of two elements (login and password): 根据文档,应使用两个元素的数组(登录名和密码)指定基本身份验证:

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

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

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