发件人文件示例./ajax/sender.php
下面我们尝试在不等待应答的情况下对 php 脚本进行 ping 操作
$url = 'https://127.0.0.1/ajax/received.php';
$curl = curl_init();
$post['test'] = 'examples daata'; // our data todo in received
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_POST, TRUE);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_USERAGENT, 'api');
//curl_setopt($curl, CURLOPT_TIMEOUT, 1); //if your connect is longer than 1s it lose data in POST better is finish script in recevie
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
curl_setopt($curl, CURLOPT_FORBID_REUSE, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($curl, CURLOPT_DNS_CACHE_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
curl_exec($curl);
curl_close($curl);
接收文件示例./ajax/received.php
如果您使用 fastcgi,请编辑 2019 年,只需完成 fastcgi 和浏览器关闭连接,但脚本仍将运行到最后。
如何完成脚本: PHP mod_fcgi with fastcgi_finish_request();
对于 Apache2:
ob_end_flush();
flush();
对于 php-fpm
fastcgi_finish_request(); $this->db->query('UPDATE new_hook_memory SET new=new+1 WHERE id=1');
旧版本:
ob_end_clean(); //if our framework have turn on ob_start() we don't need bufering respond up to this script will be finish
header("Connection: close\r\n"); //send information to curl is close
header("Content-Encoding: none\r\n"); //extra information
header("Content-Length: 1"); //extra information
ignore_user_abort(true); //script will be exisits up to finish below query even web browser will be close before sender get respond
//we doing here what we would like do
$this->db->query('UPDATE new_hook_memory SET new=new+1 WHERE id=1');