当我使用 Guzzle 发布请求时如何获得响应我使用"guzzle/guzzle": "^3.9",
我发布一些 url 请求并且我需要响应正文,但是使用 Guzzle 我没有发现这是响应,在使用标准 php 函数的地方file_get_contents我有响应正文。Guzzle het反应体如何?
$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;
Run Code Online (Sandbox Code Playgroud)
现在$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
Run Code Online (Sandbox Code Playgroud)
我尝试$contents = (string) $postCell->getBody();但有我的请求,而不是回应,但我需要回应
$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);
Run Code Online (Sandbox Code Playgroud)
如何在 Guzzle 中获得响应体?
我尝试 http 狂饮
$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;
Run Code Online (Sandbox Code Playgroud)
但是有401吗?热添加标题?
更新
$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;
Run Code Online (Sandbox Code Playgroud)
但有
"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"
Run Code Online (Sandbox Code Playgroud)
我找到解决方案
$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;
Run Code Online (Sandbox Code Playgroud)