【问题标题】:Set raw POST request body in guzzle 6在 guzzle 6 中设置原始 POST 请求正文
【发布时间】:2018-09-21 10:18:29
【问题描述】:

是否仍然可以在 Guzzle 6 中设置原始请求正文(在其他地方正确创建的 XML 数据)?

这似乎不再起作用了:Specify raw body of a POST request with Guzzle

【问题讨论】:

    标签: php guzzle6


    【解决方案1】:

    是的,Guzzle 6 可以:

    ...
    $client = new \GuzzleHttp\Client();
    
    $rawContent = <<<XML
    <?xml version='1.0' encoding='us-ascii'?>
    <slideshow>
        <slide type="all">
            <title>Overview</title>
        </slide>
    </slideshow>
    XML;
    
    $request = new \GuzzleHttp\Psr7\Request(
        'post',
        'https://httpbin.org/post',
        ['content-type' => 'text/xml'],
        $rawContent
    );
    
    $responce = $client->send($request);
    
    print_r((string) $responce->getBody());
    

    如果您需要发送原始表单数据,您可以使用:

    ...
    $request = new \GuzzleHttp\Psr7\Request(
        'post',
        'https://httpbin.org/post',
        ['content-type' => 'application/x-www-form-urlencoded'],
        'a=1&b=2'
    );
    ...
    

    【讨论】:

    • @Alex 是你要找的东西吗?
    【解决方案2】:

    Guzzle 6 docs

    示例 1:

    // Provide the body as a string.
    $r = $client->request('POST', 'http://httpbin.org/post', [
        'body' => 'raw data'
    ]);
    

    示例 2:

    // Provide an fopen resource.
    $body = fopen('/path/to/file', 'r');
    $r = $client->request('POST', 'http://httpbin.org/post', ['body' => $body]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-15
      • 1970-01-01
      • 2018-10-22
      • 2015-12-02
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      相关资源
      最近更新 更多