目前,我正在使用 exec / wget 来回调一个 URL,而不会阻止 PHP 脚本:
$newURL = /* URL we need to callback */
$newURL = escapeshellarg($newURL);
exec("curl -k $newURL > /dev/null &");
我想获取回调 URL 的响应并测试它是否以指定的字符串响应。但是,它不能阻止脚本的其余部分执行。
我怎样才能做到这一点?
这篇文章有一个使用分叉的 curl 请求发送 aysnc 请求的解决方案:
private 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;
}
它还有一个解决方案,您可以在立即关闭套接字之前写入套接字(在得到响应之前):
private function request($body) {
$protocol = "ssl";
$host = "api.segment.io";
$port = 443;
$path = "/v1/" . $body;
$timeout = $this->options['timeout'];
try {
# Open our socket to the API Server.
$socket = fsockopen($protocol . "://" . $host, $port,
$errno, $errstr, $timeout);
# Create the request body, and make the request.
$req = $this->create_body($host, $path, $content);
fwrite($socket, $req);
# ...
} catch (Exception $e) {
# ...
}
}