PHP cURL只需要发送而不是等待响应
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP cURL只需要发送而不是等待响应,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2089字,纯文字阅读大概需要3分钟。
内容图文
![PHP cURL只需要发送而不是等待响应](/upload/InfoBanner/zyjiaocheng/701/3581115ad80d4d64bc30321eb1db558f.jpg)
我需要一个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);
我尝试添加:
// curl_setopt($ch,CURLOPT_RETURNTRANSFER,false);
// curl_setopt($ch,CURLOPT_TIMEOUT_MS,100);
但它没有正常工作,API网络服务器没有收到请求.
原因是我向API发送了大量请求,因此我的脚本非常慢,因为它等待每个请求.
任何帮助表示赞赏.
解决方法:
发件人文件示例./ajax/sender.php
下面我们尝试ping php脚本而不等待回答
$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
编辑2019如果您使用fastcgi刚刚完成fastcgi和浏览器关闭连接,但脚本仍将工作到最后.
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');
内容总结
以上是互联网集市为您收集整理的PHP cURL只需要发送而不是等待响应全部内容,希望文章能够帮你解决PHP cURL只需要发送而不是等待响应所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。