How does PHP request a URL without waiting for a URL return value to execute the following code directly?

Source: Internet
Author: User
Recently encountered in the development of a problem, the program 4th line request a URL, but the response time of the URL is very long, and my program does not use his return value, so I am wondering if I can send a request on the 4th row without waiting for his return value, directly execute the 5th line of code, OK? How do you do it if you can?

Reply content:

Recently encountered in the development of a problem, the program 4th line request a URL, but the response time of the URL is very long, and my program does not use his return value, so I am wondering if I can send a request on the 4th row without waiting for his return value, directly execute the 5th line of code, OK? How do you do it if you can?

Http://www.laruence.com/2008/04/14/318.html

Sleep (10) This 10-second operation is obviously inappropriate by the PHP process that is providing the Web service, because it will certainly cause a PHP worker process to be blocked. You might consider using Popen or Proc_open to invoke a CLI program asynchronously for some time-consuming operation.

/www/index.php
  
   

If you do not use this return, you can save this address directly to the database, which is executed by another program on the server.

If you use PHP-FPM, you can see this function.fastcgi_finish_request
You write the slow-running code behind this function.
http://php.net/manual/zh/function.fastcgi-finish-request.php

You can use PHP to extend swoole asynchronously.

    1. can use multithreading, pthread extension, http://php.net/manual/en/book.pthreads.php

    2. Put the request operation in the queue, execute asynchronously


  
     array(      'timeout' => 5     ) ));  $contents = file_get_contents('http://www.google.com', 0, $context);echo '5秒后到这'?>

Request with queue
Request with queue
Request with queue

This is definitely not possible!

Either you use the queue to execute.
Either you use Swoole or node. js

PHP is designed so that it can only be executed from beginning to end.

Producer/Consumer Model

A simple and rude notation that can be used to execute this logic with Register_shutdown_function ()

You do not need to return a request for the content of what you ask him to do ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.