php - GuzzleHttp 不显示来自 Node.js 服务器的响应

标签 php node.js laravel guzzle

我正在使用GuzzleHttp。我想从 Laravel 向 Nodejs 发送请求。

    try {

        $client = new \GuzzleHttp\Client();
        $url = 'http://localhost:8085/api/test/get';
        $headers = [
            'Accept' => 'application/json',
        ];


        $res = $client->post($url, [
            'headers' => $headers,
            'json' => $data,
        ]);
        dd($res->getBody());
    }
    catch(\Exception $e) {

       echo $e->getMessage();
    }

我的请求也发送了,但是$res->getBody()没有显示我的nodejs结果。 (我确信nodejs有响应)

我的nodejs代码:

   total_result            = _res.length
    response.total_result   = total_result
    response.result         = _res
    console.log(response)
    res.setHeader('Content-Type', 'application/json')
    res.status(200).json(response); 

dd($res->getBody());的结果是:

Stream {#2694
  -stream: stream resource @1095
    wrapper_type: "PHP"
    stream_type: "TEMP"
    mode: "w+b"
    unread_bytes: 0
    seekable: true
    uri: "php://temp"
    options: []
  }
  -size: null
  -seekable: true
  -readable: true
  -writable: true
  -uri: "php://temp"
  -customMetadata: []
}

最佳答案

解决了。您应该使用异步请求:

$request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org');
$promise = $client->sendAsync($request)->then(function ($response) {
    echo 'I completed! ' . $response->getBody();
});

$promise->wait();

https://github.com/guzzle/guzzle

关于php - GuzzleHttp 不显示来自 Node.js 服务器的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57751199/

相关文章:

javascript - 改为 JavaScript 的 array_count_values

php - 确定 $_POST ["X"] 是否为空

javascript - 在 Node.js 中等待 promise 履行

javascript - 在 React.js 中集成 PayPal 的 iframe 渲染错误

php - 如何替换爆炸中的数组键

PHP do-while 循环在执行 sql 代码时以耗尽内存错误结束

关于返回多个值的 php 风格

node.js - 如何从 Nest.js 提供 Assets 并添加中间件来检测图像请求

javascript - 包含 Javascript

Laravel nova 在资源创建页面上显示自定义消息