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

前端 未结 4 461
盖世英雄少女心
盖世英雄少女心 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

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

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

    Request with POST files

    $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

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

    Async POST data

    Usefull for long server operations.

    $client = new \GuzzleHttp\Client();
    $promise = $client->requestAsync('POST', 'http://www.example.com/user/create', [
        'form_params' => [
            'email' => 'test@gmail.com',
            '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();
        }
    );
    

    Set headers

    According to documentation, you can set headers :

    // Set various headers on a request
    $client->request('GET', '/get', [
        'headers' => [
            'User-Agent' => 'testing/1.0',
            'Accept'     => 'application/json',
            'X-Foo'      => ['Bar', 'Baz']
        ]
    ]);
    

    More information for debugging

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

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

    Documentation is more explicits about new possibilities.

提交回复
热议问题