我有一个PHP站点的PHP CURL请求,它接受JSON和请求并返回JSON响应 . 通常不应超过1秒 . 但是,我已经看到CURL响应时间是可变的 . 有时,响应需要4秒,有时需要1.2秒 . 为什么会这样,如何让它更快,并在不到一秒的时间内处理整个请求?
以下是3次不同时间内同一请求的卷曲响应时间 .
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_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
)
2 回答
该网站还在做什么?如果它具有固定数量的线程(比方说),并且每个线程都忙,那么您的请求将必须等待线程为其提供服务 . 如果没有线程忙,那么它将立即得到服务 .
当然,最简单的方案是服务器CPU具有有限的资源并服务于不确定数量的请求,没有服务保证 .
CURL是在PHP中发出HTTP请求的最佳方式 . 几乎可以肯定,服务需要时间 . 喜欢在熟食店排队等候三明治 . 如果服务的速度不在您的手中,并且您没有可以使用的替代服务,那么最好的办法是确保在请求发生时继续加载页面上的其他所有内容 .