PHP GuzzleHttp。如何使用参数进行发布请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27825667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:32:36  来源:igfitidea点击:

PHP GuzzleHttp. How to make a post request with params?

phprequesthttpclientguzzle

提问by Arsen

How to make a post request with GuzzleHttp( version 5.0 ).

如何使用 GuzzleHttp(5.0 版)发出 post 请求。

I am trying to do the following:

我正在尝试执行以下操作:

$client = new \GuzzleHttp\Client();
$client->post(
    'http://www.example.com/user/create',
    array(
        'email' => '[email protected]',
        'name' => 'Test user',
        'password' => 'testpassword'
    )
);

But I am getting the error:

但我收到错误:

PHP Fatal error: Uncaught exception 'InvalidArgumentException' with the message 'No method can handle the email config key'

PHP 致命错误:未捕获异常“InvalidArgumentException”,消息为“No method can handle the email config key”

采纳答案by Marco

Try this

尝试这个

$client = new \GuzzleHttp\Client();
$client->post(
    'http://www.example.com/user/create',
    array(
        'form_params' => array(
            'email' => '[email protected]',
            'name' => 'Test user',
            'password' => 'testpassword'
        )
    )
);

回答by Samuel Dauzon

Since Marco's answer is deprecated, you must use the following syntax (according jasonlfunk's comment) :

由于Marco 的答案已弃用,您必须使用以下语法(根据 jasonlfunk 的评论):

$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'http://www.example.com/user/create', [
    'form_params' => [
        'email' => '[email protected]',
        'name' => 'Test user',
        'password' => 'testpassword',
    ]
]);

Request with POST files

使用 POST 文件请求

$response = $client->request('POST', 'http://www.example.com/files/post', [
    'multipart' => [
        [
            'name'     => 'file_name',
            'contents' => fopen('/path/to/file', 'r')
        ],
        [
            'name'     => 'csv_header',
            'contents' => 'First Name, Last Name, Username',
            'filename' => 'csv_header.csv'
        ]
    ]
]);

REST verbs usage with params

带参数的 REST 动词用法

// PUT
$client->put('http://www.example.com/user/4', [
    'body' => [
        'email' => '[email protected]',
        'name' => 'Test user',
        'password' => 'testpassword',
    ],
    'timeout' => 5
]);

// DELETE
$client->delete('http://www.example.com/user');

Async POST data

异步 POST 数据

Usefull for long server operations.

对长时间的服务器操作很有用。

$client = new \GuzzleHttp\Client();
$promise = $client->requestAsync('POST', 'http://www.example.com/user/create', [
    'form_params' => [
        'email' => '[email protected]',
        'name' => 'Test user',
        'password' => 'testpassword',
    ]
]);
$promise->then(
    function (ResponseInterface $res) {
        echo $res->getStatusCode() . "\n";
    },
    function (RequestException $e) {
        echo $e->getMessage() . "\n";
        echo $e->getRequest()->getMethod();
    }
);

More information for debugging

有关调试的更多信息

If you want more details information, you can use debugoption like this :

如果您需要更多详细信息,可以使用如下debug选项:

$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'http://www.example.com/user/create', [
    'form_params' => [
        'email' => '[email protected]',
        'name' => 'Test user',
        'password' => 'testpassword',
    ],
    // If you want more informations during request
    'debug' => true
]);

Documentationis more explicits about new possibilities.

文档更明确地说明了新的可能性。

回答by Scott Yang

Note in Guzzle V6.0+, another source of getting the following error may be incorrect use of JSON as an array:

请注意,在 Guzzle V6.0+ 中,出现以下错误的另一个原因可能是不正确地使用 JSON 作为数组:

Passing in the "body" request option as an array to send a POST request has been deprecated. Please use the "form_params" request option to send a application/x-www-form-urlencoded request, or a the "multipart" request option to send a multipart/form-data request.

不推荐将“body”请求选项作为数组传递以发送 POST 请求。请使用“form_params”请求选项发送 application/x-www-form-urlencoded 请求,或使用“multipart”请求选项发送 multipart/form-data 请求。

Incorrect:

不正确

$response = $client->post('http://example.com/api', [
    'body' => [
        'name' => 'Example name',
    ]
])

Correct:

正确

$response = $client->post('http://example.com/api', [
    'json' => [
        'name' => 'Example name',
    ]
])

Correct:

正确

$response = $client->post('http://example.com/api', [
    'headers' => ['Content-Type' => 'application/json'],
    'body' => json_encode([
        'name' => 'Example name',
    ])
])

回答by Prakash D

$client = new \GuzzleHttp\Client();
$request = $client->post('http://demo.website.com/api', [
    'body' => json_encode($dataArray)
]);
$response = $request->getBody();

Add

添加

openssl.cafilein php.inifile

openssl.cafilephp.ini文件中