Answer the question
In order to leave comments, you need to log in
Reasons for the slow work of api Yandex metrics on php?
Hello, why is this code very slow?
class APImetrika{
private $token = YA_TOKEN;
private $url_api = "https://api-metrika.yandex.ru/";
private $counter_id = YA_ID;
public function get_traffic($date1, $date2){
return $this->get_data(
$this->url_api.
"stat/v1/data?id=".
$this->counter_id.
"&metrics=ym:s:users,ym:s:visits,ym:s:pageviews".
"&pretty=1".
"&date1=".$date1.
"&date2=".$date2.
"&oauth_token=".
$this->token
);
}
private function get_data($url){
if($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = json_decode(
curl_exec($ch), true
);
curl_close($ch);
if(is_array($data)){
return $data;
}
}
return false;
}
}
$obj = new APImetrika();
$today = $obj->get_traffic(
date("Ymd"),
date("Ymd")
);
$last = $obj->get_traffic(
date("Ymd", strtotime("-1 days")),
date("Ymd", strtotime("-1 days"))
);
$last2 = $obj->get_traffic(
date("Ymd", strtotime("-2 days")),
date("Ymd", strtotime("-2 days"))
);
$last3 = $obj->get_traffic(
date("Ymd", strtotime("-3 days")),
date("Ymd", strtotime("-3 days"))
);
$last4 = $obj->get_traffic(
date("Ymd", strtotime("-4 days")),
date("Ymd", strtotime("-4 days"))
);
$last5 = $obj->get_traffic(
date("Ymd", strtotime("-5 days")),
date("Ymd", strtotime("-5 days"))
);
$last6 = $obj->get_traffic(
date("Ymd", strtotime("-6 days")),
date("Ymd", strtotime("-6 days"))
);
if ($('#traffic-chart').length) {
var chart = new Chartist.Line('#traffic-chart', {
labels: ['<?=date("d.m.y", strtotime("-6 days"));?>',
'<?=date("d.m.y", strtotime("-5 days"));?>',
'<?=date("d.m.y", strtotime("-4 days"));?>',
'<?=date("d.m.y", strtotime("-3 days"));?>',
'<?=date("d.m.y", strtotime("-2 days"));?>',
'<?=date("d.m.y", strtotime("-1 days"));?>',
''],
series: [
[<?php echo($last6["totals"][0]);?>, <?php echo($last5["totals"][0]);?>, <?php echo($last4["totals"][0]);?>, <?php echo($last3["totals"][0]);?>, <?php echo($last2["totals"][0]);?>, <?php echo($last["totals"][0]);?>, <?php echo($today["totals"][0]);?>],
[<?php echo($last6["totals"][1]);?>, <?php echo($last5["totals"][1]);?>, <?php echo($last4["totals"][1]);?>, <?php echo($last3["totals"][1]);?>, <?php echo($last2["totals"][1]);?>, <?php echo($last["totals"][1]);?>, <?php echo($today["totals"][1]);?>],
[<?php echo($last6["totals"][2]);?>, <?php echo($last5["totals"][2]);?>, <?php echo($last4["totals"][2]);?>, <?php echo($last3["totals"][2]);?>, <?php echo($last2["totals"][2]);?>, <?php echo($last["totals"][2]);?>, <?php echo($today["totals"][2]);?>]
]
}, {
low: 0,
showArea: true,
showLine: false,
showPoint: false,
fullWidth: true,
axisX: {
showGrid: false
}
});
chart.on('draw', function(data) {
if(data.type === 'line' || data.type === 'area') {
data.element.animate({
d: {
begin: 2000 * data.index,
dur: 2000,
from: data.path.clone().scale(1, 0).translate(0, data.chartRect.height()).stringify(),
to: data.path.clone().stringify(),
easing: Chartist.Svg.Easing.easeOutQuint
}
});
}
});
}
Answer the question
In order to leave comments, you need to log in
Most likely, you need to make 1 request and split the information by date, and not make 5 requests for each day.
The API, in theory, should contain such an opportunity to query immediately the period of dates
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question