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