发送 Guzzle 的“异步”请求而不调用“等待”

我正在尝试向端点发送请求,但我不想等待他们响应,因为我不需要响应。所以我正在使用 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 调用真正的异步请求?


缥缈止盈
浏览 990回答 3
3回答

Qyouu

then()如果您不想等待结果,请调用方法:$client = new GuzzleClient();$promise = $client->getAsync($url)$promise->then();Empty then()call 会在不等待结果的情况下发出 HTTP 请求,非常类似于curl_setopt(CURLOPT_RETURNTRANSFER,false) 

青春有我

use Illuminate\Support\Facades\Http;...Some Code here$prom = Http::timeout(1)->async()->post($URL_STRING, $ARRAY_DATA)->wait();... Some more important code herereturn "Request sent"; //OR whatever you need to return这对我有用,因为我不需要总是知道响应。它仍然使用,wait()但由于超时值很小,它并没有真正等待响应。希望这对其他人有帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python