PHP GuzzleHttp。如何使用params发布帖子请求?

时间:2015-01-07 17:58:43

标签: php request httpclient 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'
    )
);

但我收到错误:

  

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

4 个答案:

答案 0 :(得分:148)

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

请求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动词使用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');

异步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();
    }
);

有关调试的更多信息

如果您需要更多详细信息,可以使用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
]);

Documentation更多地阐述了新的可能性。

答案 1 :(得分:57)

  

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

试试这个

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

答案 2 :(得分:25)

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

  

传入"身体"请求选项作为发送POST的数组   请求已被弃用。请使用" form_params"请求   发送application / x-www-form-urlencoded请求的选项,或者   "多部分"请求发送multipart / form-data请求的选项。

<强>不正确

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

<强>正确

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

<强>正确

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

答案 3 :(得分:1)

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

添加

openssl.cafile文件

中的

php.ini

相关问题