How to run the PHP code asynchronous

前端 未结 5 1426
小蘑菇
小蘑菇 2020-11-27 06:55

How can I run PHP code asynchronously without waiting? I have a long run (almost infinite) that should run while server starts and should process asynchronously without wait

5条回答
  •  伪装坚强ぢ
    2020-11-27 07:31

    thanks Todd Chaffee but it is not working for me so i edited your code i hope you will not mind and may be it will also help others with this technique

    cornjobpage.php //mainpage

         
    
    
             &$val) {
                  if (is_array($val)) $val = implode(',', $val);
                    $post_params[] = $key.'='.urlencode($val);  
                }
                $post_string = implode('&', $post_params);
    
                $parts=parse_url($url);
    
                $fp = fsockopen($parts['host'],
                    isset($parts['port'])?$parts['port']:80,
                    $errno, $errstr, 30);
    
                $out = "GET ".$parts['path']."?$post_string"." HTTP/1.1\r\n";//you can use POST instead of GET if you like
                $out.= "Host: ".$parts['host']."\r\n";
                $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
                $out.= "Content-Length: ".strlen($post_string)."\r\n";
                $out.= "Connection: Close\r\n\r\n";
                fwrite($fp, $out);
                fclose($fp);
            }
            ?>
    

    testpage.php

         testValue
        ?>
    

提交回复
热议问题