PHP cURL 只需要发送而不需要等待响应 [英] PHP cURL required only to send and not wait for response

查看:34
本文介绍了PHP cURL 只需要发送而不需要等待响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个 PHP cURL 配置,以便我的脚本能够发送请求并忽略 API 发送的答案.

I need a PHP cURL configuration so that my script is able to send requests and ignore the answers sent by the API.

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
//curl_setopt($ch, CURLOPT_TIMEOUT_MS, 100);
$result = curl_exec($ch);
echo $result;
curl_close ($ch);

我尝试添加://curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);//curl_setopt($ch, CURLOPT_TIMEOUT_MS, 100);

I tried adding: // curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); //curl_setopt($ch, CURLOPT_TIMEOUT_MS, 100);

但它不能正常工作并且 API 网络服务器没有收到请求.

But its not working properly and the API webserver is not receiving the requests.

这样做的原因是我向 API 发送了大量请求,因此我的脚本非常慢,因为它等待每个请求.

The reason for this is I am sending large amount of requests to the API therefore my script is very slow because it waits for each and every request.

感谢任何帮助.

推荐答案

发送文件示例 ./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');    

这篇关于PHP cURL 只需要发送而不需要等待响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆