Home >Backend Development >PHP Tutorial >shell-php curl asynchronous request how to get results
<code> function request($url, $payload) { $cmd = "curl -X POST -H 'Content-Type: application/json'"; $cmd.= " -d '" . $payload . "' " . "'" . $url . "'"; if (!$this->debug()) { $cmd .= " > /dev/null 2>&1 &"; } //发送异步请求 exec($cmd, $output, $exit); return $exit == 0; } ?></code>
<code> function request($url, $payload) { $cmd = "curl -X POST -H 'Content-Type: application/json'"; $cmd.= " -d '" . $payload . "' " . "'" . $url . "'"; if (!$this->debug()) { $cmd .= " > /dev/null 2>&1 &"; } //发送异步请求 exec($cmd, $output, $exit); return $exit == 0; } ?></code>
<code>$a=exec($cmd, $output, $exit);</code>
Can’t you use PHP’s curl function?
Using curl should not work, but you can call the callback at the request address.
You can use a network framework like swoole
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
$response = curl_exec($ch );
swoole can be asynchronous, you can still do it synchronously