我正在使用 flipkart.com 提供的 API,这使我能够搜索并获得作为 json
输出的结果。
我使用的代码是:
$snapword = $_GET['p'];
$snapword = str_replace(' ','+',$snapword);
$headers = array(
'Fk-Affiliate-Id: myaffid',
'Fk-Affiliate-Token: c0f74c4esometokesndad68f50666'
);
$pattern = "@\(.*?\)@";
$snapword = preg_replace($pattern,'',$snapword);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://affiliate-api.flipkart.net/affiliate/search/json?query='.$snapword.'&resultCount=5');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_USERAGENT,'php');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$snapdeal = curl_exec($ch);
curl_close($ch);
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Process Time: {$time}";
它花费的时间是:Process Time: 5.3794288635254
哪个太多了,关于如何减少它有什么想法吗?
最佳答案
使用curl_getinfo()
以检索更准确的信息。它还显示解析 DNS 等花费了多少时间。
您可以使用以下键查看每个步骤所用的确切时间:
CURLINFO_TOTAL_TIME - Total transaction time in seconds for last transfer
CURLINFO_NAMELOOKUP_TIME - Time in seconds until name resolving was complete
CURLINFO_CONNECT_TIME - Time in seconds it took to establish the connection
CURLINFO_PRETRANSFER_TIME - Time in seconds from start until just before file transfer begins
CURLINFO_STARTTRANSFER_TIME - Time in seconds until the first byte is about to be transferred
CURLINFO_SPEED_DOWNLOAD - Average download speed
CURLINFO_SPEED_UPLOAD - Average upload speed
$info = curl_getinfo($curl);
echo $info['connect_time']; // Same as above, but lower letters without CURLINFO
API 很可能很慢。
您可以尝试更改为更快的 DNS 服务器(在 Linux 中:/etc/resolv.conf
)。
除此之外,您无能为力。
关于php - 提高 CURL 速度 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30368503/