异步执行
异步执行curl 命令:
exec($this->buildCurlCommand($url, $data, $headers));
protected function buildCurlCommand($url, $data, $headers)
{
// TODO(dcramer): support ca_cert
$cmd = $this->curl_path.' -X POST ';
foreach ($headers as $key => $value) {
$cmd .= '-H ' . escapeshellarg($key.': '.$value). ' ';
}
$cmd .= '-d ' . escapeshellarg($data) . ' ';
$cmd .= escapeshellarg($url) . ' ';
$cmd .= '-m 5 '; // 5 second timeout for the whole process (connect + send)
if (!$this->verify_ssl) {
$cmd .= '-k ';
}
$cmd .= '> /dev/null 2>&1 &'; // ensure exec returns immediately while curl runs in the background
return $cmd;
}
在需要执行的命令后面加上这个就可以异步执行了。意思是把正常的输出和异常的输出都丢弃到/dev/null。然后再在后台执行,就不需等待执行结果,直接返回了。
> /dev/null 2>&1 &
和 > /dev/null 2>/dev/null & 是一样的