开发者

Variable CURL Response Time

开发者 https://www.devze.com 2022-12-17 12:13 出处:网络
I have a PHP CURL request to an HTTPS site which takes a JSON and request and returns a JSON response. Usually it should not take more than 1 second. But, I have seen that the CURL response time is va

I have a PHP CURL request to an HTTPS site which takes a JSON and request and returns a JSON response. Usually it should not take more than 1 second. But, I have seen that the CURL response time is variable. Sometimes, it takes 4 seconds to respond and sometimes 1.2 seconds. Why is this so and How can I make it faster and process the whole request in less than a second?

Following is the curl response times for the same request in 3 different times..

Array
(
    [url] => https://xx.xx.xxx.xx/site/y
    [content_type] => application/json
    [http_code] => 200
    [header_size] => 198
    [request_size] => 358
    [filetime] => -1
    [ssl_verify_result] => 20
    [redirect_count] => 0
    [total_time] => 4.213941
    [namelookup_time] => 2.1E-5
    [connect_time] => 0.015284
    [pretransfer_time] => 0.722955
    [size_upload] => 222
    [size_download] => 553
    [speed_download] => 131
    [speed_upload] => 52
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 4.015112
    [redirect_time] => 0
)


Array
(
    [url] => https://xx.xx.xxx.xx/site/y
    [content_type] => application/json
    [http_code] => 200
    [header_size] => 198
    [request_size] => 358
    [filetime] => -1
    [ssl_verify_result] => 20
    [redirect_count] => 0
    [total_time] => 1.27581
    [namelookup_time] => 8.3E-5
    [connect开发者_如何学Go_time] => 0.016223
    [pretransfer_time] => 0.104996
    [size_upload] => 222
    [size_download] => 553
    [speed_download] => 433
    [speed_upload] => 174
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 1.273922
    [redirect_time] => 0
)

Array
(
    [url] => https://xx.xx.xxx.xx/site/y
    [content_type] => application/json
    [http_code] => 200
    [header_size] => 198
    [request_size] => 358
    [filetime] => -1
    [ssl_verify_result] => 20
    [redirect_count] => 0
    [total_time] => 1.294147
    [namelookup_time] => 7.0E-5
    [connect_time] => 0.025723
    [pretransfer_time] => 0.123456
    [size_upload] => 222
    [size_download] => 553
    [speed_download] => 427
    [speed_upload] => 171
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 1.292577
    [redirect_time] => 0
)


What else is the site doing ? If it has a fixed number of threads (say), and each is busy, then your request will have to wait for a thread to service it. If no threads are busy, then it'll be serviced immediately.

Of course the simplest scenario is given that the server CPU has finite resources and servicing an indeterminate number of requests, there are no service guaranteees.


CURL is a the best way to make HTTP requests in PHP. It's almost certainly the service that is taking time. Like waiting in line for a sandwich at the deli. If the speed of the service is out of your hands, and there are no alternative services you can use, then the best thing to do is to make sure everything else on your page continues to load while the request is being made.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号