PHP cURL 只需要发送而不需要等待响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8024821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP cURL required only to send and not wait for response
提问by Reza
I need a PHP cURL configuration so that my script is able to send requests and ignore the answers sent by the API.
我需要一个 PHP cURL 配置,以便我的脚本能够发送请求并忽略 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);
I tried adding: // curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); //curl_setopt($ch, CURLOPT_TIMEOUT_MS, 100);
我尝试添加: // curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); //curl_setopt($ch, CURLOPT_TIMEOUT_MS, 100);
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.
原因是我向 API 发送了大量请求,因此我的脚本非常慢,因为它等待每个请求。
Any help is appreciated.
任何帮助表示赞赏。
采纳答案by Kamil D?browski
Sender file example ./ajax/sender.php
发件人文件示例 ./ajax/sender.php
Below we trying just make ping to php script without waiting on answer
下面我们尝试只对 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);
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);
Received file example ./ajax/received.php
接收文件示例 ./ajax/received.php
EDIT 2019 if you using fastcgi just finish fastcgi and browser close connection but script still will be working up to end.
编辑 2019 如果您使用 fastcgi 刚刚完成 fastcgi 和浏览器关闭连接,但脚本仍然会工作到最后。
fastcgi_finish_request(); $this->db->query('UPDATE new_hook_memory SET new=new+1 WHERE id=1');
Old version:
旧版本:
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');
回答by lukenofurther
A bit late now but the solution to this for anyone interested is that CURLOPT_RETURNTRANSFER
needs to be set to TRUE
, not false
. That way the curl_exec
function returns a value immediately rather than waiting for the request to complete before returning - i.e. it acts asynchronously rather than synchronously.
现在有点晚了,但对于任何感兴趣的人来说,解决这个问题的方法是CURLOPT_RETURNTRANSFER
需要设置为TRUE
,而不是false
. 这样,curl_exec
函数立即返回一个值,而不是等待请求完成后再返回——即它是异步而不是同步的。
Example:
例子:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
回答by Dan
How can you tell if the request succeeded or not? You need to wait for at least the status code from the server to determine that. If latency is the issue, look at the curl multi API to perform multiple requests in parallel. You should be able to set a write callback function to abort reception of returned data once the status code has been returned.
如何判断请求是否成功?您至少需要等待来自服务器的状态代码才能确定。如果延迟是问题,请查看 curl multi API 以并行执行多个请求。您应该能够设置一个写回调函数,以在返回状态代码后中止返回数据的接收。
回答by l00k
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
curl_exec($ch);
curl_close($ch);
That works well for me.
Tested on PHP 7.1.14 Windows
这对我很有效。
在 PHP 7.1.14 Windows 上测试