php curl不等待返回,PHP cURL只需要发送而不是等待响应

我需要一个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

来源: https://codeday.me/bug/20191005/1857042.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值