PHP cURL required only to send and not wait for response

前端 未结 6 1867
被撕碎了的回忆
被撕碎了的回忆 2020-12-09 09:11

I need a PHP cURL configuration so that my script is able to send requests and ignore the answers sent by the API.

curl_setopt($ch,CURLOPT_URL,$url);
curl_se         


        
6条回答
  •  情歌与酒
    2020-12-09 09:36

    These two solutions work well for me

    ( Of course it has been a long time, but I don't think this question is outdated )

    using 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 
    

    using 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); 
    

    in both case the called php must set ignore_user_abort(true).

    And the result will not be printed in both case, but be careful with the timeout you will set, it needs to be greater than the time that the called php needs to start yielding results.

提交回复
热议问题