Asynchronous HTTP requests in PHP

后端 未结 2 1670
臣服心动
臣服心动 2020-12-03 14:25

Is there any sane way to make a HTTP request asynchronously in PHP without throwing out the response? I.e., something similar to AJAX - the PHP script initiates the request,

相关标签:
2条回答
  • 2020-12-03 15:05

    Yes, depending on the traffic of your site, spawning a separate PHP process for running a script could be devastating. It would be more efficient to use shell_exec() to start a background process that saves the output to a filename you already know, but even this could be resource intensive.

    You could also have a request queue stored in a database. A single, separate background process would pull the job, execute it, and save the output, possibly setting a flag in the DB that your web process could check.

    If you're going to use the DB queue approach, use curl_multi* class of functions to send all queued requests at once. This will limit the execution time of each iteration in your background process to the longest request time.

    0 讨论(0)
  • 2020-12-03 15:07

    V5 may not be threaded, but you can create applications that exploit in-process multitasking.

    Check out the following article: "Develop multitasking applications with PHP V5" from IBM DeveloperWorks. You can find it here http://www.ibm.com/developerworks/web/library/os-php-multitask/

    0 讨论(0)
提交回复
热议问题