且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP cURL 只需要发送而不需要等待响应

更新时间:2022-06-01 05:20:35

发送文件示例 ./ajax/sender.php

Sender file example ./ajax/sender.php

下面我们尝试只对 php 脚本进行 ping 操作,而无需等待答案

Below we trying just make ping to php script without waiting on answer

    $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); 
    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

Received file example ./ajax/received.php

EDIT 2019 如果您使用 fastcgi 刚刚完成 fastcgi 和浏览器关闭连接但脚本仍然会工作到结束.

EDIT 2019 if you using fastcgi just finish fastcgi and browser close connection but script still will be working up to end.

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
"); //send information to curl is close
    header("Content-Encoding: none
"); //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');