我有一个执行订单查找的php页面。在订单摘要页面中,我执行while
循环以显示所有订单。通常这样做很好,不是问题。最近我决定添加基于其JIRA问题/票证状态查看订单“状态”的功能。因此,例如,如果JIRA票证状态是打开的,那么订单状态也是“打开”。如果JIRA票据关闭,订单状态也会“关闭”。
我正在进行JIRA票证查询的方式是通过CURL GET请求。以下是代码示例:
// CHECK JIRA TICKET
$username = 'xxxx';
$password = 'xxxx';
$proxy = 'http://xxxx.xxxx.xxxx:8080/';
$url = "http://xxxx.xxxx.xxxx/rest/api/2/issue/".$row['jira_ticket']."?fields=status";
$ch = curl_init();
$headers = array(
'Accept: application/json',
'Content-Type: application/json'
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$ticket_result = curl_exec($ch);
curl_close($ch);
为了让我获得每个订单的JIRA
票证状态,我在while循环中运行此CURL GET
请求。代码示例:
$results = mysqli query...;
while ($row = mysqli_fetch_array($results))
{
....define some variables
....the CURL code
....etc
}
我遇到的问题是订单越多,加载页面所需的时间就越长。我知道这肯定是由于while循环中的CURL GET
请求,因为如果我删除它,那么页面会快速加载。当我调试CURL请求时,我看到的最长时间是starttransfer_time
。例如:
[http_code] => 405
[header_size] => 639
[request_size] => 239
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.294348
[namelookup_time] => 0.004216
[connect_time] => 0.004885
[pretransfer_time] => 0.004928
[size_upload] => 0
[size_download] => 1013
[speed_download] => 3441
[speed_upload] => 0
[download_content_length] => 1013
[upload_content_length] => -1
[starttransfer_time] => 0.294315
[redirect_time] => 0
有时可以达到0.5秒。所以我的问题......有什么办法可以加快CURL
请求的速度吗?如果没有,是否有更好的方法来接近我正在尝试做的事情?
编辑:
我有一个解决方法,但不是真正的答案。暂时我只将“订单状态”存储为MySQL中的一列,并将实际的JIRA CURL
请求作为后台cron作业运行在另一个文件中,并且每隔一段时间运行一次。它并不完美,但至少可以让页面更快出现。