【问题标题】:PHP GuzzleHttp. How to make a post request with params?PHP GuzzleHttp。如何使用参数发出 post 请求?
【发布时间】:2015-03-05 17:08:30
【问题描述】:

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

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

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

但我得到了错误:

PHP 致命错误:未捕获异常 'InvalidArgumentException' 并显示消息 'No method can handle the email config key'

【问题讨论】:

    标签: php request httpclient guzzle


    【解决方案1】:

    由于 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 动词用法

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

    设置标题

    根据documentation,可以设置headers:

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

    更多调试信息

    如果您想了解更多详细信息,可以使用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 更明确地说明了新的可能性。

    【讨论】:

    • 如何在 post 请求中发送查询字符串?
    • 你在找什么?如果查询字符串是 URL 的一部分,则必须直接将其添加到 URL 中,例如 example.com/user/create?mode=dev"
    • 我正在尝试使用 url 编码数据向 paypal 发送 post 请求。我认为它的 ['body'] 键。
    • @clockw0rk 我为您添加了一个 HTTP 标头部分。你有文档的链接
    • 天哪,谢谢
    【解决方案2】:

    试试这个

    $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'
            )
        )
    );
    

    【讨论】:

    • 此方法现已在 6.0 中弃用。而不是“body”使用“form_params”。
    • 不推荐将“body”请求选项作为数组传递以发送 POST 请求。请使用“form_params”请求选项发送 application/x-www-form-urlencoded 请求,或使用“multipart”请求选项发送 multipart/form-data 请求。
    • @JeremyQuinton ,所以你选择的不是那个...请回复
    • @madhur 看看下面的答案
    • 请编辑响应并添加此“此方法现在在 6.0 中已弃用。代替 'body' 使用 'form_params'”
    【解决方案3】:

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

    将“body”请求选项作为数组传入以发送 POST 请求已被弃用。请使用“form_params”请求 发送 application/x-www-form-urlencoded 请求的选项,或 发送多部分/表单数据请求的“多部分”请求选项。

    不正确

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

    【讨论】:

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

      添加

      openssl.cafilephp.ini 文件中

      【讨论】:

        猜你喜欢
        • 2016-05-30
        • 1970-01-01
        • 2017-09-13
        • 1970-01-01
        • 2015-05-08
        • 1970-01-01
        • 1970-01-01
        • 2014-10-28
        • 2011-04-28
        相关资源
        最近更新 更多