PHP GuzzleHttp.如何使用params发帖请求?

Ars*_*sen 81 php httpclient request guzzle

如何使用GuzzleHttp(5.0版)发布帖子请求.我正在尝试执行以下操作并收到错误

$client = new \GuzzleHttp\Client();
$client->post(
    'http://www.example.com/user/create',
    array(
        'email' => 'test@gmail.com',
        'name' => 'Test user',
        'password' => 'testpassword'
    )
);
Run Code Online (Sandbox Code Playgroud)

PHP致命错误:未捕获异常'InvalidArgumentException',消息'没有方法可以处理电子邮件配置密钥'

Sam*_*zon 169

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

$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',
    ]
]);
Run Code Online (Sandbox Code Playgroud)

请求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'
        ]
    ]
]);
Run Code Online (Sandbox Code Playgroud)

REST动词与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');
Run Code Online (Sandbox Code Playgroud)

异步POST数据

适用于长服务器操作.

$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();
    }
);
Run Code Online (Sandbox Code Playgroud)

有关调试的更多信息

如果您想了解更多详细信息,可以使用以下debug选项:

$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
]);
Run Code Online (Sandbox Code Playgroud)

文档更多地阐述了新的可能性.


Mar*_*rco 72

此方法现已在6.0中弃用.而不是'身体'使用'form_params'

试试这个

$client = new \GuzzleHttp\Client();
$client->post(
    'http://www.example.com/user/create',
    array(
        'form_params' => array(
            'email' => 'test@gmail.com',
            'name' => 'Test user',
            'password' => 'testpassword'
        )
    )
);
Run Code Online (Sandbox Code Playgroud)

  • 此方法现已在6.0中弃用.而不是'身体'使用'form_params'. (84认同)
  • 传递"body"请求选项作为发送POST请求的数组已被弃用.请使用"form_params"请求选项发送application/x-www-form-urlencoded请求,或使用"multipart"请求选项发送multipart/form-data请求. (5认同)

Sco*_*ang 31

注意在Guzzle V6.0 +中,获取以下错误的另一个来源可能是错误地使用JSON作为数组:

传递"body"请求选项作为发送POST请求的数组已被弃用.请使用"form_params"请求选项发送application/x-www-form-urlencoded请求,或使用"multipart"请求选项发送multipart/form-data请求.

不正确:

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

正确:

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

正确:

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