T
T
Tutucu2021-01-09 18:42:57
PHP
Tutucu, 2021-01-09 18:42:57

How to output JSON if PHP script execution duration is more than a certain value?

Hello! I have an API, I need to make it so that if processing a request to it takes more than 2 seconds, a json message is displayed. The API consists of nginx, php and mysql. JSON does not change, you can give a static file with it.
As I understand it, it is impossible to do this through php, because. the set_time_limit() function does not take into account the time of requests to mysql. I tried to do it through TimeOut in nginx and fpm, but I need the response code to be 200, not 504.
How can I implement this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nokimaro, 2021-01-09
@Tutucu

through nginx we set up the required timeout and catch 504 error

  • proxy_connect_timeout
  • proxy_send_timeout
  • proxy_read_timeout
  • send_timeout

https://gist.github.com/weapp/99049e69477f924dafa7
error_page 504 /504.json;
location /504.json{
    add_header 'Content-Type' 'application/json charset=UTF-8';
    return 200 '{"error": {"status_code": 200, "status": "Internal Server Error"}}';
}

I
iBird Rose, 2017-04-06
@iNickolay

transition on display does not work, and height must be set to the exact height, not auto. so either do it through js. or operate through max-height. first 0, then 1000px for example
https://jsfiddle.net/b0dxdou9/1/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question