4

How to get response when I post request with Guzzle I use "guzzle/guzzle": "^3.9", I post fro some url request and I need response body, but with Guzzle I dont find this is response, where use standard php function file_get_contents I have response body. How in Guzzle het response body ?

$guzzle = new Client();
$postCell = $guzzle
    ->post($url, [], $addProtectedRangeJson)
    ->addHeader('Authorization', 'Bearer ' . $arrayAccessTokenClient)
    ->addHeader('Content-type', 'application/json')
;

$postCell
    ->send()
    ->getBody(true)
;

$contents = (string) $postCell->getBody(); // get body that I post -> my request, not response
$contents = $postCell->getBody()->getContents(); // not find getContents, ask me did you mean to call getContentMd5 
$answer = json_decode($postCell->getResponse()->getBody(), true);

return $answer;

Now $answer:

result = {Guzzle\Http\EntityBody} [7]
readWriteHash = {array} [2]
contentEncoding = false
rewindFunction = null
stream = {resource} resource id='77' type='stream'
size = null
cache = {array} [9]
 wrapper_type = "PHP"
 stream_type = "TEMP"
 mode = "w+b"
 unread_bytes = 0
 seekable = true
 uri = "php://temp"
 is_local = true
 is_readable = true
 is_writable = true
customData = {array} [1]
 default = true

I try $contents = (string) $postCell->getBody(); but have my request, not response, but I need response

        $url = 'some_url';
    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'=>"Content-type: application/json\r\n" .
                "Authorization: Bearer $arrayAccessTokenClient\r\n",
            'content' => $addProtectedRangeJson
        )
    );
    // Creating the context for the request
    $context = stream_context_create($opts);
    $response = file_get_contents($url, FALSE, $context);
    $responseData = json_decode($response, TRUE);

How to get response body in Guzzle ??

I try http guzzle

$headers = [
        'Authorization' => 'Bearer ' . $arrayAccessTokenClient,
        'Content-type' => 'application/json'
    ];
    $guzzle = new \GuzzleHttp\Client($headers);

    $request = $guzzle
        ->post('https://sheets.googleapis.com/v4/spreadsheets/' . $spreadsheetId . ':batchUpdate',
            $addProtectedRange
        );
    $response = $request->getBody()->getContents();

    return $response;

But have 401? hot to add header ?

UPDATE

    $headers = [
        'Authorization' => 'Bearer ' . $arrayAccessTokenClient,
        'Content-type' => 'application/json'
    ];
    $guzzle = new \GuzzleHttp\Client();
    $request = new Request('POST', 'https://sheets.googleapis.com/v4/spreadsheets/' . $spreadsheetId . ':batchUpdate', $headers);
    $response = $guzzle
        ->send($request, $addProtectedRange);

    $answer = $response->getBody()->getContents();

    return $answer;

but have

  "Catchable Fatal Error: Argument 1 passed to GuzzleHttp\\Client::send() must be an instance of Psr\\Http\\Message\\RequestInterface, instance of Guzzle\\Http\\Message\\Request given, called in /home/ivan/host/aog-code/src/Artel/SiteBundle/Helper/GoogleSpreadSheetHelper.php on line 144 and defined"
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
shuba.ivan
  • 3,824
  • 8
  • 49
  • 121

2 Answers2

8

I find solution

$headers = [
    'Authorization' => 'Bearer ' . $arrayAccessTokenClient,
    'Content-type' => 'application/json'
];
$guzzle = new \GuzzleHttp\Client();
$request = new \GuzzleHttp\Psr7\Request('POST', 'https://sheets.googleapis.com/v4/spreadsheets/' . $spreadsheetId . ':batchUpdate', $headers);
$response = $guzzle
    ->send($request, ['body' => $addProtectedRangeJson]);

$answer = $response->getBody()->getContents();

return $answer;
Jap Mul
  • 17,398
  • 5
  • 55
  • 66
shuba.ivan
  • 3,824
  • 8
  • 49
  • 121
2

To retrieve all the data, you can use casting operator:

$contents = (string) $response->getBody();

You can also do it with

$contents = $response->getBody()->getContents();

The difference between the two approaches is that getContents returns the remaining contents, so that a second call returns nothing unless you seek the position of the stream with rewind or seek .

$stream = $response->getBody();
$contents = $stream->getContents(); // returns all the contents
$contents = $stream->getContents(); // empty string
$stream->rewind(); // Seek to the beginning
$contents = $stream->getContents(); // returns all the contents

Instead, usings PHP's string casting operations, it will reads all the data from the stream from the beginning until the end is reached.

$contents = (string) $response->getBody(); // returns all the contents
$contents = (string) $response->getBody(); // returns all the contents
Sayse
  • 42,633
  • 14
  • 77
  • 146
Rax Shah
  • 531
  • 5
  • 18
  • `"guzzle/guzzle": "^3.9"` not have `getContents()` in `$response->getBody()` maybe you mean `guzzlehttp/guzzle` ?? – shuba.ivan Aug 04 '16 at 07:56
  • yes using Guzzlehttp/Guzzle – Rax Shah Aug 04 '16 at 08:14
  • how to add header to `Guzzlehttp/Guzzle` ? I uopdate question – shuba.ivan Aug 04 '16 at 08:18
  • You can check this URl : http://docs.guzzlephp.org/en/latest/request-options.html#headers – Rax Shah Aug 04 '16 at 08:21
  • Ok, add header but have ` "Catchable Fatal Error: Argument 1 passed to GuzzleHttp\\Client::send() must be an instance of Psr\\Http\\Message\\RequestInterface, instance of Guzzle\\Http\\Message\\Request given, called in /home/ivan/host/aog-code/src/Artel/SiteBundle/Helper/GoogleSpreadSheetHelper.php on line 144 and defined" ` – shuba.ivan Aug 04 '16 at 08:38