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

前端 未结 4 458
盖世英雄少女心
盖世英雄少女心 2021-01-30 02:57

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

I am trying to do the following:

$client = new \\GuzzleHttp\\Client();
$client->post(
    \         


        
4条回答
  •  北恋
    北恋 (楼主)
    2021-01-30 03:27

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

    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.

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

提交回复
热议问题