我正在尝试向端点发送请求,但我不想等待他们响应,因为我不需要响应。所以我正在使用 Guzzle,方法如下:$url = 'http://example.com';$client = new \Guzzelhttp\Client();$promise = $client->postAsync($url, [ 'headers' => ['Some headers and authorization'], 'query' => [ 'params' => 'params', ]])->then(function ($result) { // I don't need the result. So I just leave it here.});$promise->wait();AI 理解,我必须调用 上的wait方法client才能实际发送请求。但这完全否定了“异步”请求,因为如果 url 不可访问或服务器关闭,应用程序将等待超时或任何其他错误。所以,这里的问题是,当您无论如何都必须等待响应时,Guzzle 所说的“异步”是什么意思?以及如何使用 PHP 调用真正的异步请求?
3 回答
![?](http://img1.sycdn.imooc.com/545868190001d52602200220-100-100.jpg)
Qyouu
TA贡献1786条经验 获得超11个赞
then()如果您不想等待结果,请调用方法:
$client = new GuzzleClient();
$promise = $client->getAsync($url)
$promise->then();
Empty then()call 会在不等待结果的情况下发出 HTTP 请求,非常类似于
curl_setopt(CURLOPT_RETURNTRANSFER,false)
![?](http://img1.sycdn.imooc.com/545845d30001ee8a02200220-100-100.jpg)
青春有我
TA贡献1784条经验 获得超8个赞
use Illuminate\Support\Facades\Http;
...Some Code here
$prom = Http::timeout(1)->async()->post($URL_STRING, $ARRAY_DATA)->wait();
... Some more important code here
return "Request sent"; //OR whatever you need to return
这对我有用,因为我不需要总是知道响应。它仍然使用,wait()但由于超时值很小,它并没有真正等待响应。
希望这对其他人有帮助。
添加回答
举报
0/150
提交
取消