首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在口香糖中获取响应体

如何在口香糖中获取响应体
EN

Stack Overflow用户
提问于 2016-08-04 07:05:27
回答 2查看 15.8K关注 0票数 4

如何获得响应,当我用口吻发布请求时,我使用"guzzle/guzzle": "^3.9",,我发布了一些url请求,我需要响应体,但是我不认为这是响应,这里使用标准的php函数file_get_contents,我有响应体。在口香糖中如何反应身体?

代码语言:javascript
运行
复制
$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;

现在$answer

代码语言:javascript
运行
复制
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

我尝试$contents = (string) $postCell->getBody();,但有我的请求,没有响应,但我需要响应

代码语言:javascript
运行
复制
        $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);

如何在口香糖中得到反应体??

我试着吃口香糖

代码语言:javascript
运行
复制
$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;

但有401?热添加标题?

更新

代码语言:javascript
运行
复制
    $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;

但有

代码语言:javascript
运行
复制
  "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"
EN

回答 2

Stack Overflow用户

发布于 2016-08-04 09:15:37

我找到了解决办法

代码语言:javascript
运行
复制
$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;
票数 8
EN

Stack Overflow用户

发布于 2016-08-04 07:10:26

要检索所有数据,可以使用强制转换操作符:

代码语言:javascript
运行
复制
$contents = (string) $response->getBody();

你也可以用

代码语言:javascript
运行
复制
$contents = $response->getBody()->getContents();

这两种方法的不同之处在于,getContents返回剩余的内容,因此第二个调用将不会返回任何内容,除非您通过倒带或查找来查找流的位置。

代码语言:javascript
运行
复制
$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

相反,使用PHP的字符串转换操作,它将读取从开始到结束的流中的所有数据。

代码语言:javascript
运行
复制
$contents = (string) $response->getBody(); // returns all the contents
$contents = (string) $response->getBody(); // returns all the contents
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38760592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档