【问题标题】:How do I send parameters for a PUT request in Guzzle 5?如何在 Guzzle 5 中为 PUT 请求发送参数?
【发布时间】:2015-03-25 00:43:48
【问题描述】:

我有这个代码用于发送 POST 请求的参数,它有效:

$client = new GuzzleHttp\Client();
$request = $client->createRequest('POST', 'http://example.com/test.php');
$body = $request->getBody();

$request->getBody()->replaceFields([
    'name' => 'Bob'
]);

但是,当我将 POST 更改为 PUT 时,我收到此错误:

Call to a member function replaceFields() on a non-object

这是因为 getBody 返回的是 null。

在body中发送PUT参数真的正确吗?还是应该在 URL 中做?

【问题讨论】:

    标签: php rest guzzle


    【解决方案1】:

    在 Guzzle 6 中,如果你想将 JSON 数据传递给你的 PUT 请求,那么你可以如下实现:

               $aObj = ['name' => 'sdfsd', 'language' => 'En'];
    
                $headers = [
                    "User-Agent"    => AGENT,
                    "Expect"        => "100-continue",
                    "api-origin"    => "LTc",
                    "Connection"    => "Keep-Alive",
                    "accept"        => "application/json",
                    "Host"          => "xyz.com",
                    "Accept-Encoding"=> " gzip, deflate",
                    "Cache-Control"=> "no-cache",
                    "verify"        => false,
                    "Content-Type" => "application/json"
                ];
    
              $client = new GuzzleHttp\Client([
                'auth'  => ['testUsername', 'testPassword'],
                'timeout'   => '10000',
                'base_uri'  => YOUR_API_URL,
                'headers' => $headers
            ]);
    
            $oResponse = $client->request('PUT', '/user/UpdateUser?format=json', ['body' => json_encode( $aObj, JSON_UNESCAPED_SLASHES)]);
    
            $oUser = json_decode( $oResponse->getBody());
    

    【讨论】:

      【解决方案2】:

      如果您使用的是 Guzzle 版本 6,您可以通过以下方式发出 PUT 请求

      $client = new \GuzzleHttp\Client();
      
      $response = $client->put('http://example.com/book/1', [
          'query' => [
              'price' => '50',
          ]
      ]);
      
      print_r($response->getBody()->getContents());
      

      【讨论】:

        【解决方案3】:

        当服务在等待 json 原始数据时

        $request = $client->createRequest('PUT', '/yourpath', ['json' => ['key' => 'value']]);
        

        $request = $client->createRequest('PUT', '/yourpath', ['body' => ['value']]);
        

        【讨论】:

          【解决方案4】:

          根据the manual

          body 选项用于控制包围实体的主体 请求(例如,PUT、POST、PATCH)。

          put'ing 的记录方法是:

          $client = new GuzzleHttp\Client();
          
          $client->put('http://httpbin.org', [
              'headers'         => ['X-Foo' => 'Bar'],
              'body'            => [
                  'field' => 'abc',
                  'other_field' => '123'
              ],
              'allow_redirects' => false,
              'timeout'         => 5
          ]);
          

          编辑

          根据您的评论:

          您缺少 createRequest 函数的第三个参数 - 组成 postput 数据的键/值对数组:

          $request = $client->createRequest('PUT', '/put', ['body' => ['foo' => 'bar']]);
          

          【讨论】:

          • 问题是我希望能够使用 createRequest() 方法来完成它,因为我正在确定以编程方式使用哪种方法
          • 您缺少 createRequest 函数的第三个参数 - 组成 postput 数据的键/值对数组:$request = $client->createRequest('PUT', '/put', ['json' => ['foo' => 'bar']]);
          • 答案中的链接不再有效,但这是最新版本的 Guzzle:docs.guzzlephp.org/en/latest/request-options.html#body
          • 或更准确地说是docs.guzzlephp.org/en/latest/request-options.html#form-paramsbody 已被弃用,在 Guzzle 6 中应为 form-params
          猜你喜欢
          • 1970-01-01
          • 2019-12-12
          • 1970-01-01
          • 1970-01-01
          • 2017-11-15
          • 2012-07-09
          • 2011-01-10
          • 2021-09-05
          相关资源
          最近更新 更多