Home  >  Q&A  >  body text

Asynchronous requests - Is there a way in php to do asynchronous posts similar to js? fsockopen has no way to get the request result

When sending emails in batches, we need to obtain the sending results, so we need to wait for the request results.

Is there a way in php to request the interface asynchronously, then get the data, and then execute the callback function?

Now it is a synchronous request interface, and then wait, which is very slow. . And the server cpu will increase during execution. .

Please enlighten me

The key point now is not to wait for the return of the request, because it is very slow, and directly call a certain piece of code when there is a return. . The redis queue is already in use, and the script is executing

过去多啦不再A梦过去多啦不再A梦2679 days ago723

reply all(5)I'll reply

  • 高洛峰

    高洛峰2017-05-27 17:44:08

    php installs multi-threading extension. You can achieve it by extending it yourself, and js is also implemented in a single thread.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-27 17:44:08

    You can try to use reactphp,workerman,swoolethese libraries to perform asynchronous tasks

    reply
    0
  • 怪我咯

    怪我咯2017-05-27 17:44:08

    This can be achieved with the curl_multi that comes with php

    reply
    0
  • ringa_lee

    ringa_lee2017-05-27 17:44:08

    It is strongly recommended that you use workererman. If you use fsockopen, this is only sent out asynchronously, but it is not returned! If you want asynchronous processing, use workererman. Of course, swoole can also be used, but this has C language , some error messages are from C. If you know C you can also use this. Getting started with workerman is extremely easy. The specific implementation logic is as follows:
    When you need to be asynchronous, you directly call Workerman to execute your logic. This Workerman is executed synchronously. After
    Workerman executes the logic, it calls the task task in an asynchronous manner. This is workerman's async. workerman returns directly.
    When your task is completed, it will be passed to the workerman, and the workerman will execute your asynchronous logic.
    Give you a backup framework with PHP asynchronous communication in it
    github-php-awesome translated

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-27 17:44:08

    Queue

    1. Store the information that needs to be sent in Redis or other memory cache.

    2.php provides an interface that requires access to the key token. This interface loops through the email data cached in redis and sends it.

    3. There are solutions for using scheduled tasks in both windows and linux. Regularly go to http to request this php interface and complete the sending. It is equivalent to asynchronous.

    reply
    0
  • Cancelreply