gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:54:50 27 4
gpt4 key购买 nike

我正在使用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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com