swoft-cloud / swoft

🚀 PHP Microservice Full Coroutine Framework
https://swoft.org
Apache License 2.0
5.58k stars 786 forks source link

如何使用 Guzzle 同步方式发送一个http请求 #1393

Open zhenhua2340 opened 4 years ago

zhenhua2340 commented 4 years ago
Q A
Bug report? yes
Feature request? no
Swoft version 2.0.10
Swoole version 4.5.5
PHP version 7.2.33
Runtime environment Docker

Guzzle 发送HTTP请求是,会自动异步,且没有返回值,代码:

$client = new \GuzzleHttp\Client();  
$res = $client->request('GET', 'http://127.0.0.1:9600');  
echo "\n";  
var_dump("结果:".$res->getBody()->getContents());  

控制台显示:

Server start success (Master PID: 861, Manager PID: 865)
我是返回值   ----------------------> 这里是 Guzzle 自动打印的,代码没有执行打印输出
string(9) "结果:"        ----------------------> 这里是获取的请求返回结果输出,没拿到返回值

我想要同步执行,拿到结果才能执行下一步,怎么破?

sakuraovq commented 3 years ago

这看看 swoole 吧

andiechang commented 3 years ago

没遇过你这种情形,这段代码是放在哪里执行的?