Answer the question
In order to leave comments, you need to log in
How to switch from curl to multicurl?
There is too much load on the site, since there are about 20 API requests on the page.
Therefore, I think you need to switch to multithreading. I studied the instructions, but did not understand.
The point is this.
When a response is received from the api, files with cached data are created, from which information is then taken for output.
The query structure is like this.
Code
1)
<?php
$cache_ttl = 21600; // время жизни кэша в секундах
$cache_file_airlines = "tmp/ish.data";
$cache_file_products = "tmp/ish1.data";
$map = function ($array, $from, $to)
{
$result = [];
if (!empty($array) && is_array($array))
{
foreach ($array as $element)
{
$key = $element[$from] ? : null;
$value = $element[$to] ? : null;
if ($key && $value)
{
$result[$key] = $value;
}
}
}
return $result;
};
if (file_exists($cache_file_airlines) && (time() - filemtime($cache_file_airlines)) < $cache_ttl)
{
// берём кэшированные данные
$get_airlines = file_get_contents($cache_file_airlines);
}
else
{
$get_airlines = file_get_contents('https://site.ru/json/airlines.json');
file_put_contents($cache_file_airlines, $get_airlines);
}
$airlines = array_column($dataAER, 'name','iata');
if (file_exists($cache_file_products) && (time() - filemtime($cache_file_products)) < $cache_ttl)
{
// берём кэшированные данные
$response = file_get_contents($cache_file_products);
}
else
{
$ch = curl_init();
curl_setopt(
$ch,
CURLOPT_URL,
"https://site.ru/v3/prices_for_dates?origin=MOW&destination=AER&token=*******&departure_at=" . date('Y-m-d') );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"X-Access-Token: **********"
));
$response = curl_exec($ch);
curl_close($ch);
file_put_contents($cache_file_products, $response);
}
$products = json_decode($response, true);
$replace_value = function ($key, $val) use ($airlines)
{
$response = $val;
switch ($key)
{
case 'airline':
$response = $airlines[$val];
break;
}
return $response;
} ?>
<div class="table-responsive" style="overflow-x:hidden;background: var(--responsiv-color)!important; display: flex; flex-direction: column; width: 100%;margin-left: auto; margin-right: auto; list-style-type: none;overflow-y:hidden;">
<?php
if (isset($products['data']) && is_array($products['data']))
{
foreach ($products['data'] as $key => $dataAER)
?>
<?php
$cache_ttl = 21600; // время жизни кэша в секундах
$cache_file_airlines = "tmp/ish2.data";
$cache_file_products = "tmp/ish3.data";
$map = function ($array, $from, $to)
{
$result = [];
if (!empty($array) && is_array($array))
{
foreach ($array as $element)
{
$key = $element[$from] ? : null;
$value = $element[$to] ? : null;
if ($key && $value)
{
$result[$key] = $value;
}
}
}
return $result;
};
if (file_exists($cache_file_airlines) && (time() - filemtime($cache_file_airlines)) < $cache_ttl)
{
// берём кэшированные данные
$get_airlines = file_get_contents($cache_file_airlines);
}
else
{
$get_airlines = file_get_contents('https://site.ru/json/airlines.json');
file_put_contents($cache_file_airlines, $get_airlines);
}
$airlines = array_column($dataSIP, 'name','iata');
if (file_exists($cache_file_products) && (time() - filemtime($cache_file_products)) < $cache_ttl)
{
// берём кэшированные данные
$response = file_get_contents($cache_file_products);
}
else
{
$ch = curl_init();
curl_setopt(
$ch,
CURLOPT_URL,
"https://site.ru/v3/prices_for_dates?origin=MOW&destination=SIP&token=******&departure_at=" . date('Y-m-d') );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"X-Access-Token: ******"
));
$response = curl_exec($ch);
curl_close($ch);
file_put_contents($cache_file_products, $response);
}
$products = json_decode($response, true);
$replace_value = function ($key, $val) use ($airlines)
{
$response = $val;
switch ($key)
{
case 'airline':
$response = $airlines[$val];
break;
}
return $response;
} ?>
<div class="table-responsive" style="overflow-x:hidden;background: var(--responsiv-color)!important; display: flex; flex-direction: column; width: 100%;margin-left: auto; margin-right: auto; list-style-type: none;overflow-y:hidden;">
<?php
if (isset($products['data']) && is_array($products['data']))
{
foreach ($products['data'] as $key => $dataSIP)
?>
$dataSIP
and $dataAER
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question