我需要一个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发送了大量请求,因此我的脚本非常慢,因为它等待每一个请求。
感谢任何帮助。
答案 0 :(得分:11)
发件人文件示例./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
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');
答案 1 :(得分:4)
答案 2 :(得分:1)
现在有点晚了,但感兴趣的人的解决方案是CURLOPT_RETURNTRANSFER
需要设置为TRUE
,而不是false
。这样,curl_exec
函数立即返回一个值,而不是在返回之前等待请求完成 - 即它以异步方式而不是同步方式。
示例:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
答案 3 :(得分:0)
如何判断请求是否成功?您需要至少等待服务器的状态代码来确定。如果延迟是问题,请查看curl multi API以并行执行多个请求。您应该能够设置一个写回调函数,以便在返回状态代码后中止对返回数据的接收。
答案 4 :(得分:0)
(当然已经很长时间了,但我认为这个问题已经过时了)
使用 file_get_contents :
//url of php to be called
$url = "example.php/test?id=1";
//this will set the minimum time to wait before proceed to the next line to 1 second
$ctx = stream_context_create(['http'=> ['timeout' => 1]]);
file_get_contents($url,null,$ctx);
//the php will read this after 1 second
使用 cURL :
//url of php to be called
$url = "example.php/test?id=1";
$test = curl_init();
//this will set the minimum time to wait before proceed to the next line to 100 milliseconds
curl_setopt_array($test,[CURLOPT_URL=>$url,CURLOPT_TIMEOUT_MS=>100,CURLOPT_RETURNTRANSFER=>TRUE]);
curl_exec($test);
//this line will be executed after 100 milliseconds
curl_close ($test);
在两种情况下,被调用的php必须设置ignore_user_abort(true)
。
两种情况下都不会打印结果,但是要小心设置的超时时间,该时间必须大于被调用的php开始产生结果所需的时间。
答案 5 :(得分:-1)
$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);
对我来说很好。
在PHP 7.1.14 Windows上测试