加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

增加CURL速度php

发布时间:2020-12-14 01:43:44 所属栏目:Linux 来源:网络整理
导读:我正在使用flipkart.com提供的API,这允许我搜索并获得结果输出为json. 我使用的代码是: $snapword = $_GET['p'];$snapword = str_replace(' ','+',$snapword);$headers = array( 'Fk-Affiliate-Id: myaffid','Fk-Affiliate-Token: c0f74c4esometokesndad68f
我正在使用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}";

它的时间是:处理时间: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).

除此之外,你无能为力.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读