目前,我使用exec / wget来回调URL而不阻止PHP脚本:
$newURL = /* URL we need to callback */
$newURL = escapeshellarg($newURL);
exec("curl -k $newURL > /dev/null &");
我想获得回调URL的响应并测试它是否以完全指定的字符串响应。但是,它不能阻止执行脚本的其余部分。
我怎样才能做到这一点?
答案 0 :(得分:0)
This post有一个使用分叉卷曲请求发送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) {
# ...
}
}