Php Guzzle:将常规请求转换为异步请求
我有两个狂饮的请求,其中一个需要在另一个之前完成。我只是用Php Guzzle:将常规请求转换为异步请求,php,laravel,guzzle,Php,Laravel,Guzzle,我有两个狂饮的请求,其中一个需要在另一个之前完成。我只是用 sleep(7) 在两个狂饮请求之间切换,以确保第一个请求在第二个请求之前完全完成。问题在于速度,有时第一个请求可以在一秒钟或两秒钟内完成,有时可能是六秒钟以上 当前代码: $client = new Client([ 'base_uri' => 'https://ids.w2p-tools.com/', ]); $response = $client->request('POST', 'd003/requestp
sleep(7)
在两个狂饮请求之间切换,以确保第一个请求在第二个请求之前完全完成。问题在于速度,有时第一个请求可以在一秒钟或两秒钟内完成,有时可能是六秒钟以上
当前代码:
$client = new Client([
'base_uri' => 'https://ids.w2p-tools.com/',
]);
$response = $client->request('POST', 'd003/requestpreview.php', [
'form_params' => [
'auth' => $auth,
id' => $id,
'res' => $res,
'shownonprintlayer' => $show_non_print_layers
]
]);
sleep(7);
我已尝试通过执行以下操作将其转换为异步请求:
$client = new Client([
'base_uri' => 'https://ids.w2p-tools.com/',
]);
$request = new Request('POST', 'd003/requestpreview.php', [
'form_params' => [
'auth' => $auth,
'id' => $id,
'res' => $res,
'shownonprintlayer' => $show_non_print_layers
]
]);
$test = 'None Hit';
$promise = $client->sendAsync($request);
$promise->then(
function (ResponseInterface $res){
$test = 'ResponseInterface Hit';
},
function (RequestException $e) {
$test = 'RequestException Hit';
}
);
$response = $promise->wait();
return $test;
这总是返回“未命中”
做错了什么?
$test
中的闭包函数不是全局函数。
将test=none移至顶部,并将函数签名更改为使用var,因为它不是我们添加的对象&通过引用引用它,这意味着它也将全局更改
$test = 'None Hit'; // move up here to make it the default
$promise->then(
function (ResponseInterface $res) use (&$test) { // use $test by reference
$test = 'ResponseInterface Hit';
},
function (RequestException $e) use (&$test) {
$test = 'RequestException Hit';
}
);
$response = $promise->wait();
return $test; // should now have text from one of the closures